错误:在 R 中找不到函数...
这是一个常见问题解答问题,因此请尽可能完整。答案是社区答案,因此如果您认为缺少某些内容,请随时进行编辑。
我正在使用 R 并尝试了 some.function
但收到以下错误消息:
Error: could not find function "some.function"
这个问题经常出现。当你在 R 中遇到这种类型的错误时,你该如何解决它?
This is meant to be a FAQ question, so please be as complete as possible. The answer is a community answer, so feel free to edit if you think something is missing.
I am using R and tried some.function
but I got following error message:
Error: could not find function "some.function"
This question comes up very regularly. When you get this type of error in R, how can you solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您应该检查以下几件事:
install.packages("thePackage")
(这只需要完成一次)require(thePackage)
(并检查其返回值)或library(thePackage)
(每次启动新的 R 会话时都应该执行此操作)如果您不确定该函数位于哪个包中,您可以执行一些操作。
help.search("some.function")
或??some.function
获取信息盒子可以告诉您它包含在哪个包裹中。find
和getAnywhere
也可用于定位函数。sos
包中的findFn
,如 这个答案。RSiteSearch("some.function")
或使用 rdocumentation 或 rseek 是查找该函数的替代方法。有时您需要使用旧版本的 R,但运行为新版本创建的代码。新添加的函数(例如 R 3.4.0 中的 hasName)将无法找到。如果您使用较旧的 R 版本并希望使用较新的功能,可以使用包 backports 使此类功能可用。您还可以在 git 存储库上找到需要向后移植的函数列表向后移植。请记住,早于 R3.0.0 的 R 版本与为 R3.0.0 及更高版本构建的软件包不兼容。
There are a few things you should check :
install.packages("thePackage")
(this only needs to be done once)require(thePackage)
(and check its return value) orlibrary(thePackage)
(this should be done every time you start a new R session)If you're not sure in which package that function is situated, you can do a few things.
help.search("some.function")
or??some.function
to get an information box that can tell you in which package it is contained.find
andgetAnywhere
can also be used to locate functions.findFn
in thesos
package as explained in this answer.RSiteSearch("some.function")
or searching with rdocumentation or rseek are alternative ways to find the function.Sometimes you need to use an older version of R, but run code created for a newer version. Newly added functions (eg hasName in R 3.4.0) won't be found then. If you use an older R version and want to use a newer function, you can use the package backports to make such functions available. You also find a list of functions that need to be backported on the git repo of backports. Keep in mind that R versions older than R3.0.0 are incompatible with packages built for R3.0.0 and later versions.
在存在命名空间的情况下,另一个问题是您试图从包 foo 运行未导出的函数。
例如(我知道是人为的,但是):
首先,您不应该直接调用 S3 方法,但让我们假设
plot.prcomp
实际上是包 foo。如果您知道自己在做什么,则要调用此类函数需要使用:::
。您还需要知道该函数所在的命名空间。使用getAnywhere()
我们发现该函数位于包stats中:所以我们现在可以直接调用它:
我使用了
plot.prcomp
只是举个例子来说明目的。在正常使用中,您不应该像这样调用 S3 方法。但正如我所说,如果您要调用的函数存在(例如,它可能是隐藏的实用函数),但位于命名空间
中,R 将报告它无法找到该函数,除非你告诉它要查找哪个名称空间。将其与以下内容进行比较:
统计::plot.prcomp
上面的失败是因为当
stats
使用plot.prcomp
时,它不会从stats
导出,因为错误正确地告诉我们:这记录如下:
Another problem, in the presence of a NAMESPACE, is that you are trying to run an unexported function from package foo.
For example (contrived, I know, but):
Firstly, you shouldn't be calling S3 methods directly, but lets assume
plot.prcomp
was actually some useful internal function in package foo. To call such function if you know what you are doing requires the use of:::
. You also need to know the namespace in which the function is found. UsinggetAnywhere()
we find that the function is in package stats:So we can now call it directly using:
I've used
plot.prcomp
just as an example to illustrate the purpose. In normal use you shouldn't be calling S3 methods like this. But as I said, if the function you want to call exists (it might be a hidden utility function for example), but is in anamespace
, R will report that it can't find the function unless you tell it which namespace to look in.Compare this to the following:
stats::plot.prcomp
The above fails because while
stats
usesplot.prcomp
, it is not exported fromstats
as the error rightly tells us:This is documented as follows:
当计算机在我的控制之下时,我通常可以解决这个问题,但是当使用网格时,它会更麻烦。当网格不均匀时,并非所有库都会被安装,并且我的经验通常是由于未安装依赖项而未安装软件包。为了解决这个问题,我检查以下内容:
.libPaths()
是一个很好的检查。在多次遇到这种情况后,其中一些步骤变得相当常规。虽然#7 似乎是一个很好的起点,但它们是按照我使用它们的频率的大致顺序列出的。
I can usually resolve this problem when a computer is under my control, but it's more of a nuisance when working with a grid. When a grid is not homogenous, not all libraries may be installed, and my experience has often been that a package wasn't installed because a dependency wasn't installed. To address this, I check the following:
.libPaths()
is a good check.ldd
results for R, to be sure about shared librariesHaving encountered this quite a bit, some of these steps become fairly routine. Although #7 might seem like a good starting point, these are listed in approximate order of the frequency that I use them.
如果在检查包(R CMD 检查)时发生这种情况,请查看您的命名空间。
您可以通过将以下语句添加到命名空间来解决此问题:
这将导出不以点(“.”)开头的所有内容。这允许您拥有隐藏的函数,以点开头:
If this occurs while you check your package (R CMD check), take a look at your NAMESPACE.
You can solve this by adding the following statement to the NAMESPACE:
This exports everything that doesn't start with a dot ("."). This allows you to have your hidden functions, starting with a dot:
我有错误
错误:对我使用 RStudio 制作的包进行 R CMD 检查时发生 。我发现将
exportPattern(".")
添加到 NAMESPACE 文件就可以了。作为旁注,我最初将 RStudio 配置为使用 ROxygen 来制作文档,并选择了 ROxygen 为我编写 NAMESPACE 文件的配置,这不断删除我的编辑。因此,在我的实例中,我从 Roxygen 配置中取消选中 NAMESPACE,并将 exportPattern(".") 添加到 NAMESPACE 以解决此错误。
I had the error
happen when doing R CMD check of a package I was making with RStudio. I found adding
exportPattern(".")
to the NAMESPACE file did the trick. As a sidenote, I had initially configured RStudio to use ROxygen to make the documentation -- and selected the configuration where ROxygen would write my NAMESPACE file for me, which kept erasing my edits. So, in my instance I unchecked NAMESPACE from the Roxygen configuration and added exportPattern(".") to NAMESPACE to solve this error.
即使函数名称有效,如果缺少某些强制参数(即您没有提供足够的参数),也可能会发生此错误。
我在 Rcpp 上下文中得到了这个,在那里我编写了一个带有可选参数的 C++ 函数,并且没有在 R 中提供这些参数。看来来自 C++ 的可选参数被 R 视为强制的。因此,R 找不到名称正确但参数数量不正确的匹配函数。
Rcpp 函数:
SEXP RcppFunction(arg1, arg2=0) {}
R 调用:
RcppFunction(0)
引发错误RcppFunction(0, 0)
不This error can occur even if the name of the function is valid if some mandatory arguments are missing (i.e you did not provide enough arguments).
I got this in an Rcpp context, where I wrote a C++ function with optionnal arguments, and did not provided those arguments in R. It appeared that optionnal arguments from the C++ were seen as mandatory by R. As a result, R could not find a matching function for the correct name but an incorrect number of arguments.
Rcpp Function :
SEXP RcppFunction(arg1, arg2=0) {}
R Calls :
RcppFunction(0)
raises the errorRcppFunction(0, 0)
does notRdocumentation.org 有一个非常方便的搜索功能,除其他外,它可以让您从所有内容中查找功能CRAN 上的软件包,以及来自 Bioconductor 和 GitHub 的软件包。
Rdocumentation.org has a very handy search function that - among other things - lets you find functions - from all the packages on CRAN, as well as from packages from Bioconductor and GitHub.
如果您使用的是parallelMap,您需要将自定义函数导出到从属作业,否则您会收到错误“无法找到函数”。
如果您在
parallelStart
上设置非缺失级别,则应将相同的参数传递给parallelExport
,否则您会收到相同的错误。所以应该严格遵守:If you are using
parallelMap
you'll need to export custom functions to the slave jobs, otherwise you get an error "could not find function ".If you set a non-missing level on
parallelStart
the same argument should be passed toparallelExport
, else you get the same error. So this should be strictly followed:您可以通过名称间距::函数调用来修复此
错误
You may be able to fix this error by name spacing :: the function call
to
找不到的函数可能不是指定的函数。我在使用模块 R/cR 中的函数时遇到了此错误,该函数是在模块 R/aR 中定义的,该函数之前已在 R/bR 中成功使用,并且文件来源按 a、b、c 顺序。我传递的参数之一是全局参数。它又由一个函数设置。该函数具有依赖关系,而该依赖关系又会出现错误。解决 R/aR 中定义的依赖项中的错误也解决了该错误。这是惰性求值导致难以调试情况的众多情况之一。因此,如果错误看起来毫无意义,请仔细查看参数以及它们的设置方式。
The function that cannot be found may not be the function that is named. I ran into this error when using a function in module R/c.R that was defined in module R/a.R that was previously successfully used in R/b.R with the files sourced in a,b,c order. One of the parameters I was passing was a global. It in turn was set by a function. The function had a dependency that in turn had an error. Resolving the error in the dependency which was also defined in R/a.R resolved the error. This is one of those many cases where lazy evaluation leads to hard to debug situations. So, if the error seems non-sensical, take a hard look at the parameters and how they are getting set.
我得到了同样的错误,我正在运行版本 .99xxx,我从帮助菜单中检查了更新并将我的 RStudio 更新到 1.0x,然后错误没有出现
所以简单的解决方案,只需更新你的 R Studio
I got the same, error, I was running version .99xxx, I checked for updates from help menu and updated My RStudio to 1.0x, then the error did not come
So simple solution, just update your R Studio