@@ -57,26 +57,44 @@ process_batch_q<-function(batchq, query, scen, filters, func=sum){
57
57
# ' Match GCAM ID to region using data from a lookup table.
58
58
# '
59
59
# ' We match by ID number to avoid problems with variant spellings and the like.
60
+ # ' With the optional arguments you can also omit regions for which you don't
61
+ # ' want to plot the data for some reason, and you can translate the
62
+ # ' abbreviations used in subregion output.
63
+ # '
64
+ # ' The \code{provincefile} and \code{drops} arguments are a little clunky. They
65
+ # ' are optional, but if you are using one of the built-in map sets, then you
66
+ # ' \emph{must not} specify them if they don't exist for the map set you are
67
+ # ' using. Currently, \code{rgn14} and \code{basin235} have neither drops nor
68
+ # ' province abbreviations. The \code{rgn32} set has drops, but not province
69
+ # ' abbreviations. Only the \code{chn} set (and the \code{usa} set, when it is
70
+ # ' finally implemented) has both.
60
71
# ' @param datatable A table of results produced by \code{\link{process_batch_q}}
61
- # ' @param lookupfile File containing the region lookup table. XXX: we
62
- # ' need to provide a mechanism for users to use the data installed
63
- # ' internally in the package.
64
- # ' @param provincefile File containing the province lookup table, if
65
- # ' applicable. XXX: Same comment as above
66
- # ' @param drops File containing a list of regions to drop, if
67
- # ' applicable. XXX: Same comment as above.
72
+ # ' @param lookupfile Name of one of the predefined map sets, OR, if you're using
73
+ # ' a custom map set, the file containing the region lookup table
74
+ # ' @param provincefile Name of one of the predefined map sets, OR, if you're
75
+ # ' using a custom map set, file containing the province lookup table, if
76
+ # ' applicable.
77
+ # ' @param drops Name of one of the predefined map sets, OR, if you're using
78
+ # ' a custom map set, the file containing a list of regions to drop, if
79
+ # ' applicable.
68
80
# ' @return Input table modified to include a GCAM ID for reach region.
69
81
# ' @export
70
- addRegionID <- function (datatable , lookupfile , provincefile = ' none ' , drops = ' none ' ) {
71
- if (provincefile != ' none ' ){
82
+ addRegionID <- function (datatable , lookupfile = lut.rgn32 , provincefile = NULL , drops = NULL ) {
83
+ if (! is.null( provincefile ) ){
72
84
datatable <- translateProvince(datatable , provincefile )
73
85
}
74
86
75
- if (drops != ' none ' ){
87
+ if (! is.null( drops ) ){
76
88
datatable <- dropRegions(datatable , drops )
77
89
}
78
90
79
- lookuptable <- read.csv(lookupfile , strip.white = T , stringsAsFactors = F )
91
+ lookuptable <-
92
+ if (is.symbol(lookupfile )) {
93
+ get.internal(lookupfile ,' lut' )
94
+ }
95
+ else {
96
+ read.csv(lookupfile , strip.white = T , stringsAsFactors = F )
97
+ }
80
98
81
99
# Differentiate region-Region issue
82
100
if (" Region" %in% names(datatable )){
@@ -105,16 +123,23 @@ addRegionID<-function(datatable, lookupfile, provincefile='none', drops='none')
105
123
return (finaltable )
106
124
}
107
125
126
+ # ' Replace subregion abbreviations with full subregion names
127
+ # '
128
+ # ' Subregions are given two-letter abbreviations in GCAM output. This function
129
+ # ' uses a lookup table to restore the full names.
130
+ # '
131
+ # ' @param datatable The table with the abbreviated names in it.
132
+ # ' @param provincefile Name of a defined mapset OR name of a file containing the
133
+ # ' lookup table.
108
134
translateProvince <- function (datatable , provincefile ){
109
- # ## Replace province abbreviations with full province names
110
- # ## to ensure matching with GCAM map names.
111
- # ## Inputs:
112
- # ## datatable - data frame of query from batch query CSV.
113
- # ## provincefile - string; path to file with abbreviations and full names of regions.
114
- # ## Outputs:
115
- # ## datatable - datatable modified so that abbreviations are now full names.
116
135
117
- provincetable <- read.csv(provincefile , strip.white = T )
136
+ provincetable <-
137
+ if (is.symbol(provincefile )) {
138
+ get.internal(provincefile ,' prov' )
139
+ }
140
+ else {
141
+ read.csv(provincefile , strip.white = T )
142
+ }
118
143
119
144
# Differentiate region-Region issue
120
145
if (" Region" %in% names(datatable )){
@@ -142,16 +167,24 @@ dropRegions<-function(datatable, drops){
142
167
# ## Outputs:
143
168
# ## datatable - updated data frame with regions dropped.
144
169
145
- dr <- read.csv(drops , strip.white = T , header = F )
146
- dr <- as.character(dr $ V1 )
147
-
148
- regcols <- grepl(" egion" , names(datatable )) # Find instances of "region" or "Region" columns
170
+ dr <-
171
+ if (is.symbol(drops )) {
172
+ get.internal(drops ,' drop' )
173
+ }
174
+ else {
175
+ read.csv(drops , strip.white = T , header = F )
176
+ }
177
+ dr <- as.character(dr $ V1 )
149
178
150
- datatable [regcols ]<- lapply(datatable [regcols ], function (x ) replace(x , x %in% dr , NA )) # Replace drop col values with NA
179
+ regcols <- grepl(" egion" , names(datatable )) # Find instances of "region" or "Region" columns
180
+ # # ^-- Technically this will also trigger on 'Legion' or 'legion'
151
181
152
- datatable <- na.omit(datatable ) # Remove rows containing NA
182
+ # # XXX: Do the next step with dplyr instead of this convoluted way
183
+ datatable [regcols ]<- lapply(datatable [regcols ], function (x ) replace(x , x %in% dr , NA )) # Replace drop col values with NA
153
184
154
- return (datatable )
185
+ datatable <- na.omit(datatable ) # Remove rows containing NA
186
+
187
+ return (datatable )
155
188
}
156
189
157
190
# ---------------------------------------------------------------------------
@@ -535,3 +568,41 @@ plot_GCAM <- function(mapdata, col = NULL, proj=robin, extent=EXTENT_WORLD, orie
535
568
return (mp )
536
569
}
537
570
571
+ # ' Get auxiliary data for a named mapset.
572
+ # '
573
+ # ' We have several standard map sets. Each of them has several auxiliary tables
574
+ # ' associated with it. This function retrieves the auxiliary table associated
575
+ # ' with the requested. Right now this function understands \code{rgn14},
576
+ # ' \code{rgn32}, \code{basin235}, and \code{chn}.
577
+ # '
578
+ # ' @param mapset The name of the mapset. Can be either a symbol or a string.
579
+ # ' @param type The type of table. Right now this is either 'lut', 'drop', or
580
+ # ' 'prov'
581
+ get.internal <- function (mapset , type ) {
582
+ eval(as.symbol(paste(type ,mapset ,sep = ' .' )))
583
+ }
584
+
585
+ # ' Designator for the rgn14 map set
586
+ # '
587
+ # ' This symbol will select the rgn14 map set
588
+ # ' @export
589
+ rgn14 <- quote(rgn14 )
590
+
591
+ # ' Designator for the rgn32 map set
592
+ # '
593
+ # ' This symbol will select the rgn32 map set
594
+ # ' @export
595
+ rgn32 <- quote(rgn32 )
596
+
597
+ # ' Designator for the basin235 map set
598
+ # '
599
+ # ' This symbol will select the basin235 map set
600
+ # ' @export
601
+ basin235 <- quote(basin235 )
602
+
603
+ # ' Designator for the chn map set
604
+ # '
605
+ # ' This symbol will select the chn map set
606
+ # ' @export
607
+ chn <- quote(chn )
608
+
0 commit comments