Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like requirements.txt for R? [closed]

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.

I think, this helps a lot when deploying R script into production. If there is no such functionality, how do I replicate it?

like image 400
hans-t Avatar asked Aug 13 '16 01:08

hans-t


People also ask

What is in requirements txt?

The requirements. txt is a file listing all the dependencies for a specific Python project. It may also contain dependencies of dependencies, as discussed previously. The listed entries can be pinned or non-pinned.

What is PIP install flag?

The --user flag to pip install tells Pip to install packages in some specific directories within your home directory. This is a good way to have your own default Python environment that adds to the packages within your system directories, and therefore, does not affect the system Python installation.


1 Answers

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 )         )     }  } 

Note I've built my favourite CRAN mirror into the function, so make sure you edit that for your own needs.

like image 130
rosscova Avatar answered Sep 23 '22 04:09

rosscova