-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Noam Ross
committed
Dec 24, 2023
1 parent
3e74d34
commit 20f511d
Showing
28 changed files
with
1,621 additions
and
2,131 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,87 @@ | ||
# Use the local user's .Rprofile when interactive. | ||
# Good for keeping local preferences and shortcuts, but not always reproducible. | ||
local({ | ||
user_rprof <- Sys.getenv("R_PROFILE_USER", file.path(Sys.getenv("HOME"), ".Rprofile")) | ||
if(interactive() && file.exists(user_rprof)) source(user_rprof) | ||
}) | ||
# The .Rprofile file is loaded at the start of every R session and can be used | ||
# to set common options and settings. As {targets} starts a new session for each | ||
# target, it should be limited to options and avoid loading packages or other | ||
# heavy tasks. Messages and diagnostics should be limited to interactive sessions. | ||
|
||
# ---- Function definitions ---------------------------------------------------- | ||
local({ # Wrapping in `local()` avoids side effects or cluttering the environment | ||
|
||
load_env <- function( | ||
env_files = list.files(all.files = TRUE, pattern = "^\\.env.*"), | ||
verbose = TRUE) { | ||
for (f in env_files) { | ||
tryCatch( | ||
readRenviron(f), | ||
error = function(e) { | ||
if (verbose) { | ||
if (isTRUE(suppressWarnings(readBin(f, "character", 2))[2] == "GITCRYPT")) { | ||
message("`", f, "` is encrypted, not loading. Run `git-crypt unlock`.") | ||
} else { | ||
message("Error reading ", f, ": ", e$message, ". Not loading.") | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
# ---- Interactive-only ------------------------------------------------------ | ||
if (interactive()) { | ||
# Source the user's .Rprofile if it exists, but other options will clobber it | ||
user_rprof <- Sys.getenv("R_PROFILE_USER", normalizePath("~/.Rprofile", mustWork = FALSE)) | ||
if (file.exists(user_rprof) && nzchar("USE_CAPSULE")) { | ||
source(user_rprof) | ||
} | ||
load_env(verbose = TRUE) | ||
|
||
options( | ||
renv.config.startup.quiet = FALSE, | ||
renv.config.synchronized.check = TRUE, | ||
renv.config.dependency.errors = "reported" | ||
) | ||
|
||
if (!nzchar("USE_CAPSULE") && requireNamespace("capsule", quietly = TRUE)) { | ||
capsule::whinge() | ||
} | ||
|
||
|
||
# Load .env environment variables | ||
if (file.exists(".env")) { | ||
if(isTRUE(suppressWarnings(readBin(".env", "character", 2))[2] == "GITCRYPT")) { | ||
message(".env file is encrypted, not reading. Use git-crypt unlock to decrypt") | ||
|
||
} else { | ||
try(readRenviron(".env")) | ||
# ---- Non-interactive only ------------------------------------------------ | ||
|
||
load_env(verbose = FALSE) | ||
options( | ||
renv.config.startup.quiet = TRUE, | ||
renv.config.synchronized.check = FALSE, # Avoid the check in every sub-process | ||
renv.config.dependency.errors = "ignored" | ||
) | ||
} | ||
} | ||
|
||
# ---- All sessions ---------------------------------------------------------- | ||
options( | ||
renv.config.install.verbose = TRUE # See what is happening when packages are installed | ||
) | ||
|
||
if (requireNamespace("renv", quietly = TRUE)) { | ||
renv::settings$ignored.packages(c("ehaproj", "tflow", "fnmate")) | ||
} | ||
|
||
if (!nzchar("USE_CAPSULE")) { | ||
source("renv/activate.R") | ||
load_env(verbose = FALSE) # Load again because renv can clobber env vars | ||
} | ||
|
||
|
||
}) | ||
|
||
options( | ||
repos = c(RSPM = "https://packagemanager.rstudio.com/all/latest", | ||
CRAN = "https://cran.rstudio.com/"), | ||
renv.config.auto.snapshot = TRUE, ## Attempt to keep renv.lock updated automatically | ||
tidyverse.quiet = TRUE | ||
) | ||
|
||
# Since RSPM does not provide Mac binaries, always install packages from CRAN | ||
# on mac or windows, even if renv.lock specifies they came from RSPM | ||
if (Sys.info()[["sysname"]] %in% c("Darwin", "Windows")) { | ||
options(renv.config.repos.override = c( | ||
CRAN = "https://cran.rstudio.com/", | ||
INLA = "https://inla.r-inla-download.org/R/testing")) | ||
} else if (Sys.info()[["sysname"]] == "Linux") { | ||
options(renv.config.repos.override = c( | ||
RSPM = "https://packagemanager.rstudio.com/all/latest", | ||
INLA = "https://inla.r-inla-download.org/R/testing")) | ||
# ---- Startup message --------------------------------------------------------- | ||
if (interactive() && Sys.getenv("RSTUDIO") == "1") { | ||
setHook("rstudio.sessionInit", function(newSession) { | ||
if (newSession) { | ||
cat("\014") | ||
cat("\033[38;5;196m▃▃▃▃▃▃\033[39m\033[38;5;214m▃▃▃▃▃▃\033[39m\033[38;5;226m▃▃▃▃▃▃\033[39m\033[38;5;34m▃▃▃▃▃▃\033[39m\033[38;5;27m▃▃▃▃▃▃\033[39m\033[38;5;91m▃▃▃▃▃▃\033[39m") | ||
cat("\n") | ||
} | ||
}, action = "append") | ||
} | ||
|
||
# Load the local library | ||
if(file.exists("renv/activate.R")) source("renv/activate.R") | ||
|
||
# If project packages have conflicts define them here, we start with common ones | ||
if(requireNamespace("conflicted", quietly = TRUE)) { | ||
conflicted::conflict_prefer("filter", "dplyr", quiet = TRUE) | ||
conflicted::conflict_prefer("count", "dplyr", quiet = TRUE) | ||
conflicted::conflict_prefer("select", "dplyr", quiet = TRUE) | ||
conflicted::conflict_prefer("set_names", "magrittr", quiet = TRUE) | ||
conflicted::conflict_prefer("View", "utils", quiet = TRUE) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
USE_TARGETS=1 |
This file contains 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
Oops, something went wrong.