r calling variables from list - r

I was hoping to find some help with a current issue. I've written a function (F1), which takes as input the name of another function (F2) and some other input. The input for F2 might differ, what I'm trying to do, is to use a list as input to F1 which than will use the list as input for F2.
F1<-function(input.list,F2){out=F2(input.list)}
However, I would like to avoid having to extract all variables in input.list using something like a=input.list[[1]], I do not want to name all possible input variables for all possible F2 in F1, rather I would like to have the variable names in input.list to be used directly as input for F2.

I'm not sure I completely understand what you're trying to do, but below are two examples that could point you in the right direction. The wrapper functions here supply arguments to FUN(in your case F2) but also take their own arguments (unique_F1_factor).
Firstly, if you want to work with lists of arguments supplied to your wrapper F1, then I suggest using do.call in the body of the function, like this:
# If you want to work with lists ----
F1_list = function(input_list,unique_F1_factor,FUN) {
out = do.call(FUN, input_list)
# Do stuff in F1, e.g.:
out=unique_F1_factor*out
return(out)
}
Alternatively, you could just avoid a list altogether:
# Wrapper function ----
F1 = function(...,unique_F1_factor,FUN) {
out=FUN(...)
# Do stuff in F1, e.g.:
out=unique_F1_factor*out
return(out)
}
*Note that if using a random number generator such as rnorm, the results will differ for do.call(rnorm, args) and rnorm(args) even if you specify set.seed.
Hope this helps!
Edit
Just to clarify how you would supply arguments in each case. For F1_list you would do:
# Example with list:
F1_list(
list(sd=1,mean=3,n=10),
unique_F1_factor = 10,
FUN = rnorm
)
If instead you want to avoid the list, do:
# Example w/o list ----
F1(
sd=1,
mean=3,
n=10,
unique_F1_factor = 10,
FUN = rnorm
)

Related

Provide multiple function arguments by one variable

When working with packages like openxlsx, I often find myself writing repetetive code such as defining the wb and sheet arguments with the same values.
To respect the DRY principle, I would like to define one variable that contains multiple arguments. Then, when I call a function, I should be able to provide said variable to define multiple arguments.
Example:
foo <- list(a=1,b=2,c=3)
bar <- function(a,b,c,d) {
return(a+b+c+d)
}
bar(foo, d=4) # should return 10
How should the foo() function be defined to achieve this?
Apparently you are just looking for do.call, which allows you to create and evaluate a call from a function and a list of arguments.
do.call(bar, c(foo, d = 4))
#[1] 10
How should the foo() function be defined to achieve this?
You've got it slightly backwards. Rather than trying to wrangle the output of foo into something that bar can accept, write foo so that it takes input in a form that is convenient to you. That is, create a wrapper function that provides all the boilerplate arguments that bar requires, without you having to specify them manually.
Example:
bar <- function(a, b, c, d) {
return(a+b+c+d)
}
call_bar <- function(d=4) {
bar(1, 2, 3, d)
}
call_bar(42) # shorter than writing bar(1, 2, 3, 42)
I discovered a solution using rlang::exec.
First, we must have a function to structure the dots:
getDots <- function(...) {
out <- sapply(as.list(match.call())[-1], function(x) eval(parse(text=deparse(x))))
return(out)
}
Then we must have a function that executes our chosen function, feeding in our static parameters as a list (a, b, and c), in addition to d.
execute <- function(FUN, ...) {
dots <-
getDots(...) %>%
rlang::flatten()
out <- rlang::exec(FUN, !!!dots)
return(out)
}
Then calling execute(bar, abc, d=4) returns 10, as it should do.
Alternatively, we can write bar %>% execute(abc, d=4).
Let me give you an example!
How to get two or more return values ​​from a function
Method 1: Set global variables, so that if you change global variables in formal parameters, it will also be effective in actual parameters. So you can change the value of multiple global variables in the formal parameter, then in the actual parameter is equivalent to returning multiple values.
Method 2: If you use the array name as a formal parameter, then you change the contents of the array, such as sorting, or perform addition and subtraction operations, and it is still valid when returning to the actual parameter. This will also return a set of values.
Method 3: Pointer variables can be used. This principle is the same as Method 2, because the array name itself is the address of the first element of the array. Not much to say.
Method 4: If you have learned C++, you can quote parameters
You can try these four methods here, I just think the problem is a bit similar, so I provided it to you, I hope it will help you!

Edit ... parameter in R

I'm having troubles understanding how the ...parameter works in R, I can't find a complete documentation, is it some kind of editable object?
I'm writing an R package and I would like to write a function that uses the dots, to pass them to another function (plot), but I want to pass some parameters to the inner function only if they are not in .... I know that using list(...) I can check if they have been specified, but I don't know if I can add them to ... otherwise. What's the best way to make this work?
I tried to pass an edited pars= list(...) to the inner function plot(...= pars), but it didn't work. Actually in the end I found a solution, which is updating the list pars trough modifyList and then using do.call to pass them to the inner function, but this feels a bit intricate to me, there is any simpler solution?
You can just pass ... along as so:
# weird way to construct a linear function
f0 <- function (x, b=1) x+b
f1 <- function (x, a=2, ...){
# INITIAL WRONG ANSWER stopifnot(exists("b")) # see comments
if("b" %in% names(list(...))){
f0(a*x, ...)
}else{
f0(a*x, b=4, ...) # Only makes sense if b=4 is a default that has meaning
# in f1 but not outside of f1 ; or if you cannot change
# the definition of f0 (imported functions). Otherwise,
# you'd better change the default in f0.
}
}
f1(10)
f1(10, b=3)
Arthur and Janhoo made some good suggestions, but in the end I see that the straightforward way I hoped to find doesn't exist. So in the end the best solution to me is the one I sketched in the question:
pars_user= list(...)
pars_default= list(a= 1, b= 2)
pars_fixed= list(c= 3, d= 4)
pars= modifyList(pars_default, pars_user)
pars= modifyList(pars, pars_fixed)
do.call(function, pars)

How to retrieve formals of a primitive function?

For the moment, at least, this is an exercise in learning for me, so the actual functions or their complexity is not the issue. Suppose I write a function whose argument list includes some input variables and a function name, passed as a string. This function then calculates some variables internally and "decides" how to feed them to the function name I've passed in.
For nonprimitive functions, I can do (for this example, assume non of my funcname functions have any arguments other than at most (x,y,z). If they did, I'd have to write some code to search for matching names(formals(get(funcname))) so as not to delete the other arguments):
foo <- function (a,b,funcname) {
x <- 2*a
y <- a+3*b
z <- -b
formals(get(funcname)) <- list(x=x, y=y, z=z)
bar <- get(funcname)()
return(bar)
}
And the nice thing is, even if the function funcname will execute without error even if it doesn't use x, y or z (so long as there are no other args that don't have defaults) .
The problem with "primitive" functions is I don't know any way to find or modify their formals. Other than writing a wrapper, e.g. foosin <-function(x) sin(x), is there a way to set up my foo function to work with both primitive and nonprimitive function names as input arguments?
formals(args(FUN)) can be used to get the formals of a primitive function.
You could add an if statement to your existing function.
> formals(sum)
# NULL
> foo2 <- function(x) {
if(is.primitive(x)) formals(args(x)) else formals(x)
## formals(if(is.primitive(x)) args(x) else x) is another option
}
> foo2(sum)
# $...
#
#
# $na.rm
# [1] FALSE
#
> foo2(with)
# $data
#
#
# $expr
#
#
# $...
Building on Richard S' response, I ended up doing the following. Posted just in case anyone else ever tries do things as weird as I do.
EDIT: I think more type-checking needs to be done. It's possible that coleqn could be
the name of an object, in which case get(coleqn) will return some data. Probably I need
to add a if(is.function(rab)) right after the if(!is.null(rab)). (Of course, given that I wrote the function for my own needs, if I was stupid enough to pass an object, I deserve what I get :-) ).
# "coleqn" is the input argument, which is a string that could be either a function
# name or an expression.
rab<-tryCatch(get(coleqn),error=function(x) {} )
#oops, rab can easily be neither NULL nor a closure. Damn.
if(!is.null(rab)) {
# I believe this means it must be a function
# thanks to Richard Scriven of SO for this fix to handle primitives
# we are not allowed to redefine primitive's formals.
qq <- list(x=x,y=y,z=z)
# matchup the actual formals names
# by building a list of valid arguments to pass to do.call
argk<-NULL
argnames<-names(formals(args(coleqn)))
for(j in 1:length(argnames) )
argk[j]<-which(names(qq)==argnames[1] )
arglist<-list()
for(j in 1:length(qq) )
if(!is.na(argk[j])) arglist[[names(qq)[j]]]<-qq[[j]]
colvar<- do.call(coleqn,arglist)
} else {
# the input is just an expression (string), not a function
colvar <- eval(parse(text=coleqn))
}
The result is an object generated either by the expression or the function just created, using variables internal to the main function (which is not shown in this snippet)

Using a single object to pass multiple arguments to a function?

Let's say I have a function that can't be altered, like:
add.these <- function(x,y,z) {
x + y + z
}
And I want to pass all three arguments as a single object. How do I pass this single object through to the function so it evaluates them as separate inputs?
The ideal result would be something like args <- list(x,y,z), and add.these(args) returns the result.
It's a simple question that's been bothering me but I've stupidly been unable to figure it out. The actual use case is that the function has variable numbers of arguments it requires depending on the desired outputs, and I want to pass these through as a list or something.
Are you looking for do.call?
> args=list(1,2,3)
> do.call(add.these,args)
[1] 6

Initializing function arguments in the global environment R

I'm looking for a simple function to speed up my ability to write and debug R functions. Consider the following blocks of code:
# Part A:
myfun = function(a, b = 5, out = "hello"){
if(a>b) print(out)
return(a-b)
}
# Part B:
b = 5
out = "hello"
# Part C:
do.args = function(f){
#intialize the arguments of myfun in the parent environment
???
}
The function myfun is a trivial example of a bigger problem: I often have a complicated function with many arguments. To efficiently write and debug such a function, I find it useful to initialize the arguments of the function, and 'step through' the function line-by-line. Initializing the arguments, as in Part B above, is somewhat a hassle, when there are lots of arguments, and I would prefer to have a function as in Part C, which takes only the string myfun as it arguments and produces the same effect as running Part B in the current environment.
This only works for functions where all the arguments are defined. In other words, myfun has to have a value for a defined in the function.
some.func <- function(infunc){
forms <- formals(infunc)
for(i in 1:length(forms)){
assign(names(forms)[i],forms[[i]],envir=globalenv())
}
}
You could add a qualifier to deal with the variables that do not have default values, but it may not work in all examples. In this example I defined all missing variables to NA - and you could change the definition. Note: assigning the missing variables to NULL will not work.
some.func <- function(infunc){
forms <- formals(infunc)
for(i in 1:length(forms)){
if(class(forms[[i]])=="name") forms[[i]] <- NA
assign(names(forms)[i],forms[[i]],envir=globalenv())
}
}
You could also adjust the function and simply skip assigning the missing variables by using next after the if statement rather than defining the missing variables to NA, or some other value. The next example:
some.func <- function(infunc){
forms <- formals(infunc)
for(i in 1:length(forms)){
if(class(forms[[i]])=="name") next
assign(names(forms)[i],forms[[i]],envir=globalenv())
}
}
If you want to reassign formal arguments there is a formals<- function. By default the environment in which it does the assignment is the same as that in which it was created, bu that could be changed. See ?formals and ?alist
formals(myfun) <- alist(a=,b=4, out="not awake")
myfun
#------------------
function (a, b = 4, out = "not awake")
{
if (a > b)
print(out)
return(a - b)
You need to use alist with the argument of the form a= if you want the default to be missing.
}

Resources