/
zzz.R
351 lines (285 loc) · 9.69 KB
/
zzz.R
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#' Get RAVE version
#' @export
rave_version <- function(){
as.character(utils::packageVersion('rave'))
}
latest_version <- function() {
tryCatch({
suppressWarnings({
versions <- raveio::load_json("https://beauchamplab.r-universe.dev/packages/rave")
return(list(
version = versions$Version[[1]],
built = versions$Built$Date[[1]]
))
})
}, error = function(e){
NULL
})
}
.onLoad <- function(libname, pkgname){
try({
# get rave_version
old_ver <- rave_options('rave_ver')
old_ver %?<-% rave_hist()$get_or_save('..rave_ver..', '0.0.0.0000')
new_ver <- rave_version()
is_newer <- tryCatch({
is_newer <- utils::compareVersion(old_ver, new_ver) < 0
if(!length(is_newer) || !is.logical(is_newer)){
is_newer <- TRUE
}
is_newer
}, error = function(e){
return(TRUE)
})
if(is_newer){
rave_hist()$save(
'..rave_startup_msg..' =
sprintf('RAVE %s ==> %s. Module information has been updated.',
old_ver, new_ver)
)
# New RAVE installed! update
# 3. Data files
has_data <- arrange_data_dir(TRUE)
rave_hist()$save('..rave_ver..' = new_ver)
# 1. additional settings
rave_options(
delay_input = 20,
max_worker = future::availableCores() - 1,
# crayon_enabled = TRUE,
rave_ver = new_ver
)
}else{
# has_data = arrange_data_dir(FALSE)
}
save_options()
})
}
.onUnload <- function(libpath){
clear_env(data_repository)
}
restart_r <- function(){
f <- get0(".rs.restartR")
if (is.function(f)) {
message("Restarting RStudio rsession. Might take a while. Please wait...")
f()
return(invisible())
}
message("Using startup::restart()")
startup::restart()
return(invisible())
}
#' @title Check and Install RAVE Dependencies
#' @param update_rave logical, whether to update RAVE
#' @param restart logical, whether to restart `RStudio` after installation
#' @param nightly logical, whether to install develop version
#' @param demo_data logical, whether to check demo data
#' @param ... for compatibility purpose, ignored
#' @export
check_dependencies <- function(update_rave = TRUE, restart = TRUE,
nightly = FALSE, demo_data = FALSE, ...){
if(!dipsaus::package_installed("ravemanager")) {
utils::install.packages("ravemanager", repos = c(
beauchamplab = "https://beauchamplab.r-universe.dev",
dipterix = "https://dipterix.r-universe.dev",
CRAN = "https://cloud.r-project.org"))
}
ravemanager <- asNamespace("ravemanager")
ravemanager$install(upgrade_manager = FALSE)
rave <- asNamespace("rave")
rave$arrange_modules(refresh = TRUE)
if( restart ){
f <- get0(".rs.restartR", ifnotfound = NULL)
if(is.function(f)) {
f()
}
}
return(invisible())
}
check_dependencies2 <- function(){
catgl('Arranging all existing RAVE modules', level = 'INFO', end = '\n')
arrange_modules(refresh = TRUE, reset = FALSE, quiet = TRUE)
# check if any demo data exists
catgl('Checking RAVE data repository', level = 'INFO', end = '\n')
p <- get_projects()
if('demo' %in% p){
subs <- get_subjects('demo')
if(length(subs)){
return(invisible())
}
}
# install demo subjects
download_sample_data('_group_data', replace_if_exists = TRUE)
download_sample_data('KC')
download_sample_data('YAB')
return(invisible())
}
finalize_installation_internal_demo <- function(upgrade = c('ask', 'always', 'never')){
upgrade <- match.arg(upgrade)
# check demo data
has_demo <- FALSE
installed_subs <- NULL
if('demo' %in% get_projects()){
has_demo <- TRUE
if( upgrade %in% c('never') ){ return() }
installed_subs <- get_subjects('demo')
}
opt <- c('None', 'Only missing demos', 'All', 'First two subjects')
missing_subs <- c('KC', 'YAB', 'YAD', 'YAF', 'YAH', 'YAI', 'YAJ', 'YAK')
if( upgrade == 'always' ){
missing_subs <- c('KC', 'YAB')
}
missing_subs <- missing_subs[!missing_subs %in% installed_subs]
if( upgrade == 'ask' ){
default_opt <- 1
default_opt <- dipsaus::ask_or_default(
'Choose an option to install demo subjects:\n',
paste0(seq_along(opt), '. ', opt, collapse = '\n'),
'\n\nEnter the option number',
default = default_opt
)
default_opt <- dipsaus::parse_svec(default_opt)
if(1 %in% default_opt || !any(seq_len(4) %in% default_opt)){
# no data installed
catgl('No demo data will be installed', level = 'DEFAULT')
return()
} else if(2 %in% default_opt){
} else if(3 %in% default_opt){
missing_subs <- c('KC', 'YAB', 'YAD', 'YAF', 'YAH', 'YAI', 'YAJ', 'YAK')
} else {
missing_subs <- c('KC', 'YAB')
}
catgl(paste(missing_subs, collapse = ', '), ' will be installed', level = 'DEFAULT')
}
if(length(missing_subs)){
quo <- rlang::quo({
dipsaus::rs_focus_console()
for(sub in !!missing_subs){
raveio::catgl('Launching download process - ', sub)
rave::download_sample_data(sub)
}
})
dipsaus::rs_exec(rlang::quo_squash(quo), quoted = TRUE, name = 'Download rave-base demo subjects')
}
return()
}
#' Finalize installation
#' @description download demo data
#' @param packages package name to finalize. 'rave' to only update base demo
#' data, or \code{c('threeBrain', 'ravebuiltins')} to upgrade built-in data,
#' or leave it blank to upgrade all.
#' @param upgrade whether to ask. Default is \code{'always'} to receive default
#' settings. Other choices are \code{'ask'}, \code{'never'},
#' \code{'config-only'}, and \code{'data-only'}
#' @param async whether to run scripts in parallel; default is true.
#'
#' @export
finalize_installation <- function(
packages, upgrade = c('ask', 'config-only', 'always', 'never', 'data-only'),
async = FALSE){
suppressWarnings({
upgrade <- upgrade[upgrade %in% c('ask', 'config-only', 'always', 'never', 'data-only')]
if(!length(upgrade)) {
upgrade <- "ask"
}
if(missing(packages)){
packages <- NULL
}
#
# if(!length(packages) || 'threeBrain' %in% packages){
# # Check N27 brain
# # To be backward compatible, we need to check threeBrain files
# tmp <- system.file('rave.yaml', package = 'threeBrain')
# if(tmp == ''){
# threeBrain::download_N27()
# threeBrain::merge_brain()
# }
# }
# Get all packages with rave.yaml
lib_path <- .libPaths()
allpackages <- unlist(sapply(lib_path, function(lp){
list.dirs(lp, recursive = FALSE, full.names = FALSE)
}, simplify = FALSE))
allpackages <- unique(allpackages)
yaml_path <- sapply(allpackages, function(p){
system.file('rave.yaml', package = p)
})
sel <- yaml_path != ''
if(length(packages)){
sel <- sel & (allpackages %in% packages)
}
packages <- allpackages[sel]
for(pkg in packages){
tryCatch({
# load yaml
conf <- raveio::load_yaml(system.file('rave.yaml', package = pkg))
fname <- conf$finalize_installation
if(is.character(fname) && length(fname) == 1){
ns <- asNamespace(pkg)
fun <- ns[[fname]]
if(is.function(fun)){
fml <- formals(fun)
upgrade1 <- tryCatch({
dipsaus::`%OF%`(upgrade, eval(fml$upgrade))
}, error = function(e) {
upgrade
})
# print(upgrade1)
if('async' %in% names(fml)){
fun(upgrade = upgrade1, async)
} else {
fun(upgrade = upgrade1)
}
}
}
}, error = function(e){
catgl('Error found while finalize installation of [', pkg, ']. Reason:\n',
e$message, '\nSkipping...\n', level = 'WARNING')
})
}
if( async ) {
catgl('Scheduled. There might be some job running in the background. Please wait for them to finish.')
} else {
catgl("Finalize installation done!", level = "INFO")
message("Finalize installation done! Please close all your R/RStudio sessions and restart.")
}
})
invisible()
}
.onAttach <- function(libname, pkgname){
try({
if( arrange_data_dir(FALSE) ){
current_version <- rave_version()
latest_version <- latest_version()
if(is.null(latest_version)) {
latest_version <- "(Unable to obtain the update information)"
} else {
latest_version <- sprintf("%s (built: %s)", latest_version$version, latest_version$built)
}
packageStartupMessage(sprintf(paste(
"RAVE is loaded!",
" Current version - %s",
" Latest available version - %s",
"Data Repository: \t%s",
"Raw-data Repository: \t%s",
"\nTo set option, type %s",
"To check for update, type %s.",
sep = '\n'
),
current_version, latest_version,
rave_options('data_dir'), rave_options('raw_data_dir'),
sQuote('rave::rave_options(launch_gui=TRUE)'),
sQuote('ravemanager::version_info()')
))
}else{
packageStartupMessage('[WARNING]: Cannot find RAVE repository! Please run the following command set them.\n\trave::rave_options()')
}
}, silent = TRUE)
try({
startup_msg <- rave_hist()$get_or_save('..rave_startup_msg..')
if(interactive() && !is.null(startup_msg)){
rave_hist()$save('..rave_startup_msg..' = NULL)
packageStartupMessage(startup_msg)
packageStartupMessage('Please run ', sQuote("rave::check_dependencies()"), " to check dependencies.")
}
}, silent = TRUE)
}