EDIT: I have a few functions with multiple arguments I'd like to apply over a list of lists. One of the arguments is also a list of lists.
Both functions have multiple parameters. two of which I have to indicate recursively across the lists of lists.
say I have the two following lists of lists.
mylist <- list(list(10,12,13,14,15), list(5,6,7,8,9))
m <- list(list(2,2,2,3,4), list(3,3,4,4,5))
and the functions
func1 <- function(x, att1 = m, const = 10){
e <- x^m + const
return(e)
}
func2 <- function(x, att2 = m, const = 10){
d <- sqrt(x)/m + const
return(d)
}
I don't know how to address the right argument, att1 or att2, when I want to call each function.
I tried the function below using eval(substitute(att1 = a))
nested_function <- function(df_list, FUN = func1, changing_param = a, ...){
nested_output <- lapply(seq(df_list), function(i){
lapply(seq(df_list[[i]]), function(j){
FUN(df[[i]][[j]], eval(substitute(changing_param))[[i]][[j]],...)
})
})
return(nested_output)
}
result <- nested_function(df_list, FUN = func1, changing_param = 'att1 = a')
and got the following error:
Error in df[[i]] : object of type 'closure' is not subsettable
6.
FUN(df[[i]][[j]], eval(substitute(changing_param))[[i]][[j]],
...)
5.
FUN(X[[i]], ...)
4.
lapply(seq(df_list[[i]]), function(j) {
FUN(df[[i]][[j]], eval(substitute(changing_param))[[i]][[j]],
...)
})
3.
FUN(X[[i]], ...)
2.
lapply(seq(df_list), function(i) {
lapply(seq(df_list[[i]]), function(j) {
FUN(df[[i]][[j]], eval(substitute(changing_param))[[i]][[j]],
...) ...
1.
nested_function(mylist, changing_param = m)
My problem is how to make the FUN() call recognize a as att1 in func1 and att2 in func2, since I have to designate them for each function (I can't just put the argument there).
any suggestions?
If you need to dynamically build parameter names, you often have to end up using do.call (at least with base R). I'm sure quite how all your variables were supposed to work in your example, so here's an adapted version that runs.
df_list <- list(list(10,12,13,14,15), list(5,6,7,8,9))
param_list <- list(list(2,2,2,3,4), list(3,3,4,4,5))
func1 <- function(x, att1 = m, const = 10){
e <- x^att1 + const
return(e)
}
func2 <- function(x, att2 = m, const = 10){
d <- sqrt(x)/att2 + const
return(d)
}
nested_function <- function(df_list, param_list, FUN = func1, changing_param = "a", ...){
nested_output <- lapply(seq(df_list), function(i){
lapply(seq(df_list[[i]]), function(j){
params <- list(df_list[[i]][[j]], param_list[[i]][[j]], ...)
names(params)[2] <- changing_param
do.call(FUN, params)
})
})
return(nested_output)
}
nested_function(df_list, param_list, func1, changing_param = 'att1', const=1)
nested_function(df_list, param_list, func2, changing_param = 'att2', const=2)
Here we pass in the name of the parameter we want as a string. Then when we build the parameter we are going to pass to the function, we rename the parameter we are passing in with the name supplied, then just call the function.
Related
I'm making a function (myFUN) that calls parallel::parApply at one point, with a function yourFUN that is supplied as an argument.
In many situations, yourFUN will contain custom functions from the global environment.
So, while I can pass "yourFUN" to parallel::clusterExport, I cannot know the names of functions inside it beforehand, and clusterExport returns me an error because it cannot find them.
I don't want to export the whole enclosing environment of yourFUN, since it might be very big.
Is there a way for me to export only the variables necessary for running yourFUN?
The actual function is very long, here is a minimized example of the error:
mydata <- matrix(data = 1:9, 3, 3)
perfFUN <- function(x) 2*x
opt_perfFUN <- function(y) max(perfFUN(y))
avg_perfFUN <- function(w) perfFUN(mean(w))
myFUN <- function(data, yourFUN, n_cores = 1){
cl <- parallel::makeCluster(n_cores)
parallel::clusterExport(cl, varlist = c("yourFUN"), envir = environment())
parallel::parApply(cl, data, 1, yourFUN)
}
myFUN(data = mydata, yourFUN = opt_perfFUN)
myFUN(data = mydata, yourFUN = avg_perfFUN)
Error in checkForRemoteErrors(val) : one node produced an error: could not find function "perfFUN"
Thank you very much!
A possible solution, use:
myFUN <- function(data, yourFUN, n_cores = 1) {
cl <- parallel::makeCluster(n_cores)
on.exit(parallel::stopCluster(cl), add = TRUE)
envir <- environment(yourFUN)
parallel::clusterExport(cl, varlist = ls(envir), envir = envir)
parallel::parApply(cl, data, 1, yourFUN)
}
This is my function:
f <- function(a, b, ...){
c(as.list(environment()), list(...))
}
If I call f(a = 2) no error will be raised, although b is missing. I would like to get an error in this case:
Error in f(a = 2) : argument "b" is missing, with no default
What piece of dynamic and efficient code I must add such that this error be raised? I was thinking something in line of the following: force(as.symbol(names(formals()))).
Note: In case you wonder why I need this kind of function: It is a way to standardize the kinds of lists. Such a list must have a and b, and possibly other keys. I could play with objects too...
Solutions: See Carl's answer or comments below.
f <- function(a, b, ...){
sapply(ls(environment()), get, envir = environment(), inherits = FALSE)
c(as.list(environment()), list(...))
}
Or
f <- function(a, b, ...){
stopifnot(all(setdiff(names(formals()), '...') %in% names(as.list(match.call()[-1]))))
c(as.list(environment()), list(...))
}
An idea... first check for all arguments that exist in the any function anonymously... meaning regardless of the functions, get the arguments into a list with no preset requirements:
#' A function to grab all arguments of any calling environment.. ie.. a function
#'
#'
#' \code{grab.args}
#'
grab.args <- function() {
envir <- parent.frame()
func <- sys.function(-1)
call <- sys.call(-1)
dots <- match.call(func, call, expand.dots=FALSE)$...
c(as.list(envir), dots)
}
Then, in whatever function you use it for.. store the initial arguments on a list does_have, then find all the arguments that are pre-defined in the environment with should_have, loop through the list to match names and find if any are missing values... if any are... create the error with the names that are missing, if not... do your thing...
#' As an example
#'
f <- function(a, b, ...){
does_have <- grab.args()
should_have <- ls(envir = environment())
check_all <- sapply(should_have, function(i){
!nchar(does_have[[i]])
})
if(any(mapply(isTRUE, check_all))){
need_these <- paste(names(which(mapply(isTRUE,check_all))), collapse = " and ")
cat(sprintf('Values needed for %s', need_these))
}else {
does_have
}
}
Outputs for cause....
> f(mine = "yours", a = 3)
Values needed for b
> f(b = 12)
Values needed for a
> f(hey = "you")
Values needed for a and b
Edit to throw an actual error...
f <- function(a,b,...){
Filter(missing, sapply(ls(environment()), get, environment()))
}
> f(a = 2, wtf = "lol")
Error in FUN(X[[i]], ...) : argument "b" is missing, with no default
Assume I have the following function:
f1 <- function()
{
get.var <- function(v)
{
for(n in 1:sys.nframe())
{
varName <- deparse(substitute(v, env = parent.frame(n)))
if(varName != "v")
{
break
}
}
return(list(name = varName, n = n))
}
f2 <- function(v)
{
print(v)
# get original variable name and environment
obj <- get.var(v)
#below doesn't work as expected - this is where q$a and q$b would be updated
assign(obj$name, v + 1, env = parent.frame(obj$n))
}
f3 <- function(v){ f2(v) }
f4 <- function(v){ f3(v) }
q <- list(a = 2, b = 3)
f4(q$a)
f3(q$b)
}
How can I update the value of q$a and q$b from f2? The situation is that a similar routine is called in some of my code to validate a number of arguments in a nested list. If a value is incorrect the list element needs to be updated to reflect the correct value. It's certainly an ugly way to do it but unfortunately I cannot pass the entire list to each and every validation function.
A somewhat similar question was asked here but the user was passing in a list element instead.
Instead of using assign(obj$name, v + 1, env = parent.frame(obj$n)), I replaced this with eval(parse(text = sprintf("%s <- %d", obj$name, v + 1)), envir = parent.frame(obj$n))
It is horrendously ugly, but it works.
I am facing a strange problem about do.call and curve:
func1 <- function (m, n) {
charac <- paste ("func2 <- function(x)", m, "*x^", n, sep = "")
eval(parse(text = charac))
return(func2)
}
func3 <- function (m, n) {
my.func <- func1 (m, n)
do.call("curve",list(expr = substitute(my.func)))
}
func1 constructs func2 and func3 plots the constructed func2.
But when I run func3, following error would be displayed:
> func3 (3, 6)
Error in curve(expr = function (x) :
'expr' must be a function, or a call or an expression containing 'x'
However, while I run func1 and plot the output manually (without applying func3), func2 would be plotted:
my.func <- func1 (3, 6)
do.call("curve",list(expr = substitute(my.func)))
What happened here leads me to a confusion and I do not know why do.call can not plot func2 inside func3 local environment.
Thank you
You are making this overcomplicated - you don't need to do anything special when creating f2:
f1 <- function (m, n) {
function(x) m * x ^ n
}
f3 <- function (m, n) {
f2 <- f1(m, n)
curve(f2)
}
f3(3, 6)
This could, of course, be made more concise by eliminating f1:
f4 <- function (m, n) {
f2 <- function(x) m * x ^ n
curve(f2)
}
f4(3, 6)
You can find more information about R's scoping rules (which makes this work) at https://github.com/hadley/devtools/wiki/Functions
It is not a problem of do.call, but substitute which evaluate by default in the global environment.
So you need to tell it in which environment substitution must occur. Here obviously in the local envir of func3.
This should work:
do.call("curve",list(expr = substitute(my.func,
env = parent.frame())))
Edit thanks Dwin
As said in the comment substitute env Defaults to the current evaluation environment. So Why the code below works? The answer in the help of substitute
formal argument to a function or explicitly created using
delayedAssign(), the expression slot of the promise replaces the
symbol. If it is an ordinary variable, its value is substituted,
unless env is .GlobalEnv in which case the symbol is left unchanged.
env = parent.frame(n=1) is equivalent to .GlobalEnv, that why the symbol (my.func) is left unchanged. So the correct answer would be :
do.call("curve",list(expr = substitute(my.func,
env = .GlobalEnv)))
To test , I open new R session :
func1 <- function (m, n) {
charac <- paste ("func2 <- function(x)", m, "*x^", n, sep = "")
eval(parse(text = charac))
return(func2)
}
func3 <- function (m, n) {
my.func <- func1 (m, n)
do.call("curve",list(expr = substitute(my.func,env = .GlobalEnv)))
}
Than I call
func3(2,6)
This works:
func3 <- function (m, n) {
my.func <- func1 (m, n); print(str(my.func))
do.call(curve, list(expr=bquote( my.func) ) )
}
You just need to remove line:
my.func <- func1 (m, n)
from func3.
How do I partially bind/apply arguments to a function in R?
This is how far I got, then I realized that this approach doesn't work...
bind <- function(fun,...)
{
argNames <- names(formals(fun))
bindedArgs <- list(...)
bindedNames <- names(bindedArgs)
function(argNames[!argNames %in% bindedArgs])
{
#TODO
}
}
Thanks!
Here's a version of Curry that both preserves lazy evaluation of function argument, but constructs a function that prints moderately nicely:
Curry <- function(FUN, ...) {
args <- match.call(expand.dots = FALSE)$...
args$... <- as.name("...")
env <- new.env(parent = parent.frame())
if (is.name(FUN)) {
fname <- FUN
} else if (is.character(FUN)) {
fname <- as.name(FUN)
} else if (is.function(FUN)){
fname <- as.name("FUN")
env$FUN <- FUN
} else {
stop("FUN not function or name of function")
}
curry_call <- as.call(c(list(fname), args))
f <- eval(call("function", as.pairlist(alist(... = )), curry_call))
environment(f) <- env
f
}
It basically works by generating an anonymous function in exactly the same way you would if you were constructing the partial binding yourself.
Actually, this seems to work as a work around
bind <- function(fun,...)
{
boundArgs <- list(...)
formals(fun)[names(boundArgs)] <- boundArgs
fun
}
However, ideally I want the bound arguments to disappear completely from the new function so that calls to the new function can happen with name specification, e.g. with add <- function(a,b) a+b I would like (bind(add,a=2))(1) to return 3.
Have you tried looking at roxygen's Curry function?
> library(roxygen)
> Curry
function (FUN, ...)
{
.orig = list(...)
function(...) do.call(FUN, c(.orig, list(...)))
}
<environment: namespace:roxygen>
Example usage:
> aplusb <- function(a,b) {
+ a + 2*b
+ }
> oneplusb <- Curry(aplusb,1)
> oneplusb(2)
[1] 5
Edit:
Curry is concisely defined to accept named or unnamed arguments, but partial application of fun to arguments by way of formal() assignment requires more sophisticated matching to emulate the same functionality. For instance:
> bind <- function(fun,...)
+ {
+ argNames <- names(formals(fun))
+ boundArgs <- list(...)
+ boundNames <- names(boundArgs)
+ if(is.null(boundNames)) {
+ formals(fun)[1:length(boundArgs)] <- boundArgs
+ } else {
+ formals(fun)[match(names(boundArgs),argNames)] <- boundArgs
+ }
+ fun
+ }
> oneplusb <- bind(aplusb,1)
> oneplusb(2)
Error in 2 * b : 'b' is missing
Because the first argument in this function is still a, you need to specify which argument 2 is intended for (b=), or pass it as the second argument.
> oneplusb
function (a = 1, b)
{
a + 2 * b
}
> oneplusb(b=2) ## or oneplusb(,2)
[1] 5