1

this is a function which can't be changed:

foo <- function(y = 0, x = "no") {
  print(x)
}

I never know how the argument to pass will be called. Here it is x but sometimes it is a and sometimes b and so on... I get the information in variable argName. So now argName = "x" variable arg which contains the argument to pass to the foo function:

arg <- paste0(argName, "=\"yes\"")

now I want to pass arg to the arguments of foo to evaluate the same as:

foo(x="yes")

so how to do this? this was my tries:

foo(arg)
foo(`arg`)
foo('arg')
foo("arg")
foo(noquote(arg))
foo(noquote(`arg`))
foo(noquote('arg'))
foo(noquote("arg"))
foo(eval(arg))
foo(eval(noquote(arg))
stakowerflol
  • 979
  • 1
  • 11
  • 20
  • 3
    Why can't it be `arg = list(x = "yes"); do.call(foo, arg)` as you would usually do? – Roland Oct 18 '18 at 08:47
  • because I never know the name of the argument. Sometimes it is called `x` and sometimes `a`, sometimes `b`... I get in in another variable... Edited my example. – stakowerflol Oct 18 '18 at 08:53
  • You should simply fix the creation of `arg` then: `argName <- "x"; argValue <- "yes"; setNames(list(argValue), argName)`Or does this come from outside R (usually still a bad design)? – Roland Oct 18 '18 at 09:02

0 Answers0