-
Notifications
You must be signed in to change notification settings - Fork 16
/
loadIIASATemplate.R
41 lines (41 loc) · 1.4 KB
/
loadIIASATemplate.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
#' Loads IIASA template (xlsx or yaml)
#'
#' @md
#' @author Oliver Richters
#' @param iiasatemplate filename of xlsx or yaml file provided by IIASA
#' @importFrom dplyr filter rename rename_with
#' @importFrom readxl read_excel
#' @importFrom stringr str_sub
#' @importFrom tibble enframe
#' @importFrom tidyr unnest_wider
#' @importFrom yaml read_yaml
#' @examples
#' \dontrun{
#' # Simple use. Generates submission file in output folder:
#' loadIIASATemplate(
#' iiasatemplate <- "template.xlsx"
#' )
#' }
#' @export
loadIIASATemplate <- function(iiasatemplate) {
if (! file.exists(iiasatemplate)) {
stop("# iiasatemplate ", iiasatemplate, " does not exist, unable to load it.")
}
if (str_sub(iiasatemplate, -5, -1) == ".xlsx") {
for (i in seq(20)) {
template <- rename_with(read_excel(iiasatemplate, sheet = i), tolower)
if ("variable" %in% names(template)) {
break
}
}
} else if (str_sub(iiasatemplate, -5, -1) == ".yaml") {
template <- unlist(read_yaml(iiasatemplate), recursive = FALSE)
template <- rename(rename_with(unnest_wider(enframe(template), "value"), tolower), "variable" = "name")
} else {
stop("iiasatemplate ", iiasatemplate, " is neither xlsx nor yaml, so I don't understand it.")
}
if (length(template$variable) == 0) {
stop("No 'variable' found in iiasatemplate ", iiasatemplate)
}
return(template)
}