[R] problem with nlme, environments, and packages
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Jun 16 12:48:49 CEST 2015
On 15/06/2015 8:32 PM, Greg Hather wrote:
> Hello R users,
>
> I encountered a strange problem while writing a package that uses the
> nlme function. First, I wrote some code that uses the nlme function,
> and it ran without errors. However, when I tried to put the code into
> a package, the nlme function was unable to locate a function that was
> used in the formula. Could it be that nlme is looking in the wrong
> environment? I would appreciate any suggestions. Below is a
> reproducible example with the problem.
I haven't tested this (I don't use the devtools stuff), but I'd say
there are two likely possibilities:
1. nlme() isn't evaluating the formula properly.
2. Your test isn't doing what you think it is doing, because you have a
second copy of main_function in your global environment.
Assuming you can rule out 2, could you put together a tarball of the
package that I could actually run?
Duncan Murdoch
>
> ########### BEGIN EXAMPLE ##############
>
> #' Fake package to show nlme error
> #' @export
>
> main_function <- function(x){
> library(nlme)
> result <- nlme(height ~ SSasymp(age, Asym, R0, lrc) +
> nonlinear_function(age),
> data = Loblolly,
> fixed = Asym + R0 + lrc ~ 1,
> random = Asym ~ 1,
> start = c(Asym = 103, R0 = -8.5, lrc = -3.3))
> result
> }
>
> nonlinear_function <- function(x){
> log(x)
> }
>
> ########### END EXAMPLE ##############
>
> The above code can be installed as a package and run with the commands
>
> library(devtools)
> library(roxygen2)
> setwd("C:/test") # or any prefered directory
> create("testPackage")
> setwd("./testPackage")
> document()
> setwd("..")
> install("testPackage")
> main_function()
>
> The output is
>
>> main_function()
> Error in eval(expr, envir, enclos) :
> could not find function "nonlinear_function"
>>
>> sessionInfo()
> R version 3.1.3 (2015-03-09)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 8 x64 (build 9200)
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
> attached base packages:
> [1] stats graphics grDevices utils datasets methods
> [7] base
> other attached packages:
> [1] nlme_3.1-120 testPackage_0.0.0.9000
> [3] roxygen2_4.1.1 devtools_1.8.0
> loaded via a namespace (and not attached):
> [1] curl_0.8 digest_0.6.8 git2r_0.10.1
> [4] grid_3.1.3 lattice_0.20-31 magrittr_1.5
> [7] memoise_0.2.1 Rcpp_0.11.6 rversions_1.0.1
> [10] stringi_0.4-1 stringr_1.0.0 tools_3.1.3
> [13] xml2_0.1.1
>
> Note that if I simply paste main_function and nonlinear_function into
> the R console, then main_function() runs without errors.
>
> Greg
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
More information about the R-help
mailing list