Dear list, I am wondering whether there is an (easy) way to access all arguments and their values passed to a function inside this function and (for example) store them in a list object? I could imagine using ls() inside this function and then looping through all names and assigning list entries with the values of the respective objects but I could imagine that there is already something ready made in R to achieve this. Does anybody have an idea? Thanks Jannis
Duncan Murdoch
2011-Sep-01 15:27 UTC
[R] get arguments passed to function inside a function
On 01/09/2011 11:14 AM, Jannis wrote:> Dear list, > > > I am wondering whether there is an (easy) way to access all arguments and their values passed to a function inside this function and (for example) store them in a list object? > > I could imagine using ls() inside this function and then looping through all names and assigning list entries with the values of the respective objects but I could imagine that there is already something ready made in R to achieve this. > > Does anybody have an idea?as.list(environment()) should do it. Duncan Murdoch
The sys.call or match.call functions may be what you are looking for:> tmpfun <- function(x,y,z,...) {+ as.list( sys.call() ) + }> > tmpfun( x=5, w=3, 1:10 )[[1]] tmpfun $x [1] 5 $w [1] 3 [[4]] 1:10> tmpfun2 <- function(x,y,z,...) {+ as.list( match.call() ) + }> tmpfun2( x=5, w=3, 1:10 )[[1]] tmpfun2 $x [1] 5 $y 1:10 $w [1] 3>-- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Jannis > Sent: Thursday, September 01, 2011 9:14 AM > To: r-help at r-project.org > Subject: [R] get arguments passed to function inside a function > > Dear list, > > > I am wondering whether there is an (easy) way to access all arguments > and their values passed to a function inside this function and (for > example) store them in a list object? > > I could imagine using ls() inside this function and then looping > through all names and assigning list entries with the values of the > respective objects but I could imagine that there is already something > ready made in R to achieve this. > > Does anybody have an idea? > > Thanks > Jannis > > > ______________________________________________ > 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.