Skip to content

Instantly share code, notes, and snippets.

@AliciaSchep
Created February 19, 2018 06:01
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 AliciaSchep/563b0515e2ce2448e4e3e8eef4207661 to your computer and use it in GitHub Desktop.
Save AliciaSchep/563b0515e2ce2448e4e3e8eef4207661 to your computer and use it in GitHub Desktop.
Setup use of a new, fresh library directory in R
create_lib_dir <- function(name, path = "."){
dir_path <- file.path(path, name)
if (!dir.exists(dir_path)) {
message("Creating new dir: ", dir_path)
dir.create(dir_path)
}
normalizePath(dir_path)
}
use_local_library <- function(dirname = "libs", dirpath = "."){
full_dir_path <- create_lib_dir(dirname, dirpath)
if (!file.exists(".Renviron")){
message("Creating .Renviron file")
file.create(".Renviron")
}
new_line <- paste0("R_LIBS_USER = \"",full_dir_path, "\"")
current_lines <- readLines(".Renviron")
if (!(new_line %in% current_lines)){
message("Adding line to .Renviron: ", new_line)
con <- file(".Renviron", encoding = "utf-8")
on.exit(close(con), add = TRUE)
cat(new_line, file = con, sep = "")
}
message("Restart R to take into effect.")
invisible(TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment