使用R包安装可执行脚本。

时间:2021-12-06 13:52:40

In most scripting languages (e.g. Ruby, Python, etc.) the package manager (e.g. gem, pip, etc.) can install scripts as executables and link them to a directory referenced in the PATH variable (e.g. /usr/local/bin). This turns those executable scripts into shell commands that the user can run in an standalone manner and outside the programming interface.

在大多数脚本语言(例如Ruby、Python等)中,包管理器(例如gem、pip等)可以将脚本安装为可执行文件,并将它们链接到路径变量中引用的目录(例如/usr/local/bin)。这将这些可执行脚本转换为shell命令,用户可以在编程接口之外以独立的方式运行这些命令。

I wonder if there is such a possibility in R as well. Given that R uses standard Makefiles, I guess there must be a way to do so, albeit a non-standard one. I already know we can read command line arguments in a R script using the docopt package. But is there a way to install the script as an executable upon the installation of a package?

我想知道在R中是否也存在这样的可能性。考虑到R使用的是标准的makefile,我想一定有办法做到这一点,尽管不是标准的。我已经知道我们可以使用docopt包在R脚本中读取命令行参数。但是,在安装包时,是否有办法将脚本安装为可执行文件?

It would be great to have a lead on this topic, but a single working example from CRAN would suffice as well.

在这个主题上有一个领导是很好的,但是一个来自CRAN的工作示例也足够了。

1 个解决方案

#1


2  

Short (and very sad) answer: You cannot. But read on.

简短(非常悲伤)的回答:你不能。但读下去。

Reasoning: R will only ever write package content to its own .libPaths() directory (or the first in case several are given), or a directory given by the user.

推理:R只会将包的内容写入它自己的. libpath()目录(或者第一个是给定的),或者是用户给出的目录。

So, say, /usr/local/bin/ is simply out of reach. That is a defensible strategy.

因此,例如,/usr/local/bin/只是遥不可及。这是一种站得住脚的策略。

It is also rather sad--I wrote littler (also CRAN page) for exactly this purpose: executable R scripts. And we have dozens of those at work called from cron jobs. So what do we do? A one-time soft-link from the scripts/ subdirectory of the package containing the script to /usr/local/bin. At package upgrades, the link persists as a soft-link.

它也相当令人伤心——我为这个目的编写了littler(也是CRAN page):可执行的R脚本。我们的工作人员中有几十人来自克伦·乔布斯。那么我们该怎么办呢?来自包的脚本/子目录的一次性软链接,该包包含到/usr/ local/bin上的脚本。在包升级中,链接作为软链接持续存在。

And that's what I do for e.g. all the examples shipping with littler and more from other packages. Many of them use docopt too.

这就是我所做的,例如,所有的示例都是用little和more from other package。他们中的许多人也使用docopt。

#1


2  

Short (and very sad) answer: You cannot. But read on.

简短(非常悲伤)的回答:你不能。但读下去。

Reasoning: R will only ever write package content to its own .libPaths() directory (or the first in case several are given), or a directory given by the user.

推理:R只会将包的内容写入它自己的. libpath()目录(或者第一个是给定的),或者是用户给出的目录。

So, say, /usr/local/bin/ is simply out of reach. That is a defensible strategy.

因此,例如,/usr/local/bin/只是遥不可及。这是一种站得住脚的策略。

It is also rather sad--I wrote littler (also CRAN page) for exactly this purpose: executable R scripts. And we have dozens of those at work called from cron jobs. So what do we do? A one-time soft-link from the scripts/ subdirectory of the package containing the script to /usr/local/bin. At package upgrades, the link persists as a soft-link.

它也相当令人伤心——我为这个目的编写了littler(也是CRAN page):可执行的R脚本。我们的工作人员中有几十人来自克伦·乔布斯。那么我们该怎么办呢?来自包的脚本/子目录的一次性软链接,该包包含到/usr/ local/bin上的脚本。在包升级中,链接作为软链接持续存在。

And that's what I do for e.g. all the examples shipping with littler and more from other packages. Many of them use docopt too.

这就是我所做的,例如,所有的示例都是用little和more from other package。他们中的许多人也使用docopt。