[R] problem with nlme, environments, and packages
Bert Gunter
bgunter.4567 at gmail.com
Tue Jun 16 17:05:36 CEST 2015
An aside...
Just wanted to point out that:
fun <- function(x)log(x)
can be more simply replaced by:
fun <- log
Functions in R a full first class objects and can be treated as such. In
your example, this is still silly of course, but becomes relevant in
function calls where you can do things like
myfun <- function( FUN = log,...)
{ ...
something <- FUN(X)
...
}
Just in case this might be useful to you.
Cheers,
Bert
Bert Gunter
"Data is not information. Information is not knowledge. And knowledge is
certainly not wisdom."
-- Clifford Stoll
On Mon, Jun 15, 2015 at 4:32 PM, Greg Hather <ghather at gmail.com> 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.
>
> ########### 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list