Tineke Casneuf
2008-Jan-28 08:00 UTC
[R] package.skeleton from within function: objects not found
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]]
Prof Brian Ripley
2008-Jan-28 08:30 UTC
[R] package.skeleton from within function: objects not found
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