Matthew Suderman
2007-Apr-04 22:10 UTC
[R] unexpected behavior when creating a list of functions
I wanted to create a list of functions whose output differs depending the value
of a variable when the function was created. Generally this did not work. Each
function was exactly the same, as in the simple example below:
get_data_function <- function(v) {
function() {
print(v)
}
}
data_functions <- sapply(1:10,function(v) get_data_function(v))
(data_functions[[1]])() # prints 10!
However, if I insert a statement in get_data_function to print argument v, then
I get the different functions that I wanted:
get_data_function <- function(v) {
print(v)
function() {
print(v)
}
}
data_functions <- sapply(1:10,function(v) get_data_function(v))
(data_functions[[1]])() # prints 1, as expected!
I have two questions about this:
* Is this a bug in R?
* Is there a more direct way to get the effect of printing v?
Matt
---------------------------------
Don't get soaked. Take a quick peek at the forecast
[[alternative HTML version deleted]]
jim holtman
2007-Apr-04 22:44 UTC
[R] unexpected behavior when creating a list of functions
'v' was not evaluated when you defined the function; this is
'lazy'
evaluation in R; try
get_data_function <- function(v) {
v # cause 'v' to be evaluated
function() {
print(v) # now it is defined when the function is
}
}
data_functions <- sapply(1:10,function(v) get_data_function(v))
(data_functions[[1]])()
On 4/4/07, Matthew Suderman <matthewsuderman@yahoo.com>
wrote:>
> I wanted to create a list of functions whose output differs depending the
> value of a variable when the function was created. Generally this did not
> work. Each function was exactly the same, as in the simple example below:
>
> get_data_function <- function(v) {
> function() {
> print(v)
> }
> }
> data_functions <- sapply(1:10,function(v) get_data_function(v))
> (data_functions[[1]])() # prints 10!
>
> However, if I insert a statement in get_data_function to print argument v,
> then I get the different functions that I wanted:
>
> get_data_function <- function(v) {
> print(v)
> function() {
> print(v)
> }
> }
> data_functions <- sapply(1:10,function(v) get_data_function(v))
> (data_functions[[1]])() # prints 1, as expected!
>
> I have two questions about this:
> * Is this a bug in R?
> * Is there a more direct way to get the effect of printing v?
>
> Matt
>
>
>
> ---------------------------------
> Don't get soaked. Take a quick peek at the forecast
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
[[alternative HTML version deleted]]
Thomas Lumley
2007-Apr-04 22:49 UTC
[R] unexpected behavior when creating a list of functions
On Wed, 4 Apr 2007, Matthew Suderman wrote:> I wanted to create a list of functions whose output differs depending > the value of a variable when the function was created. Generally this > did not work. Each function was exactly the same, as in the simple > example below: > > get_data_function <- function(v) { > function() { > print(v) > } > } > data_functions <- sapply(1:10,function(v) get_data_function(v)) > (data_functions[[1]])() # prints 10! > > However, if I insert a statement in get_data_function to print argument > v, then I get the different functions that I wanted: > > get_data_function <- function(v) { > print(v) > function() { > print(v) > } > } > data_functions <- sapply(1:10,function(v) get_data_function(v)) > (data_functions[[1]])() # prints 1, as expected! > > I have two questions about this: > * Is this a bug in R?No, it's lazy evaluation at work.> * Is there a more direct way to get the effect of printing v?The function force() is designed for this -- it doesn't do anything special that any other evaluation wouldn't do, but it makes clear that all you are doing is forcing evaluation. -thomas