From p.dalgaard at biostat.ku.dk  Thu Oct 16 21:08:27 2003
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Thu Oct 16 21:05:14 2003
Subject: [Rd] data() misbehaving inside a function
In-Reply-To: <20031016141029.A22651@jessie.research.bell-labs.com>
References: <20031016141029.A22651@jessie.research.bell-labs.com>
Message-ID: <x2u169ngpg.fsf@biostat.ku.dk>

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.
Meanwhile, my preferred device is

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

        -p

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907


[Rd] data() misbehaving inside a function
Peter Dalgaard