[R] foreach and deparse(substitute(x))

Hao Cen hcen at andrew.cmu.edu
Fri Dec 11 15:45:00 CET 2009


Hi,

I would like to get the actual object name passed as a parameter of a
function and am using deparse(substitute(x)) to do that. It doesn't work
when it is used along with the foreach package. Appreciate if any one can
give some suggestions on how to make it work with foreach.

FUN.aaa <- function() {
}

ff =  function( FUN){
	foreach(i = 1:3) %dopar% {
		print(sprintf("deparse(substitute(FUN)) = %s", deparse(substitute(FUN))))
		return(NULL)
	}
}

ff(FUN.aaa)

# will print
ff(FUN.aaa)
[1] "deparse(substitute(FUN)) = FUN"
[1] "deparse(substitute(FUN)) = FUN"
[1] "deparse(substitute(FUN)) = FUN"
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL


If there is no foreach, it works

FUN.aaa <- function() {
}

ff =  function( FUN){
		print(sprintf("deparse(substitute(FUN)) = %s", deparse(substitute(FUN))))
	}
}

ff(FUN.aaa)

#It works and prints
> ff(FUN.aaa)
ff(FUN.aaa)
[1] "deparse(substitute(FUN)) = FUN.aaa"
thanks

Jeff




More information about the R-help mailing list