-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rprofile
156 lines (116 loc) · 3.13 KB
/
.Rprofile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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" ,"magrittr", "ragg", "renv", "stats", "stringr"))
# 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())