开发者

Error with insertSource(): "object '.cacheOnAssign' not found"

I am trying to use the function insertSource (new in R 2.12) to update a function that I have made changes on.

However, when I use the function in this way:

insertSource('filename.R', package = 'mypackage')

I get the error:

Error in get(this, envir = envp) : object '.cacheOnAssign' not found

Unfortunately I can not come up with a simple reproducible example - if one would be helpful, please suggest how I can do it - but I have found that the following code does work:

system("echo 'nls <- function(nls) return(nls)' > foo.R")
insertSource('foo.R', package = stats)

One difference between the stats package and mypackage is the library location (mypackage is in '~/lib/R/'. (update): error still occurs when .libPaths('~/lib/R') is in .Rprofile, and googleing '.cacheOnAssign' only returns 6 hits, two of them to this question.

Questions:

  1. what does the error mean?
  2. How can I use insertSource?

Debugging

using options(error = recover)

options(error = recover) 
Error in get(this, envir = envp) : object '.cacheOnAssign' not found
Called from: get(this, envir = envp)
Browse[1]> where
where 1: get(this, envir = envp)
where 2: insertSource("filename.R", "mypackage")
Browse[1]> ls()
[1] "q"
Browse[1]> n
>

not sure what to make of these resul开发者_运维知识库ts, and where to go from here

using options(error = browser) gives more information that I have placed in a text file


This is arguably a bug in insertSource(). When you supply a file instead of an environment, it calls evalSource() on the file which synthesizes the .cacheOnAssign variable (it will be simply FALSE) in the source environment. That chokes the replacement since the variable does not necessarily exist in the package namespace.

There are several ways around this, probably the easiest is to create the environment yourself and remove .cacheOnAssign like in this fix:

insertSource <- function(source, package="", ...) {
    if (!is(source, "environment")) {
        source <- evalSource(source, package = package, lock = FALSE)
        rm(.cacheOnAssign, envir = source)
    }
    methods::insertSource(source, package = package, ...)
}


I wanted to solve this without yielding to bugger, and I think I have. :)

I am not sure what the error message means, but it is uninformative and possibly undocumented, which means that it is useless to me. In any case, my goal was to solve the issue of using insertSource rather than to figure out the meaning of this error.

It turns out that one needs to specify the functions argument in insertSource(). Here's a generic version of how I got it to work:

# Specify the new source file and the package to be updated
tmpFname <- "mySource.R"
myPkgName <- "myPkg"

# Load the package - it might not already be loaded
do.call(library, list(myPkgName))

# We need to know which functions are going to be produced, so we run `evalSource()`
tmpEval <- evalSource(tmpFname, package = myPkgName)

# Not everything from `evalSource()` is a function, so omit that stuff
newFuncs <- ls(tmpEval)[-match(c("packageName", "sourceFile"), ls(tmpEval))]

# Now, we can submit a proper incantation to `insertSource()`
insertSource(tmpFname, package = myPkgName, functions = newFuncs)

The output is then:

Modified functions inserted through trace():  (... my functions here ...)

I hope this works for others. The documentation for insertSource() and evalSource() could use some work.


Note 1. Although I think I've addressed the goal of using insertSource(), the bounty remains in place. Answering David's 2 original questions (i.e. what does the error mean, and what are these functions really doing & how should the be used), would suffice. I'm not able to answer the error question, nor do I have much familiarity of the arguments/parameters to evalSource and insertSource.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜