这是一个常见问题,所以请尽可能完整。答案是一个社区答案,所以如果您认为遗漏了什么,请随意编辑。 这个问题在meta上进行了讨论和批准。
我用R做了一些尝试。函数,但我得到以下错误消息:
Error: could not find function "some.function"
这个问题经常出现。当你在R中得到这种类型的错误时,你如何解决它?
这是一个常见问题,所以请尽可能完整。答案是一个社区答案,所以如果您认为遗漏了什么,请随意编辑。 这个问题在meta上进行了讨论和批准。
我用R做了一些尝试。函数,但我得到以下错误消息:
Error: could not find function "some.function"
这个问题经常出现。当你在R中得到这种类型的错误时,你如何解决它?
有几件事你应该检查:
Did you write the name of your function correctly? Names are case sensitive. Did you install the package that contains the function? install.packages("thePackage") (this only needs to be done once) 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) Are you using an older R version where this function didn't exist yet? 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.
如果您不确定该函数位于哪个包中,您可以做一些事情。
如果你确定你安装并加载了正确的包,输入help.search("some.function")或??some。函数获取一个信息框,该信息框可以告诉您它包含在哪个包中。 find和getAnywhere也可以用来定位函数。 如果您对该包没有任何线索,您可以在此答案中解释的sos包中使用finfn。 RSiteSearch("some.function")或使用rdocumentation或rseek进行搜索是查找函数的替代方法。
有时您需要使用旧版本的R,但运行为新版本创建的代码。新添加的函数(如R 3.4.0中的hasName)将不会被发现。如果您使用较旧的R版本,并希望使用较新的函数,则可以使用包的反向端口来提供这些函数。您还可以在backports的git repo上找到需要反向移植的函数列表。请记住,R3.0.0之前的R版本与为R3.0.0及更高版本构建的包不兼容。
当计算机在我的控制下时,我通常可以解决这个问题,但当使用网格时,它更麻烦。当网格不是同质的时,可能不会安装所有的库,我的经验经常是由于没有安装依赖项而没有安装包。为了解决这个问题,我检查了以下内容:
Is Fortran installed? (Look for 'gfortran'.) This affects several major packages in R. Is Java installed? Are the Java class paths correct? 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. Check ldd results for R, to be sure about shared libraries 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. 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.) 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.
在多次遇到这种情况后,其中一些步骤变得相当常规。虽然第7条似乎是一个很好的起点,但这些都是按照我使用它们的频率大致列出的。
另一个问题是,在存在命名空间的情况下,您试图从包foo运行一个未导出的函数。
例如(我知道有些做作,但是):
> mod <- prcomp(USArrests, scale = TRUE)
> plot.prcomp(mod)
Error: could not find function "plot.prcomp"
首先,您不应该直接调用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>
所以我们现在可以直接调用它:
> stats:::plot.prcomp(mod)
我用过情节。Prcomp只是作为一个例子来说明目的。在正常使用中,不应该像这样调用S3方法。但正如我所说,如果您想调用的函数存在(例如,它可能是一个隐藏的实用程序函数),但在一个名称空间中,R将报告它无法找到该函数,除非您告诉它查找哪个名称空间。
将其与以下内容进行比较: 统计数据::plot.prcomp 上述方法失败了,因为while stats使用了情节。Prcomp,它不是从统计数据导出的错误正确地告诉我们:
错误:“阴谋。Prcomp不是从namespace:stats导出的对象
记录如下:
Pkg::name返回命名空间Pkg中导出的变量名的值,而Pkg:::name返回内部变量名的值。
如果在检查包(R CMD检查)时发生这种情况,请查看您的命名空间。
你可以通过在命名空间中添加以下语句来解决这个问题:
exportPattern("^[^\\\\.]")
这将导出所有不以点(".")开头的内容。这允许你有你的隐藏函数,从一个点开始:
.myHiddenFunction <- function(x) cat("my hidden function")
我犯了错误
错误:找不到函数
当我用RStudio做一个包的R CMD检查时发生。我发现
exportPattern(“。”)
命名空间文件成功了。作为旁注,我最初配置了RStudio,让它使用ROxygen来制作文档——并选择了ROxygen为我编写NAMESPACE文件的配置,这将不断擦除我的编辑。因此,在我的实例中,我从Roxygen配置中取消了NAMESPACE,并向NAMESPACE添加了exportPattern(“.”)来解决这个错误。
即使函数名是有效的,如果缺少一些强制性参数(即没有提供足够的参数),也会发生此错误。 我在Rcpp上下文中得到了这个,在那里我写了一个带有可选参数的c++函数,并且没有在R中提供这些参数。似乎来自c++的可选参数被R视为强制性的。因此,R无法为正确的名称找到匹配的函数,但参数数量不正确。
Rcpp函数:SEXP RcppFunction(arg1, arg2=0) {} R呼叫: RcppFunction(0)引发错误 RcppFunction(0,0)没有
如果您正在使用parallelMap,则需要将自定义函数导出到从属作业,否则将得到“无法找到函数”的错误。
如果在parallelStart上设置了一个非缺失的级别,则应该将相同的参数传递给parallelExport,否则将得到相同的错误。所以这一点应该严格遵守:
parallelStart(mode = "<your mode here>", N, level = "<task.level>")
parallelExport("<myfun>", level = "<task.level>")
我得到了同样的错误,我正在运行版本.99xxx,我从帮助菜单中检查更新并将我的RStudio更新到1.0x,然后错误没有出现
所以很简单的解决方法,就是更新你的R Studio
您可以通过函数调用的名称间距::来修复此错误
comparison.cloud(colors = c("red", "green"), max.words = 100)
to
wordcloud::comparison.cloud(colors = c("red", "green"), max.words = 100)