r - 使用 R 包安装可执行脚本

标签 r deployment packages executable

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

我想知道R中是否也有这种可能性。鉴于 R 使用标准 Makefile,我想一定有办法做到这一点,尽管是非标准的。我已经知道我们可以在 R 脚本中读取命令行参数 using the docopt package .但是有没有办法在安装包时将脚本安装为可执行文件?

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

最佳答案

我有一个类似的目标,这是我确定的解决方案。我在我的 R 包中添加了一个函数,该函数通过并(详细)从 ~/.local/bin 创建符号链接(symbolic link)到我的包中的每个脚本exec文件夹。
其他合理的默认位置可能是 ~/.local/lib/R/bin~/bin , 但我喜欢 ~/.local/bin最好的。
所以安装包后,我指导用户运行

Rscript -e 'mypackage::install_executable_scripts()'
#' @export
install_executable_scripts <- function(into = "~/.local/bin", overwrite = FALSE) {
  scripts <- dir(system.file("exec", package = "mypackage"),
                 full.names = TRUE)
  if (!dir.exists(into)) dir.create(into)
  into <- normalizePath(into)

  dests <- file.path(normalizePath(into), basename(scripts))
  if (any(already_exist <- file.exists(dests))) {
    if (overwrite) {
      to_del <- dests[already_exist]
      cli::cat_bullet("Deleting existing file: ", to_del,
                      bullet_col = "red")
      unlink(to_del)
    } else {
      cli::cat_bullet(sprintf(
        "Skipping script '%s' because a file by that name already exists at the destination",
        basename(scripts[already_exist])))
      scripts <- scripts[!already_exist]
      dests   <-   dests[!already_exist]
    }
  }
  if (length(scripts)) {
    file.symlink(scripts, dests)
    cli::cat_line("Created symlinks:")
    cli::cat_bullet(dests, " ->\n    ", scripts, bullet_col = "green")
  } else
    cli::cat_line("Nothing installed")

  PATHS <- normalizePath(strsplit(Sys.getenv("PATH"), ":", fixed = TRUE)[[1]],
                         mustWork = FALSE)
  if(!into %in% PATHS)
    warning(sprintf("destination '%s' is not on the PATH", into))
}

关于r - 使用 R 包安装可执行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44566100/

相关文章:

r - 如何解决 rmarkdown 中的此错误消息?

scala - 为根包生成 scaladoc

r - 是否可以参数化 r 包版本?

r - 如何加入相似字符的数据集?

r - 如何按组用最新的非 NA 替换 NA?

r - 使用 group by 计算 R 中列中子字符串的出现次数

deployment - 如何在 Visual Studio Online 中复制任务组?

ruby - Capistrano部署 "the task staging does not exists"

c# - ClickOnce部署网站修改

r - 在包检查 R 2.14.0 期间使用带有小插图的 inst/extdata