I would like to ask how can we get a value of a function which is embedded in another function, as in the following example:
message <- function() {
inside.message <- function() {
return("inside.message")
}
}
run.f <- function() {
return.inside.mesage <- message()
print(return.inside.mesage)
}
run.f() # We do not get "inside.message"
Thank you in advance, all of you
As you wrote it, the function
messagereturns a function, which if evaluated will return “inside.message”. So there are a couple ways to get R to print “inside.message”.First way:
In the
messagefunction, add the linereturn(inside.message())so that the functioninside.messageis evaluated and the result is returned, instead of returning the function itself:Then evaluating
run.f()will also print “inside.message”.The second way:
Leave
messageas you have it and changerun.f()to the followingAbove, you assign the function returned by
message()to the objectreturn.inside.messageand then evaluate that function.