-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathinit.R
More file actions
59 lines (48 loc) · 1.65 KB
/
init.R
File metadata and controls
59 lines (48 loc) · 1.65 KB
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
# This file is executed with its containing directory as wd
# Remember the working directory (should be extension subfolder that contains this script)
dir_init <- getwd()
# This function is run at the beginning of R's startup sequence
# Code that is meant to be run at the end of the startup should go in `init_last`
init_first <- function() {
# return early if not a vscode term session
if (
!interactive() ||
Sys.getenv("RSTUDIO") != "" ||
Sys.getenv("TERM_PROGRAM") != "vscode"
) {
return()
}
# check required packages
required_packages <- c("jsonlite", "rlang", "readr")
missing_packages <- required_packages[
!vapply(required_packages, requireNamespace,
logical(1L),
quietly = TRUE
)
]
if (length(missing_packages)) {
message(
"VSCode R Session Watcher requires ",
toString(missing_packages), ". ",
"Please install manually in order to use VSCode-R."
)
} else {
# Initialize vsc utils after loading other default packages
assign(".First.sys", init_last, envir = globalenv())
}
}
old.First.sys <- .First.sys
# Overwrite for `.First.sys`
# Is used to make sure that all default packages are loaded first
# Will be assigned to and called from the global environment,
# Will be run with wd being the user's working directory (!)
init_last <- function() {
old.First.sys()
source(file.path(dir_init, "init_late.R"), chdir = TRUE)
# remove this function from globalenv()
suppressWarnings(
rm(".First.sys", envir = globalenv())
)
invisible()
}
init_first()