且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

R:如何在包中添加额外的功能?

更新时间:2023-02-18 12:26:04

此答案有两部分-首先是对您的问题的一般性答案,其次是针对您所引用的特定功能的特定答案,其中的问题有点不同.

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

当心-使用':::'后果自负!

Beware -- use ':::' at your own risk!

在代码中使用:::通常是设计错误,因为 相应的对象可能已经保存好了 原因.如果您觉得有问题,请考虑与软件包维护者联系. 除了检查以外,需要访问该对象.

It is typically a design mistake to use ::: in your code since the corresponding object has probably been kept internal for a good reason. Consider contacting the package maintainer if you feel the need to access the object for anything but mere inspection.

但是对于您正在做的事情,就从您自己使用的同一个软件包中临时破解另一个功能而言,这些警告应该可以忽略不计(当然,后果自负-如手册中所述)

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.