-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget.groundhog.folder.R
115 lines (89 loc) · 3.42 KB
/
get.groundhog.folder.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
#' Get current local path to groundhog folder
#'
#' @return the path to the groundhog folder, the meta-library where
#' [groundhog.library()] downloads and stores packages that can be loaded
#'
#' @note you can change the location of this folder with the command
#' `set.groundhog.folder("path")`.
#'
#' @examples
#' \dontrun{
#' get.groundhog.folder()
#' }
#'
#' @export
#'
#' @seealso [set.groundhog.folder()]
#'
# Function that gets the groundhog folder, or prompts user to create it.
get.groundhog.folder <- function() {
#Set main folder with 'cookie files' and default for library
main_folder <- paste0(path.expand("~"), "/R_groundhog")
#See if consent has been given \
consent <- (file.exists(main_folder))
#If no folder, ask for consent
if (consent == FALSE) {
message2()
message1("groundhog needs authorization to save files to '",main_folder, "'\n",
"Enter 'OK' to provide authorization")
answer <- readline()
answer <- gsub("'", "", answer) #kill the ' if entered
if (toupper(answer)=="OK")
{
consent <- TRUE
}
} #End if consent == FALSE
if (consent == FALSE)
{
message("You typed '",answer,"'. Only if you type 'OK' can you use groundhog.library()")
exit()
}
#Create main folder
dir.create(main_folder, showWarnings = FALSE, recursive = TRUE)
#Path to cookie file with location of folder
path_file_storing_groundhog_library_location <- paste0(main_folder ,"/current_groundhog_folder.txt")
#If cookie file exists, use it, otherwise, use that same location for library
if (file.exists(path_file_storing_groundhog_library_location)) {
#Read the 'cookie' file with the location of the library
f <- path_file_storing_groundhog_library_location
groundhog.folder <- readChar(f, file.info(f)$size) #Reads full file into a string, fixes bug in groundhog 1.1.0
} else {
#This is the default
groundhog.folder <-paste0(main_folder, "/groundhog_library/")
#Set it using function below
set.groundhog.folder(groundhog.folder)
} #End if cookie file does not exist
return(groundhog.folder)
}
#' Set groundhog folder location
#'
#' @param path Character. The path to the groundhog folder containing the library
#' where packages are downloaded and installed.
#'
#'
#' @examples
#' \dontrun{
#' set.groundhog.folder("~/.R_groundhog")
#' }
#'
#' @return (invisibly) `TRUE` upon success.
#'
#' @export
#'
#' @seealso [get.groundhog.folder()]
#'
set.groundhog.folder <- function(path) {
#Set main folder with 'cookie files' and default for library
main_folder <- paste0(path.expand("~"), "/R_groundhog/")
#Create main folder
dir.create(main_folder, showWarnings = FALSE, recursive = TRUE)
#Path to file saving groundhog folder
path_file_storing_groundhog_library_location <- paste0(main_folder ,"current_groundhog_folder.txt")
#Save the cookie
path <- trimws(path)
cat(path, file = path_file_storing_groundhog_library_location)
#Assign it to the live environment
Sys.setenv(GROUNDHOG_FOLDER = path)
#Show message
message1("The groundhog folder path is now:\n",path)
}