[R] package.skeleton from within function: objects not found
Tineke Casneuf
tineke.casneuf at gmail.com
Mon Jan 28 12:38:25 CET 2008
Professor Ripley,
do you have an idea why it works for me to save different types of
objects, but not for the environment object I construct in the
function:
### Example of a function that works without error:
fun2 <- function(myname){
f <- function(x,y) x+y
g <- function(x,y) x-y
d <- data.frame(a=1, b=2)
e <- "hello"
env <- sys.frames()[[sys.nframe()]]
package.skeleton(list=c("f","g","d","e"), name=myname,env=env)
}
fun2("mypkg")
####
fun3 <- function(myname){
myenv <- new.env()
apply(x, 1, function(row){
assign(row[1], row[2], envir=myenv)
})
f <- function(x,y) x+y
g <- function(x,y) x-y
d <- data.frame(a=1, b=2)
e <- "hello"
env <- sys.frames()[[sys.nframe()]]
package.skeleton(list=c("f","g","d","e","myenv"), name=myname,env=env)
}
fun3("mypkg")
###
For the second example, 'fun3', I get this error message:
Error in save(list = item, file = file.path(data_dir, sprintf("%s.rda", :
object 'myenv' not found
Thanks in advance!
On Jan 28, 2008 9:30 AM, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> You need to set the 'environment' argument (the help file is incomplete).
> e.g.
>
> env <- sys.frames()[[sys.nframe()]]
> package.skeleton(name = pkgName, list=c("f","e", "myenv"), env=env)
>
>
>
>
>
> On Mon, 28 Jan 2008, Tineke Casneuf wrote:
>
> > Hi all,
> >
> > I ran into a strange error: I am trying to create a package skeleton for a
> > new source package from within a function. Objects that are created in this
> > function are to be included in my package, but for some reason, I get an
> > error message saying that these objects cannot be found.
> >
> > Here is the code:
> > ######
> > myfun <- function(pkgName,x){
> > myenv <- new.env()
> > apply(x, 1, function(row){
> > assign(row[1], row[2], envir=myenv)
> > })
> > f <- function(x,y) x+y
> > e <- rnorm(1000)
> > # browser()
> > package.skeleton(name = pkgName, list=c("f","e", "myenv"))
> > return(myenv)
> > }
> > x <- data.frame(keys = LETTERS[1:5], values = 1:5)
> > myfun("test", x)
> > ######
> >
> > And my sessionInfo:
> >> sessionInfo()
> > R version 2.6.1 (2007-11-26)
> > i386-pc-mingw32
> > locale:
> > LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
> > States.1252;LC_MONETARY=English_United
> > States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252
> > attached base packages:
> > [1] stats graphics grDevices utils datasets methods base
> >>
> > I did not find anything referring to this problem in the help page, on the R
> > mailing list or wiki. Has anyone noticed this or can someone explain to me
> > why my objects cannot be found?
> >
> > Many thanks in advance,
> > best wishes,
> >
> > Tine
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> --
> Brian D. Ripley, ripley at stats.ox.ac.uk
> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
> University of Oxford, Tel: +44 1865 272861 (self)
> 1 South Parks Road, +44 1865 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
>
More information about the R-help
mailing list