Viechtbauer Wolfgang (STAT)
2008-Feb-29 09:02 UTC
[R] Optional data argument for a function
Dear All, I am working on a function that has an optional data argument, just like lm(). If the user sets the data argument equal to some dataframe, then the function should look inside the dataframe for the variables given to other arguments, otherwise the variables should be accessible from what I believe is called the parent frame from which the function was called. I looked at the source code for lm() (and similar functions) and the documentation of ?eval, but I just can't quite figure out how to make this work. So, for a simple example, suppose I want to do this with the following function: dosomething <- function(x, data) { temp <- x^2 + 5 return(temp) } How does the function need to be changed, so that dosomething(somevariable, data=somedataframe) will find somevariable within the dataframe somedataframe and if dosomething(somevariable) is called, the function should find somevariable in the parent frame (or anywhere else in the search path)? Thanks for any help! -- Wolfgang Viechtbauer ?Department of Methodology and Statistics ?University of Maastricht, The Netherlands ?http://www.wvbauer.com/
One option is: dosomething <- function(x, data, env=parent.frame()) { if(!missing(data)) x <- data[,deparse(substitute(x))] temp <- x^2 + 5 return(temp) } On 29/02/2008, Viechtbauer Wolfgang (STAT) <Wolfgang.Viechtbauer at stat.unimaas.nl> wrote:> Dear All, > > I am working on a function that has an optional data argument, just like lm(). If the user sets the data argument equal to some dataframe, then the function should look inside the dataframe for the variables given to other arguments, otherwise the variables should be accessible from what I believe is called the parent frame from which the function was called. I looked at the source code for lm() (and similar functions) and the documentation of ?eval, but I just can't quite figure out how to make this work. > > So, for a simple example, suppose I want to do this with the following function: > > dosomething <- function(x, data) { > temp <- x^2 + 5 > return(temp) > } > > How does the function need to be changed, so that dosomething(somevariable, data=somedataframe) will find somevariable within the dataframe somedataframe and if dosomething(somevariable) is called, the function should find somevariable in the parent frame (or anywhere else in the search path)? > > Thanks for any help! > > -- > Wolfgang Viechtbauer > Department of Methodology and Statistics > University of Maastricht, The Netherlands > http://www.wvbauer.com/ > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Maybe Matching Threads
- Broken Links on http://www.r-project.org
- Writing methods for existing generic function
- FOSS licence with BuildVignettes: false
- Error message when calling t.test() and aov() with a factor variables
- Compiling R-devel - missing some expected features (zlib, bzlib, lzma, PCRE)