关于R包发布和代码可见性的问题

Questions about R package publishing and code visibility

当我在 R 中使用包时,我会安装它并在加载时使用它。现在如果我添加一个使用另一个包的包怎么办?这个包是否也自动下载和加载?还是一般禁止 R 包使用另一个包?我不这么认为。

假设我想发布一个 R 包。在我的代码中,我可以使用其他包中的函数并安装和加载这些包吗?或者当我需要其他包的功能时,它是如何工作的?我是否必须实现一条消息,说明需要这个和那个包,用户必须先安装和加载它,我需要实现错误捕获功能,以防在 pc 系统上找不到该包?

当我想发布 R 包时,我可以 use/call Java 我的 package/code 中的代码吗?

对于已经发布的包 - 让我们以 fGarch 包为例 - 我希望看到完整的代码。我怎么能看到这个?我知道 R 是开源的,我认为只输入一个空函数并显示代码或多或少是可能的,但有时这是行不通的,尤其是我的问题是:有没有一种方法可以查看整个函数包裹代码?

对于已经发布的包,是否可以查看和查看所有提交的文件?所以像 git 这样的存储库,其中提交了所有文件 - 代码本身和其他需要的文件,如描述文件或其他 - 我可以看到这些文件并查看它们?

此外,关于 和隐藏函数:R 包中是否有我作为最终用户看不到的代码?这也涉及到我之前的问题,我如何或通过哪种方式可以看到 R 包中的整个代码?

我猜你有几个不同的问题。让我们按照您要求的顺序来接受他们:

What if I add a package which uses another package? Is this package automatically downloaded and loaded too? Or is it in general forbidden for a R package to use another package?

当然不禁止一个R包使用另一个R包。事实上,大多数 R 包 依赖 其他包。

每个 R 包的源代码必须在根目录中包含一个基于文本的 DESCRIPTION 文件。在此文件中,您会发现(除其他事项外)“依赖”字段和“导入”字段。这两个字段一起列出所有其他包 required 以使用 this 包。如果用户还没有在他们的本地库中安装这些其他包,R 将在安装请求的包时自动安装它们。

如果您的包在“Depends”中列出了依赖项,那么无论何时附加您的包,都会附加依赖项包。因此,如果您查看名为“foo”的包的源代码,您会看到它的 DESCRIPTION 文件包含行

Depends: bar,

您知道当您在 R 控制台中调用 library(foo) 时,您实际上已经完成了 library(bar); library(foo)

这并不总是理想的。包 foo 可能只需要包 bar 中的几个函数,而 bar 可能包含一些其他函数,这些函数的名称可能与其他常用函数冲突。因此,一般来说,如果您正在编写一个包并且您只想使用另一个包中的几个函数,最好使用“导入”而不是“依赖”来限制添加到用户的不必要符号的数量搜索路径。


Suppose I want to publish a R package. Within my code, can I use functions from other packages and install and load these packages

是的,您可以使用其他包中的函数。最简单的方法是在 DESCRIPTION 文件的 Depends 字段中包含包的名称。

但是,如果只在您自己的包中使用其他包中的几个函数,最佳做法是使用 DESCRIPTION 文件中的“Imports”字段,并为导入的函数使用名称空间限定符您实际的 R 代码。例如,如果您想使用 ggplot2 包中的 ggplot,那么在您的函数中您可以将其称为 ggplot2::ggplot 而不仅仅是 ggplot.

如果您发布包供其他人使用,如果用户使用默认设置调用 install.packages,依赖项将与您的包一起自动安装。例如,当我这样做时:

install.packages("fGarch")

我收到相关消息:

#> also installing the dependencies ‘timeSeries’, ‘fBasics’, ‘fastICA’

Do I have to implement a message that this and that package is needed and that the user has to install and load it prior to it and I need to implement error catching functions in case the package cannot be found on the pc system?

不,一般情况下不会。只要您在 DESCRIPTION 文件中列出了正确的包,R 就会处理这个问题。


When I want to publish a R package, can I use/call Java code within my package/code?

R 没有原生 Java API,但您可以通过 rJava package 使用您自己的 Java 代码,您可以将其列为对你的包裹。但是,有些用户很难从 Java 升级到 运行,例如可能使用 R 但未安装 Java 且没有安装管理员权限的商业和学术用户它,所以这是写包时要记住的事情。


For a package which was already published - so let's take just as an example the fGarch package - I would like to see the complete code. How can I see this?

每个可从 CRAN 下载的包都有其源代码。在 fGarch 的情况下,它是 CRAN page contains a link to the gzipped tarball of the source code. You can download this and use untar in R to review all the source code. Alternatively, many packages will have an easily-found repository on Github or other source-control sites where you can examine the source code via a browser. For example, you can browse the fGarch source on Github here.


For a package which was already published, is it possible to see and look into all files which were submitted? So like a repository as git where all files are submitted - the code itself and further files which are needed like description files or whatever - and I can see these files and look into them?

是的,您可以在非官方 Github CRAN 镜像 here

查看 Github 上上传到 CRAN 的所有包的所有源文件

Is there code in a R package which I cannot see as an end user? This refers also to my previous question, how can I or which way can I see the whole code in a R package?

如上所述,您可以通过 CRAN 或 Github 获取任何包的源代码。如您所说,您只需在 R 中键入该函数的名称即可查看 exported 函数的源代码。对于未导出的函数,您可以使用三个冒号执行相同的操作。例如,ggplot2:::adjust_breaks 允许您从 ggplot2 中查看未导出函数 adjust_breaks 的函数体。当使用像 S4、ggproto 或 R6 这样的面向对象系统时,或者当源代码包含编译的 C 或 C++ 代码时,会有一些复杂性,但我还没有遇到过无法找到的情况一两分钟后使用 R 控制台和良好的搜索引擎获得相关源代码。