You need to sign in or sign up before continuing.
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#....................................................................
#
#This file includes functions related to handling (loading, saving)
# data from raw.rds files or str.rds,
# saved in the RDS subdirectory of the data.dir
#
#....................................................................
#' Load all rds.raw.FADN data for selcted years and countries (rbinds them)
#'
#' It adds two columns: load.YEAR and load.COUNTRY in each row. This can be used to group per year,country the data
#'
#' @param countries a character vector with all the 3-letter codes of the selected countries, e.g. c("ELL", "ESP").
#' If "all" is included, all available countries are loaded
#' @param years a numeric vector with the years selected. If "all" is included, all available years are loaded
#' @param col.filter a character vector with the columns to load. If NULL, all columns are loaded. E.g columns=c('ILOTH_VET_V', 'ILVOTH_V','id')
#' @param row.filter a string giving an expression that will be evaluated in order to select rows. If NULL, all rows are returned. E.g. filter='TF8==1'
#'
#' @return list( "countries"=> c(<RETURNED COUNTRIES), "years"=>c(<AVAILABLE YEARS) )
#' @export
#'
#' @examples
#'
load.fadn.raw.rds = function(countries=c("all"),
years=c("all"),
col.filter = NULL,
row.filter=NULL) {
# check for fadnUtils.data.dir
if(is.null(get.data.dir())) {
warning("You have first to set the fadnUtils.data.dir using set.data.dir function. Exiting ....")
return(FALSE)
} else {
data.dir = get.data.dir();
rds.dir = paste0(data.dir,"/rds/")
}
#select the COUNTRY-YEAR to return
rds.avail = get.available.fadn.raw.rds();
if("all"%in%countries) {countries= unique(rds.avail$COUNTRY)}
if("all"%in%years) {years= unique(rds.avail$YEAR)}
fadn.raw.rds.avail = rds.avail[YEAR%in%years & COUNTRY%in%countries]
fadn.raw.rds.avail[,FILE:=paste0("fadn.raw.",YEAR,".",COUNTRY,".rds")]
fadn.raw.rds.avail.files = fadn.raw.rds.avail[,FILE]
#Rbind the
data.return = NULL;
for(f in fadn.raw.rds.avail.files) {
data.cur = readRDS(paste0(rds.dir, f))
if(! is.null(row.filter)) {
data.cur=data.cur[eval(parse(text=row.filter))]
}
if(! is.null(col.filter)) { #if columns is specified, load only those
col.filter.effective = col.filter[col.filter%in%names(data.cur)]
if(length(col.filter.effective)==0) { #check that at least one data column is contained in the filter, otherwise abort
warning('Column filter does not contain any column of the fadn.raw.rds data. Operation aborted')
return(NULL)
}
if(length(col.filter)>length(col.filter.effective)) {
warning(paste0("Not all columns were found: ", paste0(col.filter[!names(col.filter)%in%col.filter.effective],collapse=",")))
}
data.cur=data.cur[,..col.filter.effective]
}
data.cur[,load.YEAR:=fadn.raw.rds.avail[FILE==f,YEAR] ]
data.cur[,load.COUNTRY:=fadn.raw.rds.avail[FILE==f,COUNTRY] ]
print(paste0("Loading from ",rds.dir, f))
if(is.null(data.return)) {
data.return=data.cur
} else {
#take care in case some columns do not exist between the rbind-ed datasets
data.return=rbindlist(list(data.return,data.cur),fill = T)
}
}
return(data.return)
}
#' Load all rds.str.FADN data for seelcted years and countries
#'
#' @param str.name The extractionname to load data from
#' @param countries a character vector with all the 3-letter codes of the selected countries, e.g. c("ELL", "ESP").
#' If "all" is included, all available countries are loaded
#' @param years
#'
#' @return list( "countries"=> c(<RETURNED COUNTRIES), "years"=>c(<AVAILABLE YEARS) )
#' @export
#'
#' @examples
#'
load.fadn.str.rds = function(extraction_dir,
countries=c("all"),
years=c("all")) {
# check for fadnUtils.data.dir
if(is.null(get.data.dir())) {
warning("You have first to set the fadnUtils.data.dir using set.data.dir function. Exiting ....")
return(FALSE)
} else {
data.dir = get.data.dir();
rds.dir = paste0(data.dir,"/rds/",extraction_dir,"/")
}
#select the COUNTRY-YEAR to return
rds.avail = get.available.fadn.str.rds(extract_dir = extraction_dir);
if("all"%in%countries) {countries= unique(rds.avail$COUNTRY)}
if("all"%in%years) {years= unique(rds.avail$YEAR)}
fadn.str.rds.avail = rds.avail[YEAR%in%years & COUNTRY%in%countries]
fadn.str.rds.avail[,FILE:=paste0("fadn.str.",YEAR,".",COUNTRY,".rds")]
fadn.str.rds.avail = fadn.str.rds.avail[,FILE]
#Rbind the
data.return = NULL;
for(f in fadn.str.rds.avail) {
data.cur = readRDS(paste0(rds.dir, f))
cat(paste0("Loading from ",rds.dir, f, "\n"))
if(is.null(data.return)) {
data.return=data.cur
} else {
data.return[["info"]] = rbindlist(list(data.return[["info"]],data.cur[["info"]]),fill = T)
data.return[["lvst"]][["animals"]] = rbindlist(list(data.return[["lvst"]][["animals"]],data.cur[["lvst"]][["animals"]]),fill = T)
data.return[["lvst"]][["products"]] = rbindlist(list(data.return[["lvst"]][["products"]],data.cur[["lvst"]][["products"]]),fill = T)
data.return[["crops"]] = rbindlist(list(data.return[["crops"]],data.cur[["crops"]]),fill = T)
}
}
return(data.return)
}
#' Title
#'
#' @param countries
#' @param years
#'
#' @return
#' @export
#'
#' @examples
delete.fadn.raw = function (countries=NULL, years=NULL) {
data.dir = get.data.dir();
rds.dir = paste0(data.dir,"/rds/")
rds.avail = get.available.fadn.raw.rds();
if("all"%in%countries) {countries= unique(rds.avail$COUNTRY)}
if("all"%in%years) {years= unique(rds.avail$YEAR)}
fadn.raw.rds.avail = rds.avail[YEAR%in%years & COUNTRY%in%countries]
fadn.raw.rds.avail[,FILE:=paste0("fadn.raw.",YEAR,".",COUNTRY,".rds")]
files.to.delete = fadn.raw.rds.avail[,paste0(rds.dir, FILE)]
file.remove(files.to.delete,showWarnings = TRUE)
}
#' Title
#'
#' @param countries
#' @param years
#'
#' @return
#' @export
#'
#' @examples
delete.fadn.str = function (countries=c(), years=c()) {
data.dir = get.data.dir();
rds.dir = paste0(data.dir,"/rds/")
rds.avail = get.available.fadn.str.rds();
if("all"%in%countries) {countries= unique(rds.avail$COUNTRY)}
if("all"%in%years) {years= unique(rds.avail$YEAR)}
fadn.str.rds.avail = rds.avail[YEAR%in%years & COUNTRY%in%countries]
fadn.str.rds.avail[,FILE:=paste0("fadn.str.",YEAR,".",COUNTRY,".rds")]
files.to.delete = fadn.str.rds.avail[,paste0(rds.dir, FILE)]
res = file.remove(files.to.delete,showWarnings = TRUE)
}
#' Grep a pattern into a raw.rds column names
#'
#' Useful for the case where one want to look if there are certain columns present or missing
#'
#' @param countries a character vector with all the 3-letter codes of the selected countries, e.g. c("ELL", "ESP").
#' If "all" is included, all available countries are loaded
#' @param years a numeric vector with the years selected. If "all" is included, all available years are loa
#' @param pattern a grep-like character pattern. This parameter is passed as is to the grep function
#' @param show if TRUE, the columsn are printed
#'
#' @return Prints the columns and returns them invisibly
#' @export
#'
#' @examples
grep.columns.in.raw.rds = function(pattern,countries=c("all"),years=c("all")) {
# check for fadnUtils.data.dir
if(is.null(get.data.dir())) {
warning("You have first to set the fadnUtils.data.dir using set.data.dir function. Exiting ....")
return(FALSE)
} else {
data.dir = get.data.dir();
rds.dir = paste0(data.dir,"/rds/")
}
#find what countries, years to do the grep
rds.avail = get.available.fadn.raw.rds();
if("all"%in%countries) {countries= unique(rds.avail$COUNTRY)}
if("all"%in%years) {years= unique(rds.avail$YEAR)}
fadn.raw.rds.avail = rds.avail[YEAR%in%years & COUNTRY%in%countries]
fadn.raw.rds.avail[,FILE:=paste0("fadn.raw.",YEAR,".",COUNTRY,".rds")]
fadn.raw.rds.avail = fadn.raw.rds.avail[,FILE]
if(length(fadn.raw.rds.avail)==0) {
cat("No raw.rds for the selected countries-years")
}
#for each selected, do the grep
res = list();
for(f in fadn.raw.rds.avail) {
cat(paste0("\nGrep into ",f,"\n") )
data.cur = readRDS(paste0(rds.dir, f))
names.cur = names(data.cur)
print(names.cur[grep(pattern,names.cur)])
res[[f]] = names.cur[grep(pattern,names.cur)]
}
return(invisible(res))
}
#....................................................................
#
#The following lines includes functions related to stored rds.data,
#
# All function requires that the data.dir is already set
#....................................................................