问题描述
我想使用R脚本向其他人编写的包中添加一个经过特殊修改的函数,即仅用于会话,而不是永久使用.举例来说,具体示例是将bls_map_county2()
添加到blscrapeR包中. bls_map_county2
只是bls_map_county()
函数的副本,带有添加的...
自变量,目的是更改一些地图绘制参数.我尚未插入其他参数.按原样运行该函数,出现错误:
I would like to add an idiosyncratically modified function to package written by someone else, with an R Script, i.e. just for the session, not permanently. The specific example is, let's say, bls_map_county2()
added to the blscrapeR package. bls_map_county2
is just a copy of the bls_map_county()
function with an added ...
argument, for purposes of changing a few of the map drawing parameters. I have not yet inserted the additional parameters. Running the function as-is, I get the error:
我认为这是因为我的函数没有指向blscrapeR
命名空间.我如何将我的功能分配给(已安装,已加载的)blscrapeR
名称空间,还有什么我需要做的事情才能让它从所需的软件包中访问任何机器?
I assume this is because my function does not point to the blscrapeR
namespace. How do I assign my function to the (installed, loaded) blscrapeR
namespace, and is there anything else I need to do to let it access whatever machinery from the package it requires?
推荐答案
此答案有两部分-首先是对您的问题的一般性答案,其次是针对您所引用的特定功能的特定答案,其中的问题有点不同.
There are two parts to this answer - first a generic answer to your question, and 2nd a specific answer for the particular function that you reference, in which the problem is something slightly different.
由于已经加载了程序包名称空间,因此您应该已经可以访问它,因此只有未导出的功能才会给您带来问题.
You should already have access to the package namespace, since you loaded it, so it is only the unexported functions that will give you issues.
我通常只是将包名称的:::
运算符放在非导出函数的前面.即,找到对some_internal_function()
的调用的每个实例,然后将其替换为PackageName:::some_internal_function()
.如果您正在编辑的函数中调用了多个不同的内部函数,则可能需要对每个有问题的函数调用重复执行几次.
I usually just prepend the package name with the :::
operator to the non exported functions. I.e., find every instance of a call to some_internal_function()
, and replace it with PackageName:::some_internal_function()
. If there are several different internal functions called within the function you are editing, you may need to do this a few times for each of the offending function calls.
:::
的帮助页面确实包含这些警告
The help page for :::
does contain these warnings
和
但是对于您正在做的事情,就从您自己使用的同一个软件包中临时破解另一个功能而言,这些警告应该可以忽略不计(当然,后果自负-如手册中所述)
But for what you are doing, in terms of temporarily hacking another function from the same package for your own use, these warnings should be safe to ignore (at you own risk, of course - as it says in the manual)
在这种情况下,令人反感的行是
The offending line in this case is
ggplot2::ggplot() + geom_map(...
其中包编写者已经为ggplot()
指定了ggplot2命名空间,但是却忘记了geom_map()
这样做,它也是ggplot2的一部分(并且 not 是blscrapeR的内部函数).
in which the package writers have specified the ggplot2 namespace for ggplot()
, but forgotten to do so for geom_map()
which is also part of ggplot2 (and not an internal function in blscrapeR ).
在这种情况下,只需加载ggplot2,就可以了.
In this case, just load ggplot2, and you should be good to go.
您还可以考虑与软件包维护者联系,以告知他们该错误.
You may also consider contacting the package maintainer to inform them of this error.
这篇关于R:如何在包中添加额外的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!