[R] Passing argument in nested function calls

From: Aarti Dahiya <aarti_dahiya_at_hotmail.com>
Date: Fri 30 Jun 2006 - 06:59:00 EST


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@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 Received on Fri Jun 30 07:10:05 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 30 Jun 2006 - 10:13:46 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.