|
| 1 | +local({ |
| 2 | + rv_info <- system2("rv", c("info", "--library", "--r-version", "--repositories"), stdout = TRUE) |
| 3 | + if (!is.null(attr(rv_info, "status"))) { |
| 4 | + # if system2 fails it'll add a status attribute with the error code |
| 5 | + warning("failed to run rv info, check your console for messages") |
| 6 | + } else { |
| 7 | + # extract library, r-version, and repositories from rv |
| 8 | + rv_lib <- sub("library: (.+)", "\\1", grep("^library:", rv_info, value = TRUE)) |
| 9 | + rv_r_ver <- sub("r-version: (.+)", "\\1", grep("^r-version:", rv_info, value = TRUE)) |
| 10 | + repo_str <- sub("repositories: ", "", grep("^repositories:", rv_info, value = TRUE)) |
| 11 | + repo_entries <- gsub("[()]", "", strsplit(repo_str, "), (", fixed = TRUE)[[1]]) |
| 12 | + repo_list <- trimws(sub(".*, ", "", repo_entries)) # Extract URL |
| 13 | + names(repo_list) <- trimws(sub(", .*", "", repo_entries)) # Extract Name |
| 14 | + # this might not yet exist, so we'll normalize it but not force it to exist |
| 15 | + # and we create it below as needed |
| 16 | + rv_lib <- normalizePath(rv_lib, mustWork = FALSE) |
| 17 | + if (!dir.exists(rv_lib)) { |
| 18 | + message("creating rv library: ", rv_lib) |
| 19 | + dir.create(rv_lib, recursive = TRUE) |
| 20 | + } |
| 21 | + .libPaths(rv_lib, include.site = FALSE) |
| 22 | + options(repos = repo_list) |
| 23 | + |
| 24 | + if (interactive()) { |
| 25 | + message("rv libpaths active!\nlibrary paths: \n", paste0(" ", .libPaths(), collapse = "\n"), "\n") |
| 26 | + message("rv repositories active!\nrepositories: \n", paste0(" ", names(getOption("repos")), ": ", getOption("repos"), collapse = "\n")) |
| 27 | + sys_r <- sprintf("%s.%s", R.version$major, R.version$minor) |
| 28 | + if (!grepl(paste0("^", rv_r_ver), sys_r)) { |
| 29 | + message(sprintf("\nWARNING: R version specified in config (%s) does not match session version (%s)", rv_r_ver, sys_r)) |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | +}) |
0 commit comments