Skip to content

Instantly share code, notes, and snippets.

@logworthy logworthy/RSetup.R
Last active Mar 1, 2018

What would you like to do?
R Setup
# To get setup quickly just run the below line:
# (note that it changes with revisions to the file... will try to keep it up to date)
# source('')
# set up R with desired pacakges and load them by default
# install packages
packages_required <- c(
# check if any packages are installed
i <- installed.packages()
already_installed <- i[i[,'Package'] %in% packages_required, 'Package']
to_install <- setdiff(packages_required, already_installed)
if(length(to_install) > 0) install.packages(to_install)
# find renviron
R_PATH <- file.path(R.home("bin"), "R")
R_ENVIRON.SITE <- file.path(R_PATH, '../../../etc/')
# check if default packages is specified <- readLines(R_ENVIRON.SITE)
default_idx <- grep('^\\s*R_DEFAULT_PACKAGES\\s*=.*$',
# find important installed pacakges
i <- installed.packages()
base_packages <- i[ i[,"Priority"] %in% c("base"), c("Package")]
# replace R_DEFAULT_PACKAGES string
all_packages <- unique(c(base_packages, packages_required))
package_string <- paste(all_packages, collapse=',')
if(length(default_idx) != 0) {[default_idx] <- sprintf("R_DEFAULT_PACKAGES='%s'", package_string)
} else { <- c(, sprintf("R_DEFAULT_PACKAGES='%s'", package_string))
# write to file
writeLines(, R_ENVIRON.SITE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.