Skip to content

Instantly share code, notes, and snippets.

@logworthy
Last active March 1, 2018 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logworthy/695f037e79ca4a2c49f2bdbe3502a0c5 to your computer and use it in GitHub Desktop.
Save logworthy/695f037e79ca4a2c49f2bdbe3502a0c5 to your computer and use it in GitHub Desktop.
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('https://gist.githubusercontent.com/logworthy/695f037e79ca4a2c49f2bdbe3502a0c5/raw/459be6d79c7e2762cae9b5c981a18dfde24da4ba/RSetup.R')
# set up R with desired pacakges and load them by default
# install packages
packages_required <- c(
'data.table',
'RODBC',
'ggplot2',
'tidyr',
'plyr',
'dplyr',
'lubridate',
'stringr',
'xlsx'
)
# 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/Renviron.site')
# check if default packages is specified
r_environ.site.contents <- readLines(R_ENVIRON.SITE)
default_idx <- grep('^\\s*R_DEFAULT_PACKAGES\\s*=.*$', r_environ.site.contents)
# 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) {
r_environ.site.contents[default_idx] <- sprintf("R_DEFAULT_PACKAGES='%s'", package_string)
} else {
r_environ.site.contents <- c(r_environ.site.contents, sprintf("R_DEFAULT_PACKAGES='%s'", package_string))
}
# write to file
writeLines(r_environ.site.contents, R_ENVIRON.SITE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment