Tal Galili
2011-Jan-30 16:38 UTC
[R] Extracting a (*specific*) variable name from a list of function arguments (not "...")
Hello all, I would like to extract the variable name passed into a specific argument of a function. Using match.call() will give me a list of arguments and what was passed into them, but I couldn't find how to get a particular argument out of it (at least without using some string manipulation). Here is an example (in it, my objective is to know what is the variable name assigned to the argument "b"): foo <- function(a, b, c) { print(match.call()) } x = 1 ; y = 1 foo(x, y) # here the answer should be "y" foo(c= x, y) # here the answer should be "" (or NULL/NA) Any suggestion on how to do this? BTW, the purpose of this is to be able to avoid the error produced by, for example, this: yy <- data.frame(y = 1:10, x = 1:10) plot(y, data = yy) # error plot(y~x, data = yy) # no error... Thanks, Tal ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- [[alternative HTML version deleted]]
Uwe Ligges
2011-Jan-30 16:54 UTC
[R] Extracting a (*specific*) variable name from a list of function arguments (not "...")
On 30.01.2011 17:38, Tal Galili wrote:> Hello all, > > I would like to extract the variable name passed into a specific argument of > a function. > Using match.call() will give me a list of arguments and what was passed into > them, but I couldn't find how to get a particular argument out of it (at > least without using some string manipulation). > > Here is an example (in it, my objective is to know what is the variable name > assigned to the argument "b"): > > foo<- function(a, b, c) > { > print(match.call())Replace by print(substitute(b)) Uwe Ligges> } > x = 1 ; y = 1 > foo(x, y) # here the answer should be "y" > foo(c= x, y) # here the answer should be "" (or NULL/NA) > > > Any suggestion on how to do this? > > > BTW, the purpose of this is to be able to avoid the error produced by, for > example, this: > > yy<- data.frame(y = 1:10, x = 1:10) > plot(y, data = yy) # error > plot(y~x, data = yy) # no error... > > > > > Thanks, > Tal > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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.
Marc Schwartz
2011-Jan-30 17:01 UTC
[R] Extracting a (*specific*) variable name from a list of function arguments (not "...")
On Jan 30, 2011, at 10:38 AM, Tal Galili wrote:> Hello all, > > I would like to extract the variable name passed into a specific argument of > a function. > Using match.call() will give me a list of arguments and what was passed into > them, but I couldn't find how to get a particular argument out of it (at > least without using some string manipulation). > > Here is an example (in it, my objective is to know what is the variable name > assigned to the argument "b"): > > foo <- function(a, b, c) > { > print(match.call()) > } > x = 1 ; y = 1 > foo(x, y) # here the answer should be "y" > foo(c= x, y) # here the answer should be "" (or NULL/NA) > > > Any suggestion on how to do this? > > > BTW, the purpose of this is to be able to avoid the error produced by, for > example, this: > > yy <- data.frame(y = 1:10, x = 1:10) > plot(y, data = yy) # error > plot(y~x, data = yy) # no error...Tal, Try this: x <- 1 y <- 1 foo <- function(a, b, c) {as.list(match.call())}> foo(x, y)[[1]] foo $a x $b y> foo(x, y)$by> foo(c = x, y)$bNULL If you really only want 'b' and not be able to get the other arguments, you can use: foo2 <- function(a, b, c) {as.list(match.call())$b}> foo2(x, y)y> foo2(c = x, y)NULL In your plot example above, you are first dispatching plot.default(), which of course has no 'data' argument. In the second case, you are dispatching plot.formula() which does have a 'data' argument, as do most, if not all, R functions that take a formula as an argument. HTH, Marc Schwartz
Apparently Analagous Threads
- Extracting the terms from an rpart object
- Importing tRNA data into R ?
- environment question: changing variables from a formula through model.frame?
- Is there a way to edit a specific line in a function (e.g: doing function->text->edit->function) ?
- How to: highlight R syntax on webpages ?