".svn/pristine/7b" did not exist on "65fb2e15e95f811de0ac6a8e077627a150a1569d"
Newer
Older
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
#This file contains functions related to managing data.dir (create, set, read contents, etc.)
#' Creates a data.dir
#'
#' @param folder.path
#' @param raw_str_map.file
#' @param metadata
#'
#' @return TRUE if created succesfully; FALSE otherwise. It return in invisible mode.
#' @export
#'
#' @examples
create.data.dir = function(folder.path,
metadata = "{\n'description': 'No Description Provided',\n'created-by':'',\n'created-at':''\n}") {
if(file.exists(folder.path)) {
#if it is already a data.dir, exit
if(check.data.dir.structure(folder.path)) {
cat("This is already a data.dir structure. Doing nothing.\n")
return(invisible(FALSE));
}
}
else { #if folder does not exist, create it
if(!dir.create(folder.path)) {print("Could not create folder."); return(invisible(FALSE));}
}
#create fadnUtils.metadata ----
cat(metadata, file=paste0(folder.path,"/fadnUtils.metadata.json"))
#create DIR>csv ----
dir.create(paste0(folder.path,"/csv"))
#create DIR>rds ----
dir.create(paste0(folder.path,"/rds"))
#create DIR>spool ----
dir.create(paste0(folder.path,"/spool"))
cat(
"In this folder you can save project related files",
file = paste0(folder.path,"/spool/readme.txt")
)
return(invisible(TRUE));
}
#' Sets the data.dir
#'
#' @param new.data.dir the full path to the folder where the data.dir will be. Ending slash "/" shall not be present
#'
#' @return TRUE if succesfully set the data.dir; FALSE otherwise. Returns in invisible mode.
#' @export
#'
#' @examples
set.data.dir = function(new.data.dir) {
#check that it is a valid data.dir ----
if(!check.data.dir.structure(new.data.dir, silent = F)) {
cat("Not a valid data.dir. cannot set the folder provided.\n");
return(invisible(FALSE));
}
#set option for fadnUtils.data.dir ----
options("fadnUtils.data.dir" = new.data.dir)
#load stored.rds.env ----
stored.rds.env.path = paste0(new.data.dir,"/stored.rds.data.RData")
if(file.exists(stored.rds.env.path)) {
load(stored.rds.env.path,envir = env.stored.rds)
}
return(invisible(TRUE));
}
#' Gets the data.dir
#'
#' data.dir is the folder where data is stored
#' r package will create two subfolders:
#' csv = location to store the csv files of th DG-AGRI (fadn.raw.csv)
#' rds = location to store rds files (fadn.raw.rds, fadn.str.rds, etc.)
#'
#' @return the value of option("fadnUtils.data.dir")
#' @export
#'
#' @examples
get.data.dir = function() {
ret = getOption("fadnUtils.data.dir")
if(is.null(ret)) {
return(NULL)
} else {
if(ret=="") {
return(NULL)
} else {
return(ret)
}
}
}
#' Show the contents of data.dir
#'
#' @param data.dir a specific directory to show contents, otherwise it will read the fadnUtils.data.dir
#' @param return.list if T, returns a list, otherwise print the results
#'
#' @return returns a list containing: {description: "the description of the data dir",
#' DT of fadn.raw.rds (Country-Year) and of fadn.str.rds (country-Year)
#' @export
#'
#' @examples
show.data.dir.contents = function(data.dir=NULL, return.list=F) {
if(is.null(data.dir)) {
data.dir=get.data.dir()
}
if(!check.data.dir.structure(data.dir)) {
warning("Not a valid data.dir. Exiting ....")
return(NULL)
}
#store the results in a list
ret=list()
#get descriptio
ret[['description']]=paste(readLines(paste0(data.dir,'/fadnUtils.metadata.json'),warn = F),collapse = " ")
#get raw data
ret[["raw"]] = get.available.fadn.raw.rds()
#get extracted data
ret[["extractions"]]=list();
extr.dirs = list.dirs(path = paste0(data.dir,"/rds"), full.names = F, recursive = F)
for(d in extr.dirs) {
ret[["extractions"]][[d]]=list()
ret[["extractions"]][[d]][["contents"]] = get.available.fadn.str.rds(extract_dir = d)
}
if(return.list==T) {
return(invisible(ret));
} else {
cat("\n","Description: \n", ret[['description']])
cat("\n\nRaw data: \n")
print(dcast(ret[["raw"]] ,COUNTRY~YEAR,value.var = "COUNTRY",fun.aggregate = length))
cat("\n\nExtracted data : \n")
for(d in extr.dirs) {
cat("\n---- Extracted dir: ", d, "\n")
if(nrow(ret[["extractions"]][[d]][["contents"]])>0 ) {
print(dcast(ret[["extractions"]][[d]][["contents"]] ,COUNTRY~YEAR,value.var = "COUNTRY",fun.aggregate = length))
} else {
cat("No data present")
}
}
cat("\n\n")
}
}
#' Checks if the structure of the fadnUtils.data.dir is ok
#'
#' @param data.dir a specific directory to show contents, otherwise it will read the fadnUtils.data.dir
#' @param silent if TRUE, do not print any message
#'
#' @return TRUe if everything is ok; FALSE otherwise
#' @export
#'
#' @examples
check.data.dir.structure = function(data.dir=NULL, silent=T) {
messages = c()
if(is.null(data.dir)) {data.dir = get.data.dir()}
if(!file.exists(data.dir)) {
messages=c(messages,"Folder provided as data.dir does not exit.");
}
if(!file.exists(paste0(data.dir,"/fadnUtils.metadata.json"))) {
messages=c(messages,"Problem with data.dir: fadnUtils.metadata.json does not exist.")
}
if(!file.exists(paste0(data.dir,"/csv"))) {
messages=c(messages,"Problem with data.dir: 'csv' directory does not exist.")
}
if(!file.exists(paste0(data.dir,"/rds"))) {
messages=c(messages,"Problem with data.dir: 'rds' directory does not exist.")
}
if(length(messages)==0) {return(invisible(TRUE))}
if(!silent) {
cat(messages,sep = "\n")
}
return(invisible(FALSE))
}
#' Returns the available YEAR-COUNTRY fadn.raw.rds
#'
#' @return a DT of the available YEAR-COUNTRY fadn.raw.rds
#'
#' @export
#'
#' @examples
get.available.fadn.raw.rds = function(data.dir=NULL) {
if(is.null(data.dir)) {data.dir=get.data.dir()}
if(is.null(data.dir)) {
warning("Either provide explicitly a fadnUtils.data.dir to the function orfirst to set the fadnUtils.data.dir using set.data.dir function. Exiting ....")
return(FALSE)
}
rds.dir = paste0(data.dir,"/rds/")
rds.avail.files = list.files(rds.dir,pattern = "fadn.raw.*.rds")
pattern = "fadn[.]raw[.](\\d*)[.](\\S*)[.]rds"
fadn.raw.rds.avail = data.table(
YEAR = gsub(pattern,"\\1",rds.avail.files),
COUNTRY = sub(pattern,"\\2",rds.avail.files)
)
return(fadn.raw.rds.avail)
}
#' Returns the available YEAR-COUNTRY fadn.str.rds, for each str.folder
#'
#' @return DT of the available YEAR-COUNTRY fadn.str.rds
#' @param extract_dir The name of the extraction dir
#'
#' @export
#'
#' @examples
get.available.fadn.str.rds = function(data.dir=NULL,extract_dir) {
if(is.null(data.dir)) {data.dir=get.data.dir()}
if(is.null(data.dir)) {
warning("Either provide explicitly a fadnUtils.data.dir or set the fadnUtils.data.dir using set.data.dir function. Exiting ....")
return(FALSE)
}
rds.dir = paste0(data.dir,"/rds/",extract_dir)
rds.avail.files = list.files(rds.dir,pattern = "fadn.str.*.rds")
pattern = "fadn[.]str[.](\\d+)[.](\\S+)[.]rds"
fadn.str.rds.avail = data.table(
YEAR = gsub(pattern,"\\1",rds.avail.files),
COUNTRY = gsub(pattern,"\\2",rds.avail.files)
)
return(fadn.str.rds.avail)
}