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

1

53

In Python, you can have a file requirements.txt 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. Is there also something like that for R? If there is no such functionality, how do I replicate it?

Anglophobe answered 13/8, 2016 at 1:22 Comment(5)
Cannot tell what you are asking (and I'm not one of hte downvoters yet). Deployment is usually done with a package. Could be what is in a package DESCRIPTION file or what is in your .Rprofile file.Cosignatory
try using packratColonist
@MrFlick functionality of requirements.txt is to store all the packages needed for a project in a file so that you can download them and avoid running the script and getting Error in library(xxx) : there is no package called ‘xxx’.Septate
My attempt to reproduce something like requirements.txt for R: gist.github.com/cannin/6b8c68e7db19c4902459Tasse
I wonder whether the functionality of requirements.txt is offered in Python or pip? I think it's the latter.Sciatica
H
12

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.

Hoppe answered 13/8, 2016 at 2:41 Comment(2)
In typical American fashion, I confused the domain extension of Australia for Austria, and wondered why you weren't downloading from the nearest mirror, especially since it is CRAN's central mirror.Hornstein
install.packages should probably be substituted by devtools::install_version, because through requirements.txt one can not only install the necessary packages but also specific versions of them.Sciatica

© 2022 - 2024 — McMap. All rights reserved.