insertSource()错误:“未找到对象'.cacheOnAssign'" [英] Error with insertSource(): "object '.cacheOnAssign' not found"
问题描述
我正在尝试使用功能insertSource
(R 2.12中的新增功能)来更新我进行了更改的功能.
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')
我得到了错误:
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)
stats软件包和mypackage之间的一个区别是库的位置(mypackage在'〜/lib/R/'中.( update )):当.libPaths('~/lib/R')
在
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.
- 该错误是什么意思?
- 如何使用
insertSource
?
调试
使用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 results, and where to go from here
使用options(error = browser)
可以提供我放置在文本文件中的更多信息
using options(error = browser)
gives more information that I have placed in a text file
推荐答案
这可以说是insertSource()
中的错误.当您提供文件而不是环境时,它在文件上调用evalSource()
,该文件在源环境中合成了.cacheOnAssign
变量(简称为FALSE
).因为变量不一定存在于程序包名称空间中,所以替换操作很麻烦.
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.
有几种解决方法,最简单的方法是自己创建环境并像在此修复程序中那样删除.cacheOnAssign
:
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, ...)
}
这篇关于insertSource()错误:“未找到对象'.cacheOnAssign'"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!