-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils-metadata.R
127 lines (112 loc) · 4.19 KB
/
utils-metadata.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
#' Various internal functions to work with IOT metadata, including the labelling
#' vocabularies, row and column ordering.
#' None of these functions should be exported.
#' @keywords internal
getdata <- function(...) {
e <- new.env()
name <- utils::data(..., envir = e)[1]
e[[name]]
}
#' @keywords internal
define_prod_ind <- function() {
## Define which sources follow the prod_ind vocabulary.
## The rest of the potential sources follows the induse vocabulary, except for the Croatia
## replication data.
c("naio_10_cp1700", "naio_10_cp1750", "naio_10_pyp1700",
"naio_10_pyp1750", "naio_10_cp15", "naio_10_cp16",
"naio_10_cp1610", "naio_10_cp1620", "naio_10_cp1630",
"naio_10_pyp1620", "naio_10_pyp1630", "germany_1995")
}
#' @keywords internal
adjust_stk_flow <- function( stk_flow, source ) {
if ( source %in% c("naio_10_cp1620", "naio_10_cp1630",
"naio_10_pyp1620", "naio_10_pyp1630")
) {
'TOTAL' # Tax and margin tables only have one version
} else {
stk_flow
}
}
#' @keywords internal
#' @autoglobal
get_vocabulary_prod_ind <- function() {
getdata ("metadata") %>% # For tables that follow prod_ind vocabulary
filter( variable == "prod_na") %>%
dplyr::rename ( prod_na = code) %>%
dplyr::rename ( prod_na_lab = label ) %>%
dplyr::rename ( row_order = numeric_label ) %>%
dplyr::rename ( iotables_row = iotables_label )
}
#' @keywords internal
#' @autoglobal
get_vocabulary_induse <- function() {
getdata("metadata") %>% # For tables that follow the induse vocabulary
filter( variable == "induse") %>%
dplyr::rename ( induse = code) %>%
dplyr::rename ( induse_lab = label )%>%
dplyr::rename ( col_order = numeric_label ) %>%
dplyr::rename ( iotables_col = iotables_label )
}
#' @keywords internal
#' @autoglobal
get_vocabulary_t_rows <- function() {
getdata("metadata") %>%
filter( variable == "t_rows") %>%
dplyr::rename ( t_rows2 = code) %>%
dplyr::rename ( t_rows2_lab = label ) %>%
dplyr::rename ( row_order = numeric_label ) %>%
dplyr::rename ( iotables_row = iotables_label )
}
#' @importFrom dplyr rename
#' @keywords internal
#' @autoglobal
get_vocabulary_t_cols <- function() {
getdata("metadata") %>%
filter( variable == "t_cols") %>%
dplyr::rename ( t_cols2 = code) %>%
dplyr::rename ( t_cols2_lab = label ) %>%
dplyr::rename ( col_order = numeric_label ) %>%
dplyr::rename ( iotables_col = iotables_label )
}
#' @keywords internal
#' @autoglobal
get_metadata_rows <- function(source) {
prod_ind <- define_prod_ind()
trow_tcol <- croatia_files <- c('croatia_2010_1700', 'croatia_2010_1800',
'croatia_2010_1900')
uk_tables <- c("uk_2010_siot", "uk_2010_use", "uk_2010_imports",
"uk_2010_coeff", "uk_2010_inverse")
if ( source %in% prod_ind ) {
get_vocabulary_prod_ind()
} else if ( source %in% trow_tcol ) {
get_vocabulary_t_rows()
} else if ( source %in% uk_tables ) {
getdata("metadata_uk_2010") %>%
filter ( !is.na(.data$uk_row)) %>%
select ( -all_of(c("uk_col", "uk_col_label", "induse", "col_order")) ) %>%
mutate ( uk_row = gsub("\\.", "-", as.character(.data$uk_row))) %>%
mutate ( uk_row = gsub(" & ", "-", as.character(.data$uk_row)))
} else {
stop("Don't know which row name vocabulary to use.")
}
}
#' @keywords internal
#' @autoglobal
get_metadata_cols <- function(source) {
prod_ind <- define_prod_ind()
trow_tcol <- croatia_files <- c('croatia_2010_1700', 'croatia_2010_1800',
'croatia_2010_1900')
uk_tables <- c("uk_2010_siot", "uk_2010_use", "uk_2010_imports", "uk_2010_coeff", "uk_2010_inverse")
if ( source %in% prod_ind ) {
get_vocabulary_induse()
} else if ( source %in% trow_tcol ) {
get_vocabulary_t_cols()
} else if ( source %in% uk_tables ) {
getdata("metadata_uk_2010") %>%
filter( !is.na(.data$uk_col)) %>%
select( -uk_row, -uk_row_label, -prod_na, -row_order) %>%
mutate ( uk_col = gsub("\\.", "-", as.character(.data$uk_col))) %>%
mutate ( uk_col = gsub(" & ", "-", as.character(.data$uk_col))) %>%
mutate ( uk_col = trimws(.data$uk_col, 'both'))
}
}