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
#This file contains functions related to managing raw_str_map files
#' Merges two raw_str_map files and returns either a list or a file
#'
#' All entries in the new.raw_str_map file replace those on the source.raw_str_map file
#'
#' Both files must be relative to the current data.dir/raw_str_maps
#'
#' @param source.raw_str_map.file the filename of the source raw_str_map. It must be relative the raw_str_maps of the current data.dir
#' @param new.raw_str_map.file the filename of the mask raw_str_map. It will replace any entries of the source file. It must be relative the raw_str_maps of the current data.dir
#' @param return.file If set to T, a temporary full file path that contains the merge is returned. Otherwise a list with the contents of the merge is returned
#'
#' @return FALSE in case of problem / if return.file=T, the temporary full path of a file that contains the merged result in json / A list with the contents of the merge if return.file=F
#' @export
#'
#' @examples
raw_str_map.merge = function(source.raw_str_map.file=NULL,
new.raw_str_map.file=NULL,
return.file=F){
#Check that data.dir exist and raw_str_map files exist ----
## 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(invisible(FALSE))
}
##if data.dir is a proper dat.dir
if(check.data.dir.structure()) {
data.dir = get.data.dir();
rds.dir = paste0(data.dir,"/rds/")
raw_str_map.file = paste0(data.dir,'/raw_str_map.json')
}
##check if raw
source.raw_str_map.path = paste0(data.dir,"/raw_str_maps/",source.raw_str_map.file)
new.raw_str_map.path = paste0(data.dir,"/raw_str_maps/",new.raw_str_map.file)
if(!file.exists(source.raw_str_map.path) | !file.exists(new.raw_str_map.path)) {
warning("One or more of the raw_str_maps provided, do not exist. Exiting ....")
return(FALSE)
}
#read json files to a list ----
library("jsonlite")
source.raw_str_map = fromJSON(paste(readLines(source.raw_str_map.path), collapse="\n"))
new.raw_str_map = fromJSON(paste(readLines(new.raw_str_map.path), collapse="\n"))
#merge both files----
merged.raw_str_map = modifyList(source.raw_str_map,new.raw_str_map)
#save or export results----
if(return.file) {
#save to temporary file as JSON
f.tmp = tempfile(pattern = "file", tmpdir = tempdir())
cat(
toJSON(merged.raw_str_map, auto_unbox = T),
"\n",
file=f.tmp
)
return(f.tmp)
}
else {
return(merged.raw_str_map)
}
}
#' Takes $id, $info, $costs objects of a raw_str_map object and create Source-Description pairs
#'
#' Used internally
#'
#' @param listcontent
#'
#' @return list(COLUMN-NAME = c(SOURCE=csv column name, DESCRIPTION=description of column), ..... )
#'
#' @examples
take.raw_str_map.columns = function( listcontent ) {
str.cols = names(listcontent)
splitcol = function(x) {
if(is.list(x)) {
return(c("SOURCE"=x$source, "DESCRIPTION"=x$description))
} else {
return(c("SOURCE"=x, "DESCRIPTION"=""))}
}
return(
lapply(listcontent,splitcol)
)
}
#' Checks if the definitions of a raw_str_map are compatible with a fadn.raw.rds for a certain year and country
#'
#' Checks if all values are actual columns of the fadn.raw.rds file
#'
#' @param fadn.country
#' @param fadn.year
#' @param raw_str_map.file The full filepath of the raw_str_map
#'
#' @return
#' @export
#'
#' @examples
check.raw_str_map = function(raw_str_map.file,
fadn.country=NA,
fadn.year=NA) {
#TODO
#check if raw_str_map exist
#TODO
#check if fadn.raw.rds exist
#read file
raw_str_map.file = paste0(data.dir,"/raw_str_maps/",raw_str_map.file)
raw_str_map = fromJSON(paste(readLines(raw_str_map.file), collapse="\n"))
#get all keys
map.keys.orig = c( unlist(raw_str_map$id),
unlist(raw_str_map$info),
unlist(raw_str_map$crops,recursive = T)
)
#split all characters
map.keys.orig.split = unlist(
strsplit(map.keys.orig,
"\\+|\\-|,|ifelse|\\(|\\)|\\>|\\/|\\<"),
recursive = T
)
#remove whitespaces and empty
map.keys.orig.split=trimws(map.keys.orig.split)
map.keys.orig.split=map.keys.orig.split[!map.keys.orig.split==""]
#get the column listing of fadn.raw.rds
data.dir = get.data.dir();
rds.dir = paste0(data.dir,"/rds/")
data.name = paste0("fadn.raw.",fadn.year,".",fadn.country,".rds")
rds.data = readRDS(paste0(rds.dir,data.name))
rds.data.columns = colnames(rds.data)
#print what is in raw_str_map but not in fadn.raw.rds
cat(paste0("What is in raw_str_map but not in ",data.name,"\n"))
map.keys.orig.split[!map.keys.orig.split%in%rds.data.columns]
}