Skip to content

Instantly share code, notes, and snippets.

@danielvartan
Last active February 22, 2025 04:49
Show Gist options
  • Save danielvartan/2f7acf1ba6a3de8cf14d22da8b41fa6b to your computer and use it in GitHub Desktop.
Save danielvartan/2f7acf1ba6a3de8cf14d22da8b41fa6b to your computer and use it in GitHub Desktop.
My `.Rprofile` settings.
# Set session ID -----
session_id <- Sys.time() |> as.character()
# Load functions -----
single_quote <- function(x) paste0("'", x, "'")
double_quote <- function(x) paste0('"', x, '"')
require_pkg <- function(...) {
out <- list(...)
if (length(out) == 0) stop("'...' cannot be empty.")
lapply(
X = out,
FUN = function(x, pattern) grepl(pattern, x),
pattern = "^[A-Za-z][A-Za-z0-9.]+[A-Za-z0-9]$"
)
if (!identical(unique(unlist(out)), unlist(out))) {
stop("'...' cannot have duplicated values.")
}
pkg <- unlist(out)
namespace <- vapply(
X = pkg,
FUN = requireNamespace,
FUN.VALUE = logical(1),
quietly = TRUE,
USE.NAMES = FALSE
)
if (!all(namespace, na.rm = TRUE)) {
pkg <- pkg[!namespace]
if (length(pkg) > 1) {
str_1 <- "packages"
str_2 <- "them"
str_3 <- "c("
str_4 <- ")"
str_5 <- "all"
} else {
str_1 <- "package"
str_2 <- "it"
str_3 <- ""
str_4 <- ""
str_5 <- "the"
}
stop(
paste0(
"\n\n",
"'.Rprofile' requires the following ", str_1, " to run:", "\n\n",
paste0(single_quote(pkg), collapse = " "), "\n\n",
"You can install ", str_2, " by running:", "\n\n",
"install.packages(", str_3,
paste(double_quote(pkg), collapse = ", "),
str_4, ")", "\n\n",
"Restart R (Ctrl+Shift+F10) after installing ", str_5,
" required ", str_1, "."
),
call. = FALSE
)
}
invisible()
}
rprofile_cat_line <- function(session_id, env_var = "RPROFILE_MESSAGES") {
if (Sys.getenv(env_var) == session_id) cat("\n")
invisible()
}
rprofile_message <- function(
x,
session_id,
env_var = "RPROFILE_MESSAGES",
cat_line = TRUE,
info = FALSE
) {
if (Sys.getenv(env_var) %in% c("", session_id)) {
do.call(
what = Sys.setenv,
args =
list(session_id) |>
magrittr::set_names(env_var)
)
if (isTRUE(info)) {
cli::cli_alert_info(x, wrap = TRUE)
} else {
cli::cli_alert(x, wrap = TRUE)
}
if (isTRUE(cat_line)) cat("\n")
}
invisible()
}
# Assert required packages -----
require_pkg(
c(
"cli", "here", "httpgd", "magrittr", "ragg", "renv", "stats", "stringr",
"vscDebugger"
)
)
# Load packages -----
library(magrittr)
library(ragg)
# Show session message -----
rprofile_message(
x = "The messages below are shown only once per R session.",
session_id = session_id,
info = TRUE
)
# Activate `renv` -----
rprofile_message("`renv` activation settings:", session_id)
source(here::here("renv", "activate.R"))
rprofile_cat_line(session_id)
# Set options -----
options(scipen = 999)
# Warn about setting 'AGG' as the graphic device backend -----
rprofile_message(
paste0(
"If you haven't already set it, configure {.strong AGG} ",
"as the RStudio graphic device backend. Learn more at ",
"<https://ragg.r-lib.org/#use-ragg-in-rstudio>."
),
session_id
)
# Set system locale -----
source(here::here("R", "set_locale.R"))
set_locale(session_id)
# End line -----
rprofile_cat_line(session_id = session_id, env_var = "SET_LOCALE_MESSAGES")
# Clean the global environment -----
rm(list = ls())
# # For testing
#
# Sys.setlocale(locale = "pt_BR.utf8")
# set_locale()
# get_locale()
# Sys.getlocale()
# Sys.getenv()
# Sys.getenv("LC_TIME")
# library(cli)
# library(stats)
# library(stringr)
set_locale <- function(session_id = Sys.time() |> as.character()) {
locale_values <- c(
"en_US.utf8", "en_US.UTF-8", "en_US", "en-US", "en", "English_United States"
)
# "LC_NAME", "LC_ADDRESS", "LC_TELEPHONE" == "invalid 'category' argument"
locale_keys <-
.LC.categories |>
stringr::str_subset("LC_NUMERIC", negate = TRUE)
env_keys <- c(
.LC.categories[-1], "LC_NAME", "LC_ADDRESS", "LC_TELEPHONE",
"LC_IDENTIFICATION"
)
# Sys.setlocale("LC_NUMERIC", "C")
for (i in locale_values) {
test <- Sys.setlocale(locale = i) |> suppressWarnings()
if (!test == "" && Sys.getlocale("LC_TIME") == i) {
for (j in locale_keys) Sys.setlocale(j, i)
for (j in env_keys) do.call(Sys.setenv, as.list(stats::setNames(i, j)))
Sys.setenv(LANG = "en")
Sys.setenv(LANGUAGE = "en")
break
}
}
if (test == "" || !Sys.getlocale("LC_TIME") %in% locale_values) {
cli::cli_alert_danger(
paste0(
"An error occurred while attempting to set the system locale to English ",
"({.strong Sys.setlocale(locale = 'en_US.utf8')}). ",
"Errors related to locale values are likely caused by differences in ",
"how operating systems handle locale settings. ",
"Run {.strong {cli::col_blue('?Sys.setlocale')}} to investigate ",
"the issue."
),
wrap = TRUE
)
}
if (Sys.getenv("SET_LOCALE_MESSAGES") == "") {
Sys.setenv(SET_LOCALE_MESSAGES = session_id)
get_locale()
}
invisible()
}
# library(cli)
get_locale <- function() {
cli::cli_alert(
paste0(
"The current system locale is:\n\n",
"{Sys.getlocale()}\n\n"
),
wrap = TRUE
)
env_keys <- c(
.LC.categories[-1], "LC_NAME", "LC_ADDRESS", "LC_TELEPHONE",
"LC_IDENTIFICATION"
)
cat("\n")
env_values <- character()
for (i in env_keys) {
env_values <- c(env_values, paste0(i, "=", Sys.getenv(i)))
}
env_values <- paste(env_values, collapse = ";")
cli::cli_alert(
paste0(
"The current values for the locale environment variables are:\n\n",
"{env_values}"
),
wrap = TRUE
)
cat("\n")
cli::cli_alert(
paste0(
"The system locale needs to be set to {.strong English} to ensure ",
"proper functionality ({.strong C} is ok too)."
),
wrap = TRUE
)
invisible()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment