forked from CIDA-CSPH/CIDAtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_meta_data.R
238 lines (219 loc) · 7.34 KB
/
project_meta_data.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
#' Set Project Analyst
#'
#' This function allows you to set the project analyst.
#' This will overwrite the current value if exists.
#'
#' @param AnalystName A string containing the analyst name
#' @return A message stating the name has been changed.
#' @keywords options Analyst ProjData
#' @export
#'
SetProjectAnalyst <- function(AnalystName){
if(!is.character(AnalystName)) stop('Analyst Name must be a character string')
if(length(AnalystName) > 1) {
warning('Only First String is Used')
AnalystName <- AnalystName[1]
}
SetProjectData('analyst', AnalystName)
return(paste('The Project Analyst name has been changed to', AnalystName))
}
#' Set Project Name
#'
#' This function allows you to set the project name. This will overwrite the
#' current value if exists.
#'
#' @param ProjectName A string containing the analyst name
#' @return A message stating the name has been changed.
#' @keywords options ProjectName ProjData
#' @export
#'
SetProjectName <- function(ProjectName){
if(!is.character(ProjectName)) stop('Project Name must be a character string')
if(length(ProjectName) > 1) {
warning('Only First String is Used')
ProjectName <- ProjectName[1]
}
SetProjectData('ProjectName', ProjectName)
return(paste('The Project name has been changed to', ProjectName))
}
#' Set PI Name
#'
#' This function allows you to set the Project's PI. This will overwrite the
#' current value if exists.
#'
#' @param PI A string containing the analyst name
#' @return A message stating the name has been changed.
#' @keywords options PI ProjData
#' @export
#'
SetProjectPI <- function(PI){
if(!is.character(PI)) stop('PI Name must be a character string')
if(length(PI) > 1) {
warning('Only First String is Used')
PI <- PI[1]
}
SetProjectData('PI', PI)
return(paste('The Project PI has been changed to', PI))
}
#' Set Project Location
#'
#' This function allows you to set the Project's location on the CIDA drive.
#' This will overwrite the current value if exists.
#'
#' @param path A string containing the file path to the project location on CIDA drive
#' @return A message stating the name has been changed.
#' @keywords options location ProjData
#' @export
#'
SetProjectLocation <- function(path){
if(!is.character(path)) stop('Path must be a character string')
if(length(path) > 1) {
warning('Only First String is Used')
path <- path[1]
}
path <- proj.location.handler(path)
SetProjectData('datalocation', path)
return(paste('The Project Location has been changed to', path))
}
#' Get Project Analyst
#'
#' This function returns the Project Analyst Name. If none exists, it
#' will return the value of CIDAtools.analyst option or blank if the option
#' is not set.
#'
#' @return A character string with the analyst name
#' @keywords options Analyst ProjData
#' @export
#'
ProjectAnalyst <- function(){
if(file.exists(file.path('.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('.ProjData/Data.dcf'), all = T)
if('analyst' %in% names(ProjData)) return(ProjData$analyst)
}
if(file.exists(file.path('../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../.ProjData/Data.dcf'), all = T)
if('analyst' %in% names(ProjData)) return(ProjData$analyst)
}
if(file.exists(file.path('../../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../../.ProjData/Data.dcf'), all = T)
if('analyst' %in% names(ProjData)) return(ProjData$analyst)
}
if(!is.null(getOption('CIDAtools.analyst'))){
return(getOption('CIDAtools.analyst'))
}
return('')
}
#' Get Project Name
#'
#' This function returns the Project Name or blank if none exists.
#'
#' @return A character string with the project name
#' @keywords options ProjData ProjectName
#' @export
#'
ProjectName <- function(){
if(file.exists(file.path('.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('.ProjData/Data.dcf'), all = T)
if('ProjectName' %in% names(ProjData)) return(ProjData$ProjectName)
}
if(file.exists(file.path('../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../.ProjData/Data.dcf'), all = T)
if('ProjectName' %in% names(ProjData)) return(ProjData$ProjectName)
}
if(file.exists(file.path('../../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../../.ProjData/Data.dcf'), all = T)
if('ProjectName' %in% names(ProjData)) return(ProjData$ProjectName)
}
return('')
}
#' Get PI Name
#'
#' This function returns the PI Name or blank if none exists.
#'
#' @return A character string with the PI name
#' @keywords options ProjData PI
#' @export
#'
ProjectPI <- function(){
if(file.exists(file.path('.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('.ProjData/Data.dcf'), all = T)
if('PI' %in% names(ProjData)) return(ProjData$PI)
}
if(file.exists(file.path('../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../.ProjData/Data.dcf'), all = T)
if('PI' %in% names(ProjData)) return(ProjData$PI)
}
if(file.exists(file.path('../../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../../.ProjData/Data.dcf'), all = T)
if('PI' %in% names(ProjData)) return(ProjData$PI)
}
return('')
}
#' Get Project data location on CIDA Drive
#'
#' @param path (optional) a relative path to a particular place in the project
#'
#' @return full (absolute) file path including the project location on CIDA drive
#' @export
#'
#' @examples
#' # Read data from current project
#' \dontrun{
#' df <- read.csv(ProjectLocation("DataRaw/my_proj_data.csv"))
#' }
#'
ProjectLocation <- function(path = ''){
if(file.exists(file.path('.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('.ProjData/Data.dcf'), all = T)
if('datalocation' %in% names(ProjData)){
temp_path <- CIDA_drive_path(ProjData$datalocation)
return(file.path(temp_path, path))
}
}
if(file.exists(file.path('../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../.ProjData/Data.dcf'), all = T)
if('datalocation' %in% names(ProjData)){
temp_path <- CIDA_drive_path(ProjData$datalocation)
return(file.path(temp_path, path))
}
}
if(file.exists(file.path('../../.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('../../.ProjData/Data.dcf'), all = T)
if('datalocation' %in% names(ProjData)){
temp_path <- CIDA_drive_path(ProjData$datalocation)
return(file.path(temp_path, path))
}
}
message('Project location not found, use SetProjectData("datalocation", x).')
return("")
}
#' Set data for project
#'
#' Allows you to set misc project data parameters
#' for Project Name, Analyst, or PI recommend you use specific function
#'
#' @param Parameter Project Parameter to be set
#' @param Value Value to set to project parameter
#' @export
#'
#'
SetProjectData <- function(Parameter, Value){
if (!is.character(Parameter) | !is.character(Parameter))
stop('Parameter must be a character string of length one')
if(!is.character(Value)) stop('Value must be a character string')
if(length(Value) > 1) {
warning('Only First String is Used')
Value <- Value[1]
}
if(Parameter=='datalocation'){
Value <- proj.location.handler(Value)
}
if(file.exists(file.path('.ProjData/Data.dcf'))){
ProjData <- read.dcf(file.path('.ProjData/Data.dcf'), all = T)
} else{
dir.create(paste0('.ProjData/'), recursive = T, showWarnings = F)
ProjData <- list()
}
ProjData[Parameter] <- Value
write.dcf(ProjData, file.path('.ProjData/Data.dcf'))
}