我也有类似的目标,这是我确定的解决方案。我在我的 R 包中添加了一个函数,该函数通过并(详细地)创建从 ~/.local/bin 到我的包 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))
}