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

发布于 2024-11-29 02:57:21 字数 535 浏览 3 评论 0原文

这是一个常见问题解答问题,因此请尽可能完整。答案是社区答案,因此如果您认为缺少某些内容,请随时进行编辑。

此问题已在元上讨论并获得批准。< /a>

我正在使用 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.

This question was discussed and approved on meta.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

花落人断肠 2024-12-06 02:57:21

您应该检查以下几件事:

  1. 您是否正确书写了函数名称?名称区分大小写。
  2. 您是否安装了包含该功能的软件包? install.packages("thePackage") (这只需要完成一次)
  3. 您是否将该包附加到工作区?
    require(thePackage)(并检查其返回值)或library(thePackage)(每次启动新的 R 会话时都应该执行此操作)
  4. 您是否使用旧的 R哪个版本还没有这个功能?
  5. 您是否使用特定软件包的不同版本?这可能是任一方向的:随着时间的推移,功能会被添加和删除,并且您引用的代码可能需要比您安装的包更新或旧的版本。

如果您不确定该函数位于哪个包中,您可以执行一些操作。

  1. 如果您确定安装并附加/加载了正确的软件包,请输入 help.search("some.function")??some.function 获取信息盒子可以告诉您它包含在哪个包裹中。
  2. findgetAnywhere 也可用于定位函数。
  3. 如果您对该包一无所知,可以使用 sos 包中的 findFn,如 这个答案
  4. RSiteSearch("some.function") 或使用 rdocumentationrseek 是查找该函数的替代方法。

有时您需要使用旧版本的 R,但运行为新版本创建的代码。新添加的函数(例如 R 3.4.0 中的 hasName)将无法找到。如果您使用较旧的 R 版本并希望使用较新的功能,可以使用包 backports 使此类功能可用。您还可以在 git 存储库上找到需要向后移植的函数列表向后移植。请记住,早于 R3.0.0 的 R 版本与为 R3.0.0 及更高版本构建的软件包不兼容。

There are a few things you should check :

  1. Did you write the name of your function correctly? Names are case sensitive.
  2. Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once)
  3. Did you attach that package to the workspace ?
    require(thePackage) (and check its return value) or library(thePackage) (this should be done every time you start a new R session)
  4. Are you using an older R version where this function didn't exist yet?
  5. Are you using a different version of the specific package? This could be in either direction: functions are added and removed over time, and it's possible the code you're referencing is expecting a newer or older version of the package than what you have installed.

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. find and getAnywhere can also be used to locate functions.
  3. If you have no clue about the package, you can use findFn in the sos package as explained in this answer.
  4. 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.

灯下孤影 2024-12-06 02:57:21

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

例如(我知道是人为的,但是):

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

首先,您不应该直接调用 S3 方法,但让我们假设 plot.prcomp 实际上是包 foo。如果您知道自己在做什么,则要调用此类函数需要使用 :::。您还需要知道该函数所在的命名空间。使用getAnywhere()我们发现该函数位于包stats中:

> 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)

我使用了plot.prcomp 只是举个例子来说明目的。在正常使用中,您不应该像这样调用 S3 方法。但正如我所说,如果您要调用的函数存在(例如,它可能是隐藏的实用函数),但位于命名空间中,R 将报告它无法找到该函数,除非你告诉它要查找哪个名称空间。

将其与以下内容进行比较:
统计::plot.prcomp
上面的失败是因为当 stats 使用 plot.prcomp 时,它不会从 stats 导出,因为错误正确地告诉我们:

错误:“plot.prcomp”不是从“namespace:stats”导出的对象

这记录如下:

pkg::name 返回命名空间 pkg 中导出的变量名称的值,而 pkg:::name 返回内部变量名称的值。

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):

> 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:

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

Compare this to the following:
stats::plot.prcomp
The above fails because while stats uses plot.prcomp, it is not exported from stats as the error rightly tells us:

Error: 'plot.prcomp' is not an exported object from 'namespace:stats'

This is documented as follows:

pkg::name returns the value of the exported variable name in namespace pkg, whereas pkg:::name returns the value of the internal variable name.

拒绝两难 2024-12-06 02:57:21

当计算机在我的控制之下时,我通常可以解决这个问题,但是当使用网格时,它会更麻烦。当网格不均匀时,并非所有库都会被安装,并且我的经验通常是由于未安装依赖项而未安装软件包。为了解决这个问题,我检查以下内容:

  1. 是否安装了 Fortran? (查找“gfortran”。)这会影响 R 中的几个主要包。
  2. 是否安装了 Java? Java 类路径是否正确?
  3. 检查该软件包是否已由管理员安装并可供适当的用户使用。有时,用户会将软件包安装在错误的位置,或者在没有适当访问正确库的情况下运行。 .libPaths() 是一个很好的检查。
  4. 检查 R 的 ldd 结果,以确保共享库
  5. 定期运行一个脚本是很好的,该脚本仅加载所需的每个包并进行一些小测试。这可以在工作流程中尽早捕获包问题。这类似于构建测试或单元测试,只不过它更像是冒烟测试,以确保非常基本的东西有效。
  6. 如果包可以存储在网络可访问的位置,是吗?如果不能,有没有办法确保机器上的版本一致? (这可能看起来有些过时,但正确的软件包安装包括正确版本的可用性。)
  7. 该软件包可用于给定的操作系统吗?不幸的是,并非所有软件包都可以跨平台使用。这将返回到步骤 5。如果可能,请尝试找到一种方法来处理不同的操作系统,方法是切换到适当的包风格或在某些情况下关闭依赖关系。

在多次遇到这种情况后,其中一些步骤变得相当常规。虽然#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:

  1. Is Fortran installed? (Look for 'gfortran'.) This affects several major packages in R.
  2. Is Java installed? Are the Java class paths correct?
  3. 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.
  4. Check ldd results for R, to be sure about shared libraries
  5. 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.
  6. 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.)
  7. 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.

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.

月依秋水 2024-12-06 02:57:21

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

您可以通过将以下语句添加到命名空间来解决此问题:

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

这将导出不以点(“.”)开头的所有内容。这允许您拥有隐藏的函数,以点开头:

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

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:

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")
想你的星星会说话 2024-12-06 02:57:21

我有错误

错误:找不到函数some.function

错误:对我使用 RStudio 制作的包进行 R CMD 检查时发生 。我发现将

exportPattern(".")

添加到 NAMESPACE 文件就可以了。作为旁注,我最初将 RStudio 配置为使用 ROxygen 来制作文档,并选择了 ROxygen 为我编写 NAMESPACE 文件的配置,这不断删除我的编辑。因此,在我的实例中,我从 Roxygen 配置中取消选中 NAMESPACE,并将 exportPattern(".") 添加到 NAMESPACE 以解决此错误。

I had the error

Error: could not find function some.function

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.

违心° 2024-12-06 02:57:21

即使函数名称有效,如果缺少某些强制参数(即您没有提供足够的参数),也可能会发生此错误。
我在 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 error
RcppFunction(0, 0) does not

西瓜 2024-12-06 02:57:21

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

enter image description here

尝蛊 2024-12-06 02:57:21

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

如果您在 parallelStart 上设置非缺失级别,则应将相同的参数传递给 parallelExport,否则您会收到相同的错误。所以应该严格遵守:

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

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 to parallelExport, else you get the same error. So this should be strictly followed:

parallelStart(mode = "<your mode here>", N, level = "<task.level>")
parallelExport("<myfun>", level = "<task.level>")
一世旳自豪 2024-12-06 02:57:21

您可以通过名称间距::函数调用来修复此

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

错误

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

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)
美羊羊 2024-12-06 02:57:21

找不到的函数可能不是指定的函数。我在使用模块 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.

守望孤独 2024-12-06 02:57:21

我得到了同样的错误,我正在运行版本 .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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文