-
Notifications
You must be signed in to change notification settings - Fork 16
/
getTemplate.R
39 lines (39 loc) · 1.55 KB
/
getTemplate.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
#' Retrieves latest template for a given project or mapping file
#'
#' @md
#' @author Falk Benke, Oliver Richters
#' @param project name of the project of requested template, or filename of mapping or template
#' @importFrom utils read.csv2
#' @importFrom gms chooseFromList
#' @importFrom magclass unitsplit
#' @export
getTemplate <- function(project = NULL) {
templates <- templateNames()
if (is.null(project)) {
project <- chooseFromList(names(templates), type = "templates",
returnBoolean = FALSE, multiple = FALSE)
if (length(project) == 0) stop("No template selected, abort.")
}
if (! file.exists(project)) {
project <- gsub("\\.csv$", "", gsub("^mapping_template_", "", project))
}
filename <- if (project %in% names(templates)) templates[project] else project
if (file.exists(filename)) {
data <- read.csv2(filename, header = TRUE, sep = ";", na.strings = list(""), strip.white = TRUE)
if (ncol(data) == 5) { # is a mapping template generated by generateMappingfile
if ("Variable" %in% colnames(data) && ! "Unit" %in% colnames(data)) {
varunit <- magclass::unitsplit(data$Variable)
data$Variable <- varunit$variable
data$Unit <- varunit$unit
}
if ("piam_variable" %in% colnames(data) && ! "piam_unit" %in% colnames(data)) {
varunit <- magclass::unitsplit(data$piam_variable)
data$piam_variable <- varunit$variable
data$piam_unit <- varunit$unit
}
}
return(data)
} else {
stop("Mapping file ", filename, " not found.")
}
}