-
Notifications
You must be signed in to change notification settings - Fork 13
/
FAOSTATbuild.R
182 lines (157 loc) · 7.44 KB
/
FAOSTATbuild.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
###########################################################################
## Title: This script builds the FAOSTAT package
## Created: 09/05/2013
## Updated: 09/07/2014
###########################################################################
# Needed libraries --------------------------------------------------------
library(roxygen2)
library(RJSONIO)
library(utils)
library(knitr)
# Ghost script ------------------------------------------------------------
Sys.setenv(R_GSCMD='"C:/Program Files/gs/gs9.07/bin/gswin32c.exe"')
# FAOcountryProfile -------------------------------------------------------
tmp <-
read.csv(file = "FAOcountryProfileUTF8.csv", header = TRUE,
stringsAsFactors = FALSE, na.string = "", encoding = "UTF-8")
tmp[tmp == " "] <- NA
FAOcountryProfile <-
tmp[, c("FAOST_CODE", "ADM0_CODE", "ISO2_CODE", "ISO2_WB_CODE",
"ISO3_CODE", "ISO3_WB_CODE", "UN_CODE", "CRS_CODE", "MOTHER_M49_CODE",
"M49_FAOST_CODE", "FAO_TABLE_NAME", "OFFICIAL_FAO_NAME",
"UNOFFICIAL1_NAME", "UNOFFICIAL2_NAME", "UNOFFICIAL3_NAME",
"SHORT_NAME", "ABBR_FAO_NAME_SP", "UN_YEAR_START_INFO",
"UN_YEAR_END_INFO", "FORMER_MEMBER_INFO", "COMPOSITION_INFO",
"UN_STATUS_INFO")]
save(FAOcountryProfile, file = "FAOcountryProfile.RData")
FAOregionProfile <- tmp
save(FAOregionProfile, file = "FAOregionProfile.RData")
# FAOmetaTable ------------------------------------------------------------
## Groups
# urlGrp <- "http://fenix.fao.org/wds/rest/groups/faostat2/en"
# urlGrp <- "http://fenixapps2.fao.org/wds/rest/groups/faostat2/en"
urlGrp <- "http://faostat3.fao.org/wds/rest/groups/faostat2/en"
groupCode <-
unique(data.frame(groupCode = sapply(fromJSON(urlGrp, encoding = "UTF-8"),
function(x) x[1]),
groupName = sapply(fromJSON(urlGrp, encoding = "UTF-8"),
function(x) x[2]),
stringsAsFactors = FALSE))
## Domains
# urlDom <- "http://fenix.fao.org/wds/rest/domains/faostat2/"
# urlDom <- "http://fenixapps2.fao.org/wds/rest/domains/faostat2/"
urlDom <- "http://faostat3.fao.org/wds/rest/domains/faostat2/"
base <- data.frame()
for(i in 1:NROW(groupCode)){
tmp <- fromJSON(paste(urlDom, groupCode[i, "groupCode"], "/en", sep = ""),
encoding = "UTF-8")
tmp2 <- unique(data.frame(groupCode = groupCode[i, "groupCode"],
domainCode = sapply(tmp, function(x) x[1]),
domainName = sapply(tmp, function(x) x[2]),
stringsAsFactors = FALSE))
base <- rbind(base, tmp2)
}
domainCode <- base
## Elements
# base <- data.frame()
# for(i in 1:NROW(domainCode)){
# tmp <- try(fromJSON(paste("http://faostat3.fao.org/wds/rest/procedures/elements/faostat2/",
# domainCode[i, "domainCode"], "/en", sep = ""), encoding = "UTF-8"))
# if(!inherits(tmp, "try-error") & length(tmp) != 0){
# tmp2 <- unique(data.frame(domainCode = domainCode[i, "domainCode"],
# elementCode = sapply(tmp, function(x) x[1]),
# elementName = sapply(tmp, function(x)
# paste0(x[2], "(", x[3], ")")),
# stringsAsFactors = FALSE))
# base <- rbind(base, tmp2)
# }
# }
# elemCode <- base
elemCode <- read.csv("./DomainItemElement.csv", header = TRUE)
elemCode <- unique(elemCode[,c("DomainCode", "ElementCode")])
elementDescription <- read.csv("./ElementDescription.csv", header = TRUE)
elementDescription <- elementDescription[, c("ElementCode", "ElementUnitNameE")]
elemCode <- merge(elemCode, elementDescription, by = "ElementCode", all.x = TRUE)
colnames(elemCode) <- c("elementCode", "domainCode", "elementName")
elemCode <- elemCode[, c("domainCode", "elementCode", "elementName")]
## Items
base <- data.frame()
for(i in 1:NROW(domainCode)){
tmp <- try(fromJSON(paste("http://faostat3.fao.org/wds/rest/procedures/items/faostat2/",
domainCode[i, "domainCode"], "/en", sep = ""), encoding = "UTF-8"))
if(!inherits(tmp, "try-error") & length(tmp) != 0){
tmp2 <- unique(data.frame(domainCode = domainCode[i, "domainCode"],
itemCode = sapply(tmp, function(x) x[1]),
itemName = sapply(tmp, function(x) x[2]),
stringsAsFactors = FALSE))
base <- rbind(base, tmp2)
}
}
itemCode <- base
## Items aggregated
base <- data.frame()
for(i in 1:NROW(domainCode)){
tmp <- try(fromJSON(paste("http://faostat3.fao.org/wds/rest/procedures/itemsaggregated/faostat2/",
domainCode[i, "domainCode"], "/en", sep = ""), encoding = "UTF-8"))
if(!inherits(tmp, "try-error") & length(tmp) != 0){
tmp2 <- unique(data.frame(domainCode = domainCode[i, "domainCode"],
itemCode = sapply(tmp, function(x) x[1]),
itemName = sapply(tmp, function(x) x[2]),
stringsAsFactors = FALSE))
base <- rbind(base, tmp2)
}
}
itemAggCode <- base
## Table
FAOmetaTable <-
list(groupTable = groupCode, domainTable = domainCode,
itemTable = itemCode, itemAggTable = itemAggCode, elementTable = elemCode)
save(FAOmetaTable, file = "FAOmetaTable.RData")
# Building the package ----------------------------------------------------
## Remove the folder if it exists
if(file.exists("./FAOSTAT"))
unlink("FAOSTAT", recursive = TRUE)
## Build the package
package.skeleton("FAOSTAT", code_files = paste("./Codes/R/",
dir("./Codes/R/",
pattern = "\\.R$"), sep = ""),
force = FALSE)
## Include the data
dir.create("FAOSTAT/data")
file.copy(from = "./FAOcountryProfile.RData",
to = "FAOSTAT/data/", overwrite = TRUE)
file.copy(from = "./FAOregionProfile.RData",
to = "FAOSTAT/data/", overwrite = TRUE)
file.copy(from = "./FAOmetaTable.RData",
to = "FAOSTAT/data/", overwrite = TRUE)
file.copy(from = "./DESCRIPTION", to = "FAOSTAT/",
overwrite = TRUE)
unlink("./FAOSTAT/Read\\-and\\-delete\\-me")
## Include Demo
dir.create("FAOSTAT/demo")
file.copy(from = "./FAOSTATdemo.R",
to = "FAOSTAT/demo/", overwrite = TRUE)
cat("FAOSTATdemo Demonstration for the FAOSTAT package\n",
file = "FAOSTAT/demo/00Index")
## Include tests
dir.create("FAOSTAT/tests")
file.copy("Codes/tests", "FAOSTAT", recursive = TRUE)
## Use roxygen to build the documentation
roxygenize("FAOSTAT")
## Include vignette
dir.create("./FAOSTAT/vignettes/")
dir.create("./FAOSTAT/inst/")
dir.create("./FAOSTAT/inst/doc/")
file.copy(from = "./Documentation/FAOSTAT.pdf",
to = "./FAOSTAT/inst/doc/", overwrite = TRUE)
file.copy(from = "./Documentation/FAOSTAT.Rnw",
to = "./FAOSTAT/vignettes/", overwrite = TRUE)
file.copy(from = "./Documentation/FAOSTAT.pdf",
to = "./FAOSTAT/vignettes/", overwrite = TRUE)
## Build and check the package
system("R CMD INSTALL --build FAOSTAT")
system("R CMD build FAOSTAT")
system("R CMD check --as-cran --timings FAOSTAT")
###########################################################################
## End
###########################################################################