Sample R Configurations

Sample R configuration, including how to install packages persistently.
Author

Lars Vilhuber

Single configuration file with relative paths

  • Encouraged:
# Use of file.path() for platform neutrality
basepath <- file.path("My Documents","Paper 15")
# alternatively, if using Git
basepath =  rprojroot::find_root(rprojroot::is_git_root)

includes <- file.path(basepath,"includes")
inputdata <- file.path(basepath,"inputdata")
tables <- file.path(basepath,"text/tables")
matsize <- 11000

Use of relative paths in main program for inclusions

Discouraged:

source("C:\My Documents\yesterdays programs\submodule.do")
  • Encouraged:
source(file.path(includes,"submodule.R"))

or

source(file.path("includes","submodule.R")

Installing packages in a project-specific directory

# Install packages locally
.libPaths( c( includes , .libPaths() ) )

You can then create a single dependency file that is run before all others:

00_install.R:
# run this only when updating the packages 
# all packages necessary to run the main.R have 
# been included in this replication package 
include "config.R"
install.packages('rddensity', lib=libpath, destdir=libpath)
install.packages('rdrobust', lib=libpath, destdir=libpath) 
install.packages('gridExtra', lib=libpath, destdir=libpath)
install.packages('ggplot2', lib=libpath, destdir=libpath)

and in your main program

basepath =  rprojroot::find_root(rprojroot::is_git_root)
source(file.path(basepath,"config.R"))
library(rdrobust)
library(rddensity)
library(gridExtra)
library(ggplot2)

will always work, because it no longer requires download, and fixes the version of the packages used.

Installing specific package versions

Using remotes package:

remotes::install_version("rdrobust", version = "2023.10.1", lib=libpath)
remotes::install_version("rddensity", version = "2023.10.1", lib=libpath)
remotes::install_version("gridExtra", version = "2.3", lib=libpath)