错误:找不到功能...在R中
这是一个常见问题解答问题,因此请尽可能完整。答案是社区的答案,因此,如果您认为缺少某些东西,请随时编辑。
我正在使用R并尝试了 some.function.function
,但我得到了以下错误消息:
Error: could not find function "some.function"
这个问题定期出现。当您在R中获得此类型的错误时,如何解决?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您应该检查一些事情:
install.package(“ thepackage”)
(只需要一次完成一次)require(package)
(检查其返回值)或library(package)
(每次开始新的R会话时,如果您不确定该功能所在的包裹所在,可以做一些事情。
help.search(“ some.function”)
或?可以告诉您包含哪个软件包的框。
查找
和getanywhere
也可以用于定位功能。sos
软件包中使用findfn
,如此答案。rsitesearch(“ some.function”)
或使用 rdocumentation 或 rseek 是查找功能的替代方法。有时您需要使用旧版本的R,但是为较新版本创建的Run代码。那时找不到新添加的功能(例如,r 3.4.0中的hasname)。如果使用较旧的R版本并想使用较新的功能,则可以使用软件包 backports 使此类功能可用。您还可以找到需要在。请记住,与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()
我们发现该函数在package stats :因此我们现在可以直接使用:
我使用
plot.prcomp
作为说明目的的示例。在正常使用中,您不应该调用这样的S3方法。但是正如我所说,如果您要调用的函数存在(例如,它可能是隐藏的实用程序功能),但是在namespace
中,R将报告它找不到该功能,除非您告诉它要查看哪个名称空间。将其与以下内容进行比较:
stats :: 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()
是一个很好的检查。LDD
R的结果,以确保共享库的确定,遇到了很多事情之后,其中一些步骤变得相当常规。尽管#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检查)时发生这种情况,请查看您的名称空间。
您可以通过在名称空间中添加以下语句来解决此问题:
这导出了不以dot开头的所有内容(“。”)。这使您可以从一个点开始拥有隐藏的功能:
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进行的软件包检查时发生。我发现
添加导出(“。”)
在命名空间文件中 可以做到这一点。作为旁注,我最初已配置了rstudio来使用roxygen来制作文档 - 并选择了roxygen会为我编写我的命名空间文件的配置,从而不断删除我的编辑。因此,在我的情况下,我从roxygen配置中未选中的名称空间,并将earportpattern(“。”)添加到命名空间以求解此错误。
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上下文中得到了这个,在那里我用选项naln参数编写了C ++函数,并且没有在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上的包装,以及来自生物通用器和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
上设置一个非错过级别,则应将相同的参数传递到parallexport
,否则您会遇到相同的错误。因此,应严格遵循: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.
continue
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