A couple of observations:
(cross-posting this as it's relevant for rook and rApache groups; hope
you don't mind)
1. setRefClass stores it's class definition based on it's argument
"where". When you were running your code within R without rApache, you
were probably assigning it to the global environment. In your case
under rApache it's stored in an anonymous environment specific to your
file. Read more about this argument in ?getRefClass.
2. Reference Class methods have a different scope chain than typical
function calls. They can only see the other objects and methods of the
class, and failing that I believe they look to the global environment
and then the rest of the search path. In your case Foo would never be
found in Bar::call since it's lexically found in the same file but not
as I described. Make sense?
Here's a solution that works:
# Explicitly add the reference class to the global environment
setRefClass('Foo', methods = list(foo = function() 'foo'),where=globalenv())
Bar <- setRefClass(
'Bar',
methods = list(
call = function(env) {
# Create a local variable Foo. Note that I had to qualify
getRefClass since there's a
# parent method named getRefClass which takes different
arguments. You can chalk
# that up to bad language design or "just another R quirk" :)
# And of course we know where 'Foo' is so we say where it is.
Foo <- methods::getRefClass('Foo',where=globalenv())
req <- Rook::Request$new(env)
res <- Rook::Response$new()
foo <- Foo$new()
res$write(foo$foo())
res$finish()
}
)
)
On Mon, May 6, 2013 at 4:00 PM, Jeremy Stephens
<
jeremy.f...@vanderbilt.edu> wrote:
> Jeff,
>
> I've defined a reference class that I'm using inside of a Rook app.
> Something like this:
>
> Foo <- setRefClass('Foo', methods = list(foo = function() 'foo'))
> Bar <- setRefClass(
> 'Bar',
> methods = list(
> call = function(env) {
> req <- Rook::Request$new(env)
> res <- Rook::Response$new()
> foo <- Foo$new()
> res$write(foo$foo())
> res$finish()
> }
> )
> )
>
> When I run this in R interactively with Rhttpd, it works. In the RApache
> environment, though, it can't find the Foo class.
>
> Any ideas?
>
> Thanks,
> Jeremy
--
http://biostat.mc.vanderbilt.edu/JeffreyHorner