forked from NEONScience/NEON-IS-data-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow.renv.init.rstr.R
106 lines (84 loc) · 5.15 KB
/
flow.renv.init.rstr.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
##############################################################################################
#' @title Initialize renv lockfile for NEON-IS-data processing package/module dependencies
#' @author
#' Cove Sturtevant \email{csturtevant@battelleecology.org} \cr
#' @description Workflow. Initializes/re-initializes the renv.lock file for the chosen module or local
# package. The renv.lock file is a listing of the exact packages used by the module/package and
# their versions as existed in the top-facing library when renv.lock was last created/updated,
# i.e the local packrat library or user library > system library). Adjust the options below to
# suite your needs, e.g. to save and use and/or use the packrat library within the module or
# package folder.
#
# If a lockfile (renv.lock) already exists, the user will be interactively asked whether to
# restore from it or reinitialize it from the current top-facing library. Do not use this script
# to update the lockfile after installing packages in the packrat library (i.e. when using
# renv::install when the packrat library is the top-facing library). In that case, use
# renv::snapshot(</path/to/module/or/package/>) to update the lockfile, otherwise the packrat
# library will be reinitialized from the user/system library, erasing all your recent install work.
#'
#' @return Lockfile listing module/package dependencies in the module/package directory
#' @references
#' License: (example) GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#' @keywords Currently none
# changelog and author contributions / copyrights
# Cove Sturtevant (2020-01-23)
# original creation
##############################################################################################
# ------ Choose options --------
# Module or local package directory
#dirWork <- '/scratch/SOM/Github/RstudioServer/NEON-IS-data-processing/NEON-IS-data-processing/pack/NEONprocIS.qaqc'
#dirWork <- '~/NEON-IS-data-processing/pack/NEONprocIS.wq'
dirWork <- '~/NEON-IS-data-processing/flow/flow.wq.fdom.corr'
PackIgnr <- c('NEONprocIS.base','NEONprocIS.cal','NEONprocIS.qaqc','NEONprocIS.wq') # These should already be in the respective docker containers
# Keep and use the local project that renv creates when creating/updating the lockfile
# in dirWork? If TRUE, note .Rprofile and .Rproj files will be created/retained in dirWork,
# and R will be restarted in this new or existing project. If FALSE, any resulting .Rprofile
# and .Rproj files will be deleted. R may or may not restart depending on the options selected
# below.
KeepProj <- FALSE
# Keep the local library in dirWork that results from creating/updating the lockfile? If TRUE,
# the local library is retained. If FALSE, it is deleted. Cannot be FALSE if KeepProj is TRUE.
# NOTE: Only the renv.lock file is needed for restoring dependencies, and is still retained if
# this option is TRUE.
KeepLocalLib <- FALSE
# Use the local library created/updated in dirWork after creating/updating the lockfile?
# If TRUE, any package updates or new installations will be installed in the local library.
# Any libraries used in the R session will first be loaded from the local library. If FALSE,
# the R session will quit and must be reloaded.
# Cannot be TRUE if KeepLocalLib is FALSE, and cannot be FALSE if KeepProj is TRUE.
# In other words, you cannot use a local library you chose to delete, and you must use the
# local library if you decided to keep the created/existing project files.
UseLocalLib <- FALSE
#----------------------- Begin Program -----------------------
# Error check the options
if(KeepProj == TRUE && KeepLocalLib == FALSE){
stop('KeepLocalLib cannot be FALSE if KeepProj is TRUE. Check options.')
} else if (KeepLocalLib == FALSE && UseLocalLib == TRUE) {
stop('UseLocalLib cannot be TRUE if KeepLocalLib is FALSE. Check options.')
} else if (KeepProj == TRUE && UseLocalLib == FALSE){
stop('UseLocalLib cannot be FALSE if KeepProj is TRUE. Check options.')
}
# Ensure that package 'renv' is installed
if(!("renv" %in% rownames(installed.packages()))) install.packages("renv")
# Change the working directory to the module/package directory
# MUST DO THIS - if not the ignored packages are not actually ignored
base::setwd(dirWork)
base::suppressWarnings(base::unlink(base::paste0(dirWork,'/renv.lock'))) # For some reason it works best to do a complete rewrite
# Initialize dependency management
renv::settings$ignored.packages(PackIgnr)
renv::init(dirWork,restart=KeepProj)
# If there is already a lockfile, you will be asked whether to restore from the lockfile,
# discard the lockfile and re-initialize it, or to activate the project but do nothing else.
# Choose the option that suites the desired use of this script. For example, if wanting to
# In any case, a .Rprofile file will be created, and potentially a *.Rproj file. Delete them.
if(KeepProj == FALSE){
base::suppressWarnings(base::file.remove(base::paste0(dirWork,'/',c('*.Rproj','.Rprofile','.Rhistory'))))
}
# Remove the local library
if(KeepLocalLib == FALSE){
base::suppressWarnings(base::unlink(base::paste0(dirWork,'/renv/'), recursive=TRUE))
}
# Don't use the local library
if(UseLocalLib == FALSE){
base::quit()
}