错误:在R中找不到函数。

时间:2022-09-08 16:17:33

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.

这是一个常见问题,所以请尽可能完整。答案是一个社区的答案,所以如果你觉得有什么东西不见了,可以随意编辑。

This question was discussed and approved on meta.

这个问题在meta上得到了讨论和批准。

I am using R and tried some.function but I got this error message :

我用R试了一些。函数,但我得到了这个错误信息:

Error: could not find function `some.function`

This question comes up very regularly. When you get the error: could not find function in R, how can you solve it?

这个问题经常出现。当你得到错误的时候:在R中找不到函数,你怎么解决它?

10 个解决方案

#1


93  

There are a few things you should check :

有几件事你应该检查一下:

  1. Did you write the name of your function correctly? Names are case sensitive.
  2. 你的函数名写对了吗?名称是区分大小写的。
  3. Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once)
  4. 您是否安装了包含该函数的包?安装。包(“包”)(这只需要做一次)
  5. Did you attach that package to the workspace ? require(thePackage) or library(thePackage) (this should be done every time you start a new R session)
  6. 您把那个包附加到工作空间了吗?需要(包)或库(包)(每次启动新的R会话时都应该这样做)
  7. Are you using an older R version where this function didn't exist yet?
  8. 你使用的是一个旧的R版本,这个函数还不存在吗?

If you're not sure in which package that function is situated, you can do a few things.

如果您不确定函数位于哪个包中,您可以做一些事情。

  1. If you're sure you installed and attached/loaded the right package, type help.search("some.function") or ??some.function to get an information box that can tell you in which package it is contained.
  2. 如果你确信你已经安装并加载了正确的软件包,输入help.search(“some.function”)或?函数获取一个信息框,它可以告诉您它包含在哪个包中。
  3. find and getAnywhere can also be used to locate functions.
  4. find和getAnywhere也可以用于定位函数。
  5. If you have no clue about the package, you can use findFn in the sos package as explained in this answer.
  6. 如果您对包没有任何线索,可以在sos包中使用findFn来解释这个答案。
  7. RSiteSearch("some.function") or searching with rseek are alternative ways to find the function.
  8. RSiteSearch(“some.function”)或用rseek搜索是找到该函数的其他方法。

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.

有时您需要使用旧版本的R,但是要运行为新版本创建的代码。新添加的函数(如R 3.4.0中的hasName)将不会被发现。如果您使用较老的R版本,并且希望使用一个较新的函数,您可以使用包后端口来实现这些功能。您还可以找到需要在backports的git repo上返回的函数列表。请记住,大于R3.0.0的R版本与为R3.0.0和后版本构建的包不兼容。

#2


24  

Another problem, in the presence of a NAMESPACE, is that you are trying to run an unexported function from package foo.

在名称空间存在的情况下,另一个问题是,您试图从包foo运行一个未导出的函数。

For example (contrived, I know, but):

例如(我知道,但是):

> mod <- prcomp(USArrests, scale = TRUE)
> plot.prcomp(mod)
Error: could not find function "plot.prcomp"

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. Using getAnywhere() we find that the function is in package stats:

首先,您不应该直接调用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>

So we can now call it directly using:

所以我们现在可以直接用:

> stats:::plot.prcomp(mod)

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 a namespace, R will report that it can't find the function unless you tell it which namespace to look in.

我使用阴谋。以prcomp为例说明其目的。在正常使用中,您不应该调用S3方法。但是正如我所说的,如果您想要调用的函数存在(它可能是一个隐藏的实用函数),但是在一个名称空间中,R将报告它不能找到这个函数,除非您告诉它要查看哪个名称空间。

#3


10  

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:

在我的控制下,我通常可以解决这个问题,但在使用网格时更麻烦。当网格不是同质的时候,不是所有的库都可以安装,而且我的经验经常是一个包没有安装,因为没有安装依赖项。为了解决这个问题,我检查了以下内容:

  1. Is Fortran installed? (Look for 'gfortran'.) This affects several major packages in R.
  2. Fortran安装吗?(寻找“gfortran”。)这影响了R中的几个主要包。
  3. Is Java installed? Are the Java class paths correct?
  4. Java安装吗?Java类路径是否正确?
  5. 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.
  6. 检查该包是否由管理员安装,并可由适当的用户使用。有时,用户会在错误的地方安装包,或者在没有适当的访问权限的情况下运行,. . libpath()是一个很好的检查。
  7. Check ldd results for R, to be sure about shared libraries
  8. 检查ldd结果为R,以确保共享库。
  9. 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.
  10. 定期运行一个脚本,它只加载所需的每个包并进行一些测试,这很好。这在工作流中尽可能早地捕获包问题。这类似于构建测试或单元测试,只不过它更像一个烟雾测试,以确保基本的东西能够正常工作。
  11. 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.)
  12. 如果包可以存储在网络可访问的位置,是吗?如果不能,是否有办法确保机器之间的一致版本?(这可能看起来不太好,但是正确的包安装包括了正确版本的可用性。)
  13. 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.
  14. 是否可以为给定的操作系统提供包?不幸的是,并非所有的包都可以跨平台使用。这可以追溯到第5步。如果可能的话,尝试找到一种方法来处理不同的操作系统,切换到一个适当的包的味道或者在某些情况下关闭依赖项。

Having 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.

遇到这种情况时,其中的一些步骤变得相当常规。虽然#7看起来是一个很好的起点,但是这些都是以我使用它们的频率的近似顺序列出的。

#4


5  

If this occurs while you check your package (R CMD check), take a look at your NAMESPACE.

如果在检查包(R CMD检查)时发生这种情况,请查看您的命名空间。

You can solve this by adding the following statement to the NAMESPACE:

您可以通过在名称空间中添加以下语句来解决这个问题:

exportPattern("^[^\\\\.]")

This exports everything that doesn't start with a dot ("."). This allows you to have your hidden functions, starting with a dot:

这个导出所有不以点(“。”)开头的东西。这允许你有你的隐藏的功能,从一个点开始:

.myHiddenFunction <- function(x) cat("my hidden function")

#5


4  

I had the error

我有错误

Error: could not find function some.function

错误:找不到函数some.function。

happen when doing R CMD check of a package I was making with RStudio. I found adding

在做R CMD检查的时候,我正在用RStudio做一个包。我发现添加

exportPattern(".")

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.

对命名空间文件执行了这个技巧。作为一个sidenote,我最初配置了RStudio以使用ROxygen来制作文档——并选择了ROxygen为我编写名称空间文件的配置,这使我的编辑器不断被删除。因此,在我的实例中,从Roxygen配置中不选中名称空间,并将exportPattern(“。”)添加到名称空间来解决这个错误。

#6


3  

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.

即使在缺少某些强制参数(i)时,该函数的名称是有效的,也会发生此错误。你没有提供足够的论据。我得到了这一个Rcpp上下文,我写了一个c++函数与optionnal参数,并没有提供这些参数在R .看来optionnal参数从c++被视为强制性R .因此,R找不到匹配的函数正确的名字,但一个不正确的数量的参数。

Rcpp Function : SEXP RcppFunction(arg1, arg2=0) {}
R Calls :
RcppFunction(0) raises the error
RcppFunction(0, 0) does not

Rcpp函数:SEXP RcppFunction(arg1, arg2=0) {} R调用:RcppFunction(0)提高了错误RcppFunction(0,0)。

#7


2  

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.

Rdocumentation.org有一个非常方便的搜索功能,它可以让你找到各种功能——从CRAN上的所有软件包,以及从生物指挥和GitHub上的软件包。

错误:在R中找不到函数。

#8


0  

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 ".

如果使用parallelMap,则需要将自定义函数导出到从属作业,否则就会出现“无法找到函数”的错误。

If you set a non-missing level on parallelStart the same argument should be passed to parallelExport, else you get the same error. So this should be strictly followed:

如果您在并行启动时设置了一个非缺失级别,那么应该将相同的参数传递给parallelExport,否则您将得到相同的错误。所以应该严格遵守:

parallelStart(mode = "<your mode here>", N, level = "<task.level>")
parallelExport("<myfun>", level = "<task.level>")

#9


0  

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

我得到了相同的错误,我正在运行版本。99xxx,我检查了帮助菜单的更新,并更新了我的RStudio到1.0x,然后错误没有出现。

So simple solution, just update your R Studio

简单的解决方案,只需更新你的R工作室。

#10


0  

You may be able to fix this error by name spacing :: the function call

您可以通过名称间距来修复这个错误::函数调用。

comparison.cloud(colors = c("red", "green"), max.words = 100)

to

wordcloud::comparison.cloud(colors = c("red", "green"), max.words = 100)

#1


93  

There are a few things you should check :

有几件事你应该检查一下:

  1. Did you write the name of your function correctly? Names are case sensitive.
  2. 你的函数名写对了吗?名称是区分大小写的。
  3. Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once)
  4. 您是否安装了包含该函数的包?安装。包(“包”)(这只需要做一次)
  5. Did you attach that package to the workspace ? require(thePackage) or library(thePackage) (this should be done every time you start a new R session)
  6. 您把那个包附加到工作空间了吗?需要(包)或库(包)(每次启动新的R会话时都应该这样做)
  7. Are you using an older R version where this function didn't exist yet?
  8. 你使用的是一个旧的R版本,这个函数还不存在吗?

If you're not sure in which package that function is situated, you can do a few things.

如果您不确定函数位于哪个包中,您可以做一些事情。

  1. If you're sure you installed and attached/loaded the right package, type help.search("some.function") or ??some.function to get an information box that can tell you in which package it is contained.
  2. 如果你确信你已经安装并加载了正确的软件包,输入help.search(“some.function”)或?函数获取一个信息框,它可以告诉您它包含在哪个包中。
  3. find and getAnywhere can also be used to locate functions.
  4. find和getAnywhere也可以用于定位函数。
  5. If you have no clue about the package, you can use findFn in the sos package as explained in this answer.
  6. 如果您对包没有任何线索,可以在sos包中使用findFn来解释这个答案。
  7. RSiteSearch("some.function") or searching with rseek are alternative ways to find the function.
  8. RSiteSearch(“some.function”)或用rseek搜索是找到该函数的其他方法。

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.

有时您需要使用旧版本的R,但是要运行为新版本创建的代码。新添加的函数(如R 3.4.0中的hasName)将不会被发现。如果您使用较老的R版本,并且希望使用一个较新的函数,您可以使用包后端口来实现这些功能。您还可以找到需要在backports的git repo上返回的函数列表。请记住,大于R3.0.0的R版本与为R3.0.0和后版本构建的包不兼容。

#2


24  

Another problem, in the presence of a NAMESPACE, is that you are trying to run an unexported function from package foo.

在名称空间存在的情况下,另一个问题是,您试图从包foo运行一个未导出的函数。

For example (contrived, I know, but):

例如(我知道,但是):

> mod <- prcomp(USArrests, scale = TRUE)
> plot.prcomp(mod)
Error: could not find function "plot.prcomp"

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. Using getAnywhere() we find that the function is in package stats:

首先,您不应该直接调用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>

So we can now call it directly using:

所以我们现在可以直接用:

> stats:::plot.prcomp(mod)

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 a namespace, R will report that it can't find the function unless you tell it which namespace to look in.

我使用阴谋。以prcomp为例说明其目的。在正常使用中,您不应该调用S3方法。但是正如我所说的,如果您想要调用的函数存在(它可能是一个隐藏的实用函数),但是在一个名称空间中,R将报告它不能找到这个函数,除非您告诉它要查看哪个名称空间。

#3


10  

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:

在我的控制下,我通常可以解决这个问题,但在使用网格时更麻烦。当网格不是同质的时候,不是所有的库都可以安装,而且我的经验经常是一个包没有安装,因为没有安装依赖项。为了解决这个问题,我检查了以下内容:

  1. Is Fortran installed? (Look for 'gfortran'.) This affects several major packages in R.
  2. Fortran安装吗?(寻找“gfortran”。)这影响了R中的几个主要包。
  3. Is Java installed? Are the Java class paths correct?
  4. Java安装吗?Java类路径是否正确?
  5. 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.
  6. 检查该包是否由管理员安装,并可由适当的用户使用。有时,用户会在错误的地方安装包,或者在没有适当的访问权限的情况下运行,. . libpath()是一个很好的检查。
  7. Check ldd results for R, to be sure about shared libraries
  8. 检查ldd结果为R,以确保共享库。
  9. 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.
  10. 定期运行一个脚本,它只加载所需的每个包并进行一些测试,这很好。这在工作流中尽可能早地捕获包问题。这类似于构建测试或单元测试,只不过它更像一个烟雾测试,以确保基本的东西能够正常工作。
  11. 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.)
  12. 如果包可以存储在网络可访问的位置,是吗?如果不能,是否有办法确保机器之间的一致版本?(这可能看起来不太好,但是正确的包安装包括了正确版本的可用性。)
  13. 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.
  14. 是否可以为给定的操作系统提供包?不幸的是,并非所有的包都可以跨平台使用。这可以追溯到第5步。如果可能的话,尝试找到一种方法来处理不同的操作系统,切换到一个适当的包的味道或者在某些情况下关闭依赖项。

Having 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.

遇到这种情况时,其中的一些步骤变得相当常规。虽然#7看起来是一个很好的起点,但是这些都是以我使用它们的频率的近似顺序列出的。

#4


5  

If this occurs while you check your package (R CMD check), take a look at your NAMESPACE.

如果在检查包(R CMD检查)时发生这种情况,请查看您的命名空间。

You can solve this by adding the following statement to the NAMESPACE:

您可以通过在名称空间中添加以下语句来解决这个问题:

exportPattern("^[^\\\\.]")

This exports everything that doesn't start with a dot ("."). This allows you to have your hidden functions, starting with a dot:

这个导出所有不以点(“。”)开头的东西。这允许你有你的隐藏的功能,从一个点开始:

.myHiddenFunction <- function(x) cat("my hidden function")

#5


4  

I had the error

我有错误

Error: could not find function some.function

错误:找不到函数some.function。

happen when doing R CMD check of a package I was making with RStudio. I found adding

在做R CMD检查的时候,我正在用RStudio做一个包。我发现添加

exportPattern(".")

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.

对命名空间文件执行了这个技巧。作为一个sidenote,我最初配置了RStudio以使用ROxygen来制作文档——并选择了ROxygen为我编写名称空间文件的配置,这使我的编辑器不断被删除。因此,在我的实例中,从Roxygen配置中不选中名称空间,并将exportPattern(“。”)添加到名称空间来解决这个错误。

#6


3  

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.

即使在缺少某些强制参数(i)时,该函数的名称是有效的,也会发生此错误。你没有提供足够的论据。我得到了这一个Rcpp上下文,我写了一个c++函数与optionnal参数,并没有提供这些参数在R .看来optionnal参数从c++被视为强制性R .因此,R找不到匹配的函数正确的名字,但一个不正确的数量的参数。

Rcpp Function : SEXP RcppFunction(arg1, arg2=0) {}
R Calls :
RcppFunction(0) raises the error
RcppFunction(0, 0) does not

Rcpp函数:SEXP RcppFunction(arg1, arg2=0) {} R调用:RcppFunction(0)提高了错误RcppFunction(0,0)。

#7


2  

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.

Rdocumentation.org有一个非常方便的搜索功能,它可以让你找到各种功能——从CRAN上的所有软件包,以及从生物指挥和GitHub上的软件包。

错误:在R中找不到函数。

#8


0  

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 ".

如果使用parallelMap,则需要将自定义函数导出到从属作业,否则就会出现“无法找到函数”的错误。

If you set a non-missing level on parallelStart the same argument should be passed to parallelExport, else you get the same error. So this should be strictly followed:

如果您在并行启动时设置了一个非缺失级别,那么应该将相同的参数传递给parallelExport,否则您将得到相同的错误。所以应该严格遵守:

parallelStart(mode = "<your mode here>", N, level = "<task.level>")
parallelExport("<myfun>", level = "<task.level>")

#9


0  

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

我得到了相同的错误,我正在运行版本。99xxx,我检查了帮助菜单的更新,并更新了我的RStudio到1.0x,然后错误没有出现。

So simple solution, just update your R Studio

简单的解决方案,只需更新你的R工作室。

#10


0  

You may be able to fix this error by name spacing :: the function call

您可以通过名称间距来修复这个错误::函数调用。

comparison.cloud(colors = c("red", "green"), max.words = 100)

to

wordcloud::comparison.cloud(colors = c("red", "green"), max.words = 100)