错误:找不到功能...在R中

发布于 2025-02-11 05:10:17 字数 766 浏览 2 评论 0 原文

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

在meta上讨论并批准了这个问题。 /a>

我正在使用R并尝试了 some.function.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

秋心╮凉 2025-02-18 05:10:17

您应该检查一些事情:

  1. 您是否正确编写了功能的名称?名称是案例敏感的。
  2. 您是否安装了包含该功能的软件包? install.package(“ thepackage”)(只需要一次完成一次)
  3. 您是否将该软件包附加到工作区?
    require(package)(检查其返回值)或 library(package)(每次开始新的R会话时,
  4. 都应该使用旧的R此功能还不存在的版本?
  5. 您是否使用特定软件包的不同版本?这可能是朝任一方向的:随着时间的推移,添加和删除了功能,并且您引用的代码可能期望该软件包的更新版本或更旧的版本要比安装了。

如果您不确定该功能所在的包裹所在,可以做一些事情。

  1. 如果您确定已安装和附加/加载正确的软件包,请键入 help.search(“ some.function”)?可以告诉您包含哪个软件包的框。
  2. 查找 getanywhere 也可以用于定位功能。
  3. 如果您对该软件包一无所知,则可以在 sos 软件包中使用 findfn ,如此答案
  4. 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 :

  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.

野の 2025-02-18 05:10:17

在存在命名空间的情况下,另一个问题是,您正在尝试从软件包 foo 中运行一个未出现的功能。

例如(我知道,但是):

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

首先,您不应该直接调用S3方法,而是假设 plot.prcomp 实际上是包装中的一些有用的内部功能 foo 。如果您知道自己在做什么,则需要使用 ::: 。您还需要知道找到功能的命名空间。使用 getAnywhere()我们发现该函数在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>

因此我们现在可以直接使用:

> stats:::plot.prcomp(mod)

我使用 plot.prcomp 作为说明目的的示例。在正常使用中,您不应该调用这样的S3方法。但是正如我所说,如果您要调用的函数存在(例如,它可能是隐藏的实用程序功能),但是在 namespace 中,R将报告它找不到该功能,除非您告诉它要查看哪个名称空间。

将其与以下内容进行比较:
stats :: plot.prcomp
以上失败是因为 stats 使用 plot.prcomp ,它并未从 stats 中导出,因为该错误正确地告诉我们:

错误:'plot.prcomp'不是从'namespace:stats'

导出的对象

如下所示:

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

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.

春风十里 2025-02-18 05:10:17

当计算机受到控制时,我通常可以解决此问题,但是使用网格时,这更令人讨厌。当网格不是同质的时,并非所有库都可以安装,并且我的经验通常是因为没有安装依赖性,因此没有安装软件包。为了解决这个问题,我检查以下内容:

  1. 安装了Fortran吗? (寻找'gfortran'。)这会影响R.
  2. Java中的几个主要软件包吗? Java类路径正确吗?
  3. 检查包软件包是否由管理员安装,并由适当的用户使用。有时,用户会在错误的地方安装软件包,或者在不适当访问正确的库中运行。 .libpaths()是一个很好的检查。
  4. 检查 LDD R的结果,以确保共享库的确定,
  5. 可以定期运行一个脚本,该脚本只需加载每个包装并进行一些测试。这会尽早在工作流程中捕获包装问题。这类似于构建测试或单元测试,只是更像是烟雾测试以确保非常基本的东西有效。
  6. 如果可以将软件包存储在可访问网络的位置,是吗?如果他们不能,是否有办法确保整个机器上的一致版本? (这似乎是OT,但是正确的软件包安装包括右版本的可用性。)
  7. 适用于给定OS的软件包吗?不幸的是,并非所有的软件包都可以跨平台提供。这可以追溯到第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.

梦纸 2025-02-18 05:10:17

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

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

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

这导出了不以dot开头的所有内容(“。”)。这使您可以从一个点开始拥有隐藏的功能:

.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")
十年九夏 2025-02-18 05:10:17

我有错误

错误:找不到函数 some.function

在执行我与Rstudio进行的软件包检查时发生。我发现

添加导出(“。”)

在命名空间文件中 可以做到这一点。作为旁注,我最初已配置了rstudio来使用roxygen来制作文档 - 并选择了roxygen会为我编写我的命名空间文件的配置,从而不断删除我的编辑。因此,在我的情况下,我从roxygen配置中未选中的名称空间,并将earportpattern(“。”)添加到命名空间以求解此错误。

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.

小鸟爱天空丶 2025-02-18 05:10:17

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

摇划花蜜的午后 2025-02-18 05:10:17

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

enter image description here

决绝 2025-02-18 05:10:17

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

如果您在 Parallelstart 上设置一个非错过级别,则应将相同的参数传递到 parallexport ,否则您会遇到相同的错误。因此,应严格遵循:

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>")
森林很绿却致人迷途 2025-02-18 05:10:17

您可能可以通过名称间距:: 函数呼叫

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)
-残月青衣踏尘吟 2025-02-18 05:10:17

找不到的函数可能不是命名的函数。当在模块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.

梦幻的味道 2025-02-18 05:10:17

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

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