Skip to content

data() misbehaving inside a function

14 messages · David James, Martin Maechler, Kurt Hornik +5 more

#
Calls of the form data(package = pkg) inside a function 
incorrectly fail ("pkg" is a local variable).  For instance,

   foo <- function(pkg) data(package = pkg)
   foo("base")
   Error in .find.package(package, lib.loc, verbose = verbose) :
           none of the packages were found

   ## workaround -- force argument "package" to be an expression
   ## (not a name)

   foo <- function(pkg) data(package = as.character(pkg))
   all.equal(data(package = "base"), foo("base"))
   [1] TRUE

The cause is the following piece of code inside data()

    > print(data)
    .... 
    if (!missing(package))
               if (is.name(y <- substitute(package)))
                              package <- as.character(y)
    ...

namely, when data() is invoked from within a function, e.g.,
data(package = pkg), the argument "package" inside data() is indeed
a name, but not one that should be coerced to character as when one
types data(base) from the R prompt, but rather simply a variable
in the calling function; in the example above, the arg "package"
to data() is effectively computed to be "pkg", which (luckily)
does not correspond to any attached package.

version
       _
platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status   Patched
major    1
minor    8.0
year     2003
month    10
day      13
language R
#
David James <dj@research.bell-labs.com> writes:
This is pretty much unavoidable if you want a function to accept
unquoted names. It's not in principle different from

women <- "airquality"
data(women)

not being equivalent to data("airquality"). Some functions (library(),
require(), demo()) have a character.only argument to prevent it, and I
suppose we should consider putting it on help() and data() as well.
Meanwhile, my preferred device is

foo <- function(pkg) eval(substitute(data(package)))

        -p
#
Yes, but this is not to do with being inside a function:

pkg <- "base"
data(package=pkg)

ilustrates it, and library etc behave in the same way (except
library can make use of character.only = TRUE).

You can use substitute, too, as in

foo <- function(pkg) eval(substitute(data(package = pkg), list(pkg=pkg)))
On Thu, 16 Oct 2003, David James wrote:

            

  
    
#
Peter Dalgaard <p.dalgaard@biostat.ku.dk> writes:
^^^^^^^
Doh. "pkg", of course.

        -p
#
Or get rid of non-standard evaluation and educate users to use quoted
strings where strings should be used.

-k
#
>> David James <dj@research.bell-labs.com> writes:
    >>> Calls of the form data(package = pkg) inside a function 
    >>> incorrectly fail ("pkg" is a local variable).  For instance,
    >>> 
    >>> foo <- function(pkg) data(package = pkg)
    >>> foo("base")
    >>> Error in .find.package(package, lib.loc, verbose = verbose) :
    >>> none of the packages were found

    >> This is pretty much unavoidable if you want a function to accept
    >> unquoted names. It's not in principle different from

    >> women <- "airquality"
    >> data(women)

    >> not being equivalent to data("airquality"). Some functions (library(),
    >> require(), demo()) have a character.only argument to prevent it, and I
    >> suppose we should consider putting it on help() and data() as well.

    KH> Or get rid of non-standard evaluation and educate users to use quoted
    KH> strings where strings should be used.

and infuriate those who know and used the S language for more
than 15 years, where  help(help)  has always worked? 

Definitely not worth the pain (I *know* I'd hear ... comments from them!)!
I'd go for adding `character.only'.

Martin
#
KH> Or get rid of non-standard evaluation and educate users to use quoted
KH> strings where strings should be used.
I would think that these users either click themselves to happiness, or
use ? because it is shorter.
Actually, I do think it is worth the pain.  The way I understand it, we
have a strategic decision to gradually eliminate non-standard eval.

-k
#
Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> writes:

  <Martin wrote re dropping unquoted function args from packages/data>
I might be one of them.  You'll hear my screams for weeks...
and for better or worse, I actually agree with Kurt.  Most languages
don't let you do non-standard eval regularly, and in R/S, it is used
"regularly".  I don't think that the added complexity is worth the
convenience of saving 2 keystrokes. 

best,
-tony
#
Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> writes:
Ummm, ?help is another non-standard eval. "?" is an operator,
operators work on objects not (normally) their names....
#
[Fortunately, you're mostly in Seattle ...]
:-)

-k
#
KH> Or get rid of non-standard evaluation and educate users to use quoted
KH> strings where strings should be used.
Right.  I had meant to say that if we need non-standard eval, let's have
it for ?, where we need it for things like method ? expression anyways,
but let's eliminate it from help().

-k
#
At 05:50 PM 17/10/2003, A.J. Rossini wrote:
For what it's worth, I'd add my voice to Kurt and Tony. Even though I've 
been an S user for 17 years, I still find it weird that

poisson(link=log)

works but

a <- "log"; poisson(link=a)

doesn't given that the help doc says

poisson(link="log")

Gordon
#
On Fri, 17 Oct 2003, Kurt Hornik wrote:

            
help(help) might be unavoidable, but there is nothing incompatible with S
about requiring the argument to data() to be quoted, and that was what the
original question was about.


	-thomas
#
On Fri, 17 Oct 2003, Thomas Lumley wrote:

            
It was about the *package* argument to data, and I see no reason at all
why that should be accepted as a name.  We don't allow lib.loc in
library() to be a name, and that seems the closest equivalent.

I think that for back-conmpatibility we do need to allow

?topic
help(topic)
data(dataset)

but could require the package argument to be quoted (note that data has a
list= argument, and help could be given a character.only one).