[R] Passing argument in nested function calls

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 30 00:24:29 CEST 2006


getArgs should be:

getArgs <- function(...) extractArgs(...)

# test
extractArgs(x = 3)
getArgs(x = 3)


On 6/29/06, Aarti Dahiya <aarti_dahiya at hotmail.com> wrote:
> Please, do not ask why you would do this.  I have simplified my complicated
> code to just the basic problem occuring to be able to make it easier to
> understand the actual problem.
>
> I have two function getArgs() and extractArgs() - code and test results
> provided below.
> I test them using getArgs(id = 's1002') from the command line.
>
> getArgs() prints the name of the argument as id and the actual argument as
> s1002, which is correct.
>
> But, when I call extractArgs() from within getArgs() passing value of passed
> (stored in passed, passed is created from paste(names(args),"=",
> sQuote(args))); extractArgs() treats the value of passed as a string "id =
> 's1002'" and treats the whole thing as one argument.  So, it prints
> args[[1]] as the whole thing id = 's1002'", but prints blank for
> names(args[[1]]), obviously beacuse it cannot find a name for the argument.
>
> If I call extractArgs(id = 's1002') from the command line, it works just
> fine, prints the name of the argument as id and the actual argument as
> s1002, which is correct.
>
> How do I fix the problem so that when extractArgs() is called within
> getArgs(), it behaves the same way as getArgs() and extracts the argument
> and argument name?  Can we unstring passed or change it into another mode?
>
> I'll appreciate any help or pointers.  Thanks.  Aarti
>
> getArgs <- function(...)
> {
>    cat("\nEntering getArgs()\n\n")
>
>    args <- list(...)
>
>    cat("In getArgs(), names(args[1]) is ")
>    cat(names(args[1]))
>    cat(" and args[[1]] is ")
>    cat(args[[1]])
>    cat("\n")
>
>    passed <- paste(names(args),"=", sQuote(args))
>
>    extractArgs(passed)
>
>    cat("Exiting getArgs()\n\n")
> }
>
> extractArgs <- function(...)
> {
>    cat("\nEntering extractArgs()\n\n")
>
>    args <- list(...)
>
>    cat("In extractArgs(), names(args[1]) is ")
>    cat(names(args[1]))
>    cat(" and args[[1]] is ")
>    cat(args[[1]])
>    cat("\n")
>
>    cat("\nExiting extractArgs()\n\n")
> }
>
> Test:
>
> >getArgs(id = 's1002')
>
> Entering getArgs()
>
> In getArgs(), names(args[1]) is id and args[[1]] is s1002
>
> Entering extractArgs()
>
> In extractArgs(), names(args[1]) is  and args[[1]] is id = 's1002'
>
> Exiting extractArgs()
>
> Exiting getArgs()
>
> >extractArgs(id = 's1002')
>
> Entering extractArgs()
>
> In extractArgs(), names(args[1]) is id and args[[1]] is s1002
>
> Exiting extractArgs()
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



More information about the R-help mailing list