Created
February 19, 2018 06:01
-
-
Save AliciaSchep/563b0515e2ce2448e4e3e8eef4207661 to your computer and use it in GitHub Desktop.
Setup use of a new, fresh library directory in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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