Re: [R] Checking for and adding "..." arguments to a function...
Hello, Use ?formals. > formals(myfunction) $a $b $c $... Hope this helps, Rui Barradas Em 17-02-2014 21:22, Jonathan Greenberg escreveu: R-helpers: I'm guessing this is an easy one for some of you, but I'm a bit stumped. Given some arbitrary function (doesn't matter what it does): myfunction <- function(a,b,c) { return(a+b+c) } I want to test this function for the presence of the ellipses ("...") and, if they are missing, create a new function that has them: myfunction <- function(a,b,c,...) { return(a+b+c) } So, 1) how do I test for whether a function has an ellipses argument and, 2) how do I "append" the ellipses to the argument list if they do exist? Note that the test/modification should be done without invoking the function, e.g. I'm not asking how to test for this WITHIN the function, I'm asking how to test "myfunction" directly as an R object. Thanks! --j __ R-help@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.
Re: [R] Checking for and adding "..." arguments to a function...
Here are two ways: ## construct formals adding ... f <- c(formals(myfunction), unlist(alist(... = ))) ## replace the formals, excluding the extra ... if it previously existed formals(myfunction) <- f[!duplicated(names(f))] ## 2nd way, searching for ... and doing the replacement only if it is not found if(!any(grepl("...", names(formals(myfunction) { formals(myfunction) <- c(formals(myfunction), unlist(alist(... = ))) } HTH, Ista On Mon, Feb 17, 2014 at 4:22 PM, Jonathan Greenberg wrote: > R-helpers: > > I'm guessing this is an easy one for some of you, but I'm a bit > stumped. Given some arbitrary function (doesn't matter what it does): > > myfunction <- function(a,b,c) > { > return(a+b+c) > } > > I want to test this function for the presence of the ellipses ("...") > and, if they are missing, create a new function that has them: > > myfunction <- function(a,b,c,...) > { > return(a+b+c) > } > > So, 1) how do I test for whether a function has an ellipses argument > and, 2) how do I "append" the ellipses to the argument list if they do > exist? > > Note that the test/modification should be done without invoking the > function, e.g. I'm not asking how to test for this WITHIN the > function, I'm asking how to test "myfunction" directly as an R object. > > Thanks! > > --j > > > -- > Jonathan A. Greenberg, PhD > Assistant Professor > Global Environmental Analysis and Remote Sensing (GEARS) Laboratory > Department of Geography and Geographic Information Science > University of Illinois at Urbana-Champaign > 259 Computing Applications Building, MC-150 > 605 East Springfield Avenue > Champaign, IL 61820-6371 > Phone: 217-300-1924 > http://www.geog.illinois.edu/~jgrn/ > AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > __ > R-help@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. __ R-help@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.
Re: [R] Checking for and adding "..." arguments to a function...
On Feb 17, 2014, at 1:22 PM, Jonathan Greenberg wrote: > R-helpers: > > I'm guessing this is an easy one for some of you, but I'm a bit > stumped. Given some arbitrary function (doesn't matter what it does): > > myfunction <- function(a,b,c) > { > return(a+b+c) > } > > I want to test this function for the presence of the ellipses ("...") > and, if they are missing, create a new function that has them: > > myfunction <- function(a,b,c,...) > { > return(a+b+c) > } > > So, 1) how do I test for whether a function has an ellipses argument > and, One way: > myfunction <- function(a,b,c,...) + { + return(a+b+c) + } > formals(myfunction) $a $b $c $... > length(formals(myfunction)$...) [1] 1 > myfunction <- function(a,b,c) + { + return(a+b+c) + } > length(formals(myfunction)$...) [1] 0 > 2) how do I "append" the ellipses to the argument list if they do > exist? Not exactly sure but the usual way to capture the objects passed in the three dots mechanism is with list(...) > myfunction <- function(a,b,c,...) + { others <- list(...); for(i in others) {cat(i);cat("\n")} + return(a+b+c) + } > myfunction(a=1,b=2,c=3,other1 =4, other2=5) 4 5 [1] 6 > > Note that the test/modification should be done without invoking the > function, e.g. I'm not asking how to test for this WITHIN the > function, I'm asking how to test "myfunction" directly as an R object. > > Thanks! > > --j > > > -- > Jonathan A. Greenberg, PhD > Assistant Professor > Global Environmental Analysis and Remote Sensing (GEARS) Laboratory > Department of Geography and Geographic Information Science > University of Illinois at Urbana-Champaign > 259 Computing Applications Building, MC-150 > 605 East Springfield Avenue > Champaign, IL 61820-6371 > Phone: 217-300-1924 > http://www.geog.illinois.edu/~jgrn/ > AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 > > __ > R-help@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. David Winsemius Alameda, CA, USA __ R-help@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.
[R] Checking for and adding "..." arguments to a function...
R-helpers: I'm guessing this is an easy one for some of you, but I'm a bit stumped. Given some arbitrary function (doesn't matter what it does): myfunction <- function(a,b,c) { return(a+b+c) } I want to test this function for the presence of the ellipses ("...") and, if they are missing, create a new function that has them: myfunction <- function(a,b,c,...) { return(a+b+c) } So, 1) how do I test for whether a function has an ellipses argument and, 2) how do I "append" the ellipses to the argument list if they do exist? Note that the test/modification should be done without invoking the function, e.g. I'm not asking how to test for this WITHIN the function, I'm asking how to test "myfunction" directly as an R object. Thanks! --j -- Jonathan A. Greenberg, PhD Assistant Professor Global Environmental Analysis and Remote Sensing (GEARS) Laboratory Department of Geography and Geographic Information Science University of Illinois at Urbana-Champaign 259 Computing Applications Building, MC-150 605 East Springfield Avenue Champaign, IL 61820-6371 Phone: 217-300-1924 http://www.geog.illinois.edu/~jgrn/ AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 __ R-help@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.