是否有R的requirements.txt之类的内容? [英] Is there something like requirements.txt for R?
问题描述
是否具有Python中的requirements.txt
之类的功能,您可以在其中存储用于文件的软件包列表,并且每当其他人想要运行您的程序并需要安装依赖项时,他们都可以执行pip install -r requirements.txt
.
Is there a functionality like requirements.txt
in Python, where you can store a list of packages used into a file, and whenever other people want to run your programs and need to install the dependencies, they can just do pip install -r requirements.txt
.
我认为,这在将R脚本部署到生产中时会很有帮助.如果没有这种功能,该如何复制呢?
I think, this helps a lot when deploying R script into production. If there is no such functionality, how do I replicate it?
推荐答案
根据注释,您可能需要研究构建一个程序包,并将需求包括在DESCRIPTION文件中.如果您正在谈论将.R脚本投入生产",则可以在开始时放置一个函数,以确保已安装所需的软件包.这是我自己的软件包中包含的内容,我可以在任何脚本的开头调用pkgLoad( <list of packages> )
,以确保已安装和加载了软件包.我提供了我最喜欢的软件包的列表,以便pkgLoad()
的调用会安装并加载我所有通常的可疑对象:
As per the comments, you might want to look at building a package, and including the requirements in the DESCRIPTION file. If you're talking about putting a .R script "into production", you can put a function at the start to make sure the packages required are installed. Here's something along those lines that I have in my own package, and I can call pkgLoad( <list of packages> )
at the beginning of any script to make sure the packages are installed and loaded. I include a list of my favourite packages, such that a call of pkgLoad()
installs and loads all my usual suspects:
pkgLoad <- function( packages = "favourites" ) {
if( length( packages ) == 1L && packages == "favourites" ) {
packages <- c( "data.table", "chron", "plyr", "dplyr", "shiny",
"shinyjs", "parallel", "devtools", "doMC", "utils",
"stats", "microbenchmark", "ggplot2", "readxl",
"feather", "googlesheets", "readr", "DT", "knitr",
"rmarkdown", "Rcpp"
)
}
packagecheck <- match( packages, utils::installed.packages()[,1] )
packagestoinstall <- packages[ is.na( packagecheck ) ]
if( length( packagestoinstall ) > 0L ) {
utils::install.packages( packagestoinstall,
repos = "http://cran.csiro.au"
)
} else {
print( "All requested packages already installed" )
}
for( package in packages ) {
suppressPackageStartupMessages(
library( package, character.only = TRUE, quietly = TRUE )
)
}
}
请注意,我已经在函数中内置了我最喜欢的CRAN镜像,因此请确保根据自己的需要进行编辑.
Note I've built my favourite CRAN mirror into the function, so make sure you edit that for your own needs.
这篇关于是否有R的requirements.txt之类的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!