这是一个常见问题,所以请尽可能完整。答案是一个社区答案,所以如果您认为遗漏了什么,请随意编辑。 这个问题在meta上进行了讨论和批准。
我用R做了一些尝试。函数,但我得到以下错误消息:
Error: could not find function "some.function"
这个问题经常出现。当你在R中得到这种类型的错误时,你如何解决它?
这是一个常见问题,所以请尽可能完整。答案是一个社区答案,所以如果您认为遗漏了什么,请随意编辑。 这个问题在meta上进行了讨论和批准。
我用R做了一些尝试。函数,但我得到以下错误消息:
Error: could not find function "some.function"
这个问题经常出现。当你在R中得到这种类型的错误时,你如何解决它?
当前回答
当计算机在我的控制下时,我通常可以解决这个问题,但当使用网格时,它更麻烦。当网格不是同质的时,可能不会安装所有的库,我的经验经常是由于没有安装依赖项而没有安装包。为了解决这个问题,我检查了以下内容:
Is Fortran installed? (Look for 'gfortran'.) This affects several major packages in R. Is Java installed? Are the Java class paths correct? Check that the package was installed by the admin and available for use by the appropriate user. Sometimes users will install packages in the wrong places or run without appropriate access to the right libraries. .libPaths() is a good check. Check ldd results for R, to be sure about shared libraries It's good to periodically run a script that just loads every package needed and does some little test. This catches the package issue as early as possible in the workflow. This is akin to build testing or unit testing, except it's more like a smoke test to make sure that the very basic stuff works. If packages can be stored in a network-accessible location, are they? If they cannot, is there a way to ensure consistent versions across the machines? (This may seem OT, but correct package installation includes availability of the right version.) Is the package available for the given OS? Unfortunately, not all packages are available across platforms. This goes back to step 5. If possible, try to find a way to handle a different OS by switching to an appropriate flavor of a package or switch off the dependency in certain cases.
在多次遇到这种情况后,其中一些步骤变得相当常规。虽然第7条似乎是一个很好的起点,但这些都是按照我使用它们的频率大致列出的。
其他回答
另一个问题是,在存在命名空间的情况下,您试图从包foo运行一个未导出的函数。
例如(我知道有些做作,但是):
> mod <- prcomp(USArrests, scale = TRUE)
> plot.prcomp(mod)
Error: could not find function "plot.prcomp"
首先,您不应该直接调用S3方法,但让我们假设plot。Prcomp实际上是包foo中一些有用的内部函数。如果你知道你在做什么,调用这样的函数需要使用:::。您还需要知道函数所在的名称空间。使用getAnywhere(),我们发现该函数在包的统计信息中:
> getAnywhere(plot.prcomp)
A single object matching ‘plot.prcomp’ was found
It was found in the following places
registered S3 method for plot from namespace stats
namespace:stats
with value
function (x, main = deparse(substitute(x)), ...)
screeplot.default(x, main = main, ...)
<environment: namespace:stats>
所以我们现在可以直接调用它:
> stats:::plot.prcomp(mod)
我用过情节。Prcomp只是作为一个例子来说明目的。在正常使用中,不应该像这样调用S3方法。但正如我所说,如果您想调用的函数存在(例如,它可能是一个隐藏的实用程序函数),但在一个名称空间中,R将报告它无法找到该函数,除非您告诉它查找哪个名称空间。
将其与以下内容进行比较: 统计数据::plot.prcomp 上述方法失败了,因为while stats使用了情节。Prcomp,它不是从统计数据导出的错误正确地告诉我们:
错误:“阴谋。Prcomp不是从namespace:stats导出的对象
记录如下:
Pkg::name返回命名空间Pkg中导出的变量名的值,而Pkg:::name返回内部变量名的值。
如果您正在使用parallelMap,则需要将自定义函数导出到从属作业,否则将得到“无法找到函数”的错误。
如果在parallelStart上设置了一个非缺失的级别,则应该将相同的参数传递给parallelExport,否则将得到相同的错误。所以这一点应该严格遵守:
parallelStart(mode = "<your mode here>", N, level = "<task.level>")
parallelExport("<myfun>", level = "<task.level>")
如果在检查包(R CMD检查)时发生这种情况,请查看您的命名空间。
你可以通过在命名空间中添加以下语句来解决这个问题:
exportPattern("^[^\\\\.]")
这将导出所有不以点(".")开头的内容。这允许你有你的隐藏函数,从一个点开始:
.myHiddenFunction <- function(x) cat("my hidden function")
我得到了同样的错误,我正在运行版本.99xxx,我从帮助菜单中检查更新并将我的RStudio更新到1.0x,然后错误没有出现
所以很简单的解决方法,就是更新你的R Studio
我犯了错误
错误:找不到函数
当我用RStudio做一个包的R CMD检查时发生。我发现
exportPattern(“。”)
命名空间文件成功了。作为旁注,我最初配置了RStudio,让它使用ROxygen来制作文档——并选择了ROxygen为我编写NAMESPACE文件的配置,这将不断擦除我的编辑。因此,在我的实例中,我从Roxygen配置中取消了NAMESPACE,并向NAMESPACE添加了exportPattern(“.”)来解决这个错误。