-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rprofile
142 lines (118 loc) · 5.33 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
#
# .Rprofile
#
# Copyright (C) 2019 James Joseph Balamuta <balamut2@illinois.edu>
#
# Version 2.5.1 -- 09/26/19
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
###############################################################################
# Disabling package installation when using R GUI or RStudio IDE.
#
# On R startup inside of either the R GUI or RStudio IDE, the settings provided
# within this Rprofile are loaded. In particular, the Rprofile setups
# intercepts for `install.packages`, disables CRAN, and displays a startup
# message letting users know packages are pre-installed.
#
# There is a companion .Renviron file that sets specific RStudio environment
# variables to further disable the environment configuration.
###############################################################################
.First = function() {
## Local CRAN ----
## Initialize an empty local CRAN repository.
## Note: In this setup, we are not placing package
## sources or binaries into this location.
local_user_cran = file.path(Sys.getenv("HOME"), "cran")
dir.create(local_user_cran, showWarnings = FALSE)
## Set the location of packages to the empty local CRAN.
## Ensure that the appropriate RStudio Secure warning is handled.
options(repos = c("CRAN" = paste0("file://", local_user_cran)))
# Exit if a human isn't present (e.g. run from an Rscript / R CMD)
if (!interactive()) {
return()
}
## CBTF Help Messages ----
## Hard coded help documentation location
cbtf_help_url = "https://cbtf.engr.illinois.edu/for-students/software-RStudio.html"
## Open the URL for students to view help documentation
help_cbtf = function(url = cbtf_help_url) {
message("Opening help documentation at: ", url)
utils::browseURL(url)
}
## Place the help function into the global environment
base::assign("help_cbtf", help_cbtf, envir = globalenv())
## Provide startup messages in red text to indicate the status of
## package installation and environment specific documentation
wrap_msg = function(x) {
message(paste(strwrap(x), collapse = "\n"))
}
cbtf_disabled_cran_msg = function() {
message("Note: ")
wrap_msg("Installing packages from CRAN is disabled.")
message(
paste0(c("Packages have been pre-installed for:", "STAT 385, STAT 430 DSPM, STAT 432, and CEE 202\n"), collapse="\n")
)
}
cbtf_documentation_msg = function() {
wrap_msg("For help with R and RStudio in the CBTF, please consult the help guide by using the `help_cbtf()` function.")
}
cbtf_welcome_msg = function() {
cbtf_disabled_cran_msg()
cbtf_documentation_msg()
}
## Disable install.packages ----
## Override an R function in a package.
shim_pkg_func = function(name, pkgname, value) {
## Ensure package is loaded.
## If the package is not on the search path, we cannot modify it!
pkg_env_name = paste0("package:", pkgname)
if (!pkg_env_name %in% search()) {
# Load the library
library(pkgname, character.only = TRUE)
}
# Retrieve package environment
env = as.environment(pkg_env_name)
#pkg_ns_env = asNamespace(pkgname)
# Unlock environment where the function/variable is found.
base::unlockBinding(name, env)
# Make the assignment into the environment with the new value
base::assign(name, value, envir = env)
# Close the environment
base::lockBinding(name, env)
}
## Provide an alternative install.packages(...) routine
install_packages_shim = function(...) { cbtf_disabled_cran_msg() }
## Setup a shim to override the install.packages() function
if(Sys.getenv("RSTUDIO") == "1") {
## Establishes a delayed registration of the shim after
## RStudio's startup procedure occurs. This procedure overwrites settings
## sometimes established in .Rprofile. Thus, we need a delayed
## component to ensure we're preventing
## install.packages() from being delayed
## c.f. https://github.com/rstudio/rstudio/issues/1579#issuecomment-495706255
setHook("rstudio.sessionInit", function(newSession) {
if (newSession)
shim_pkg_func("install.packages", "utils", install_packages_shim)
}, action = "append")
} else {
## Establish the shim for _R_ terminal sessions or R GUI.
## Attempting to establish the hook with RStudio would result
## in its initialization procedure overwrite this shim.
shim_pkg_func("install.packages", "utils", install_packages_shim)
}
## Display the welcome bumper
cbtf_welcome_msg()
}