[Rd] Question on Code snippet semantics

Marc Schwartz marc_schwartz at me.com
Mon Jul 21 17:20:42 CEST 2014


On Jul 21, 2014, at 10:07 AM, Mick Jordan <mick.jordan at oracle.com> wrote:

> I came across this code in library.R
> 
> package <- as.character(substitute(package))
> 
> where package is the first argument to the "library" function.
> 
> I've been racking my brains to understand why this is not just an elaborate (and ineffcient) way to write:
> 
> package <- "package"
> 
> E.g.
> 
> > package <- as.character(substitute(package))
> > package
> [1] "package"
> >
> 
> Thanks
> Mick Jordan


Frequently used in a function body, where the function author wants the argument to be passed as an object name, rather than a character vector, or perhaps both, as is the case with library() and require().

For example:

test <- function(x) {as.character(substitute(x))}

# Quoted, passing "MyPackage" as a character vector
> test("MyPackage")
[1] "MyPackage"


# Not quoted, passing the object MyPackage
> test(MyPackage)
[1] "MyPackage"


In both cases, the argument passed as 'x' can then be used within the function as a character vector, rather than as the object itself.

Regards,

Marc Schwartz



More information about the R-devel mailing list