Skip to content

Commit 0d40392

Browse files
author
Gonçalo Marques
committed
added metis_countries, resolved dependencies, new ccyy_to_yyyy and ccyy_to_cname and fixed structure_to_plot
1 parent 5f8dc57 commit 0d40392

12 files changed

Lines changed: 233 additions & 187 deletions

R/checks_for_lissyuse.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ check_length_iso2 <- function(data) {
6363
#' @return Stops if all iso2 codes in `data` are invalid, and it warns if only some are invalid.
6464
check_iso2 <- function(data, lws = FALSE) {
6565
valid_iso2 <- if (lws) {
66-
lissyrtools::get_countries_lws()
66+
lissyrtools::datasets %>% dplyr::filter(database == "LWS") %>% dplyr::select(iso2) %>% unique() %>% dplyr::pull() %>% as.vector()
6767
} else {
68-
lissyrtools::get_countries_lis()
68+
lissyrtools::datasets %>% dplyr::filter(database == "LIS") %>% dplyr::select(iso2) %>% unique() %>% dplyr::pull() %>% as.vector()
6969
}
7070

7171
data_iso2 <- stringr::str_sub(data, 1, 2)

R/data.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@
5555
#' @format A data frame with 9 variables:
5656
"value_label_c_data"
5757

58+
59+
#' Countries Names and ISO Codes.
60+
#'
61+
#' A data frame containing the country names, and its respective iso2 and iso3 codes.
62+
#'
63+
#' @format A data frame with 3 variables:
64+
"metis_countries_df"
65+
66+
67+
5868
#' LIS Variables
5969
#'
6070
#' A character vector containing the names of the variables available in LIS.

R/get_labels_notes_surveys.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
variable_labels <- function(vars = NULL) {
2121

2222
if (is.null(vars)) {
23-
output <- tibble::deframe(data_vars_labels)
23+
output <- tibble::deframe(lissyrtools::data_vars_labels)
2424
return(output)
2525
} else if (!(is.null(vars))) {
2626
if (is.character(vars)) {
@@ -45,7 +45,7 @@ variable_labels <- function(vars = NULL) {
4545

4646
vars_accepted <- vars[!vars %in% invalid_vars]
4747

48-
output <- tibble::deframe(data_vars_labels)[vars_accepted]
48+
output <- tibble::deframe(lissyrtools::data_vars_labels)[vars_accepted]
4949
return(output)
5050
} else if (isTRUE(exists("vars"))) {
5151
if (!(is.list(vars))) {
@@ -54,7 +54,7 @@ variable_labels <- function(vars = NULL) {
5454
))
5555
} else {
5656
columns <- names(vars[[1]])
57-
output <- tibble::deframe(data_vars_labels)[columns]
57+
output <- tibble::deframe(lissyrtools::data_vars_labels)[columns]
5858
return(output)
5959
}
6060
}
@@ -142,7 +142,7 @@ variable_has_note <- function(variable, iso2, lws = FALSE) {
142142

143143
years <- get_years_function(i)[[1]]
144144

145-
existing_years <- data_with_warnings %>%
145+
existing_years <- lissyrtools::data_with_warnings %>%
146146
dplyr::filter(database == db, iso2 == i, var_name == variable) %>%
147147
dplyr::pull(year)
148148

R/structure_to_plot.R

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,22 @@ structure_to_plot <- function(data_list, print_columns = TRUE) {
134134
# 1st structure
135135
if (
136136
all(
137-
names(data_list) %in%
138-
c(
139-
names(lissyrtools::get_countries_lis()),
140-
names(lissyrtools::get_countries_lws())
141-
)
142-
)
137+
names(data_list) %in% (lissyrtools::metis_countries_df %>% dplyr::select(name) %>% unique() %>% dplyr::pull())
138+
)
143139
) {
144-
result_df <- purrr::list_rbind(purrr::imap(
140+
141+
list_of_cc <- lissyrtools::metis_countries_df %>% dplyr::filter(name %in% names(data_list)) %>% dplyr::select(-iso3) %>% as.vector()
142+
143+
vector_of_cc <- list_of_cc[["iso2"]]
144+
names(vector_of_cc) <- list_of_cc[["name"]]
145+
146+
result_df <- dplyr::bind_rows(purrr::imap(
145147
data_list,
146148
~ tibble::enframe(.x, name = "year", value = "value") %>%
147149
dplyr::mutate(cname = .y)
148150
)) %>%
149151
dplyr::mutate(
150-
cc = lissyrtools::get_countries_lis()[cname],
152+
cc = vector_of_cc[cname],
151153
yy = stringr::str_sub(year, 3, 4),
152154
dname = paste0(cc, yy),
153155
year = as.integer(year)
@@ -161,14 +163,14 @@ structure_to_plot <- function(data_list, print_columns = TRUE) {
161163
purrr::map_chr(data_list, ~ class(.x)[1]) %in% c("numeric", "integer")
162164
)
163165
) {
164-
result_df <- purrr::list_rbind(purrr::imap(
166+
result_df <- dplyr::bind_rows(purrr::imap(
165167
data_list,
166168
~ tibble::enframe(.x, name = "category", value = "value") %>%
167169
dplyr::mutate(dname = stringr::str_sub(.y,1,4))
168170
)) %>%
169171
dplyr::mutate(
170-
cname = lissyrtools::ccyy_to_cname(dname),
171-
year = lissyrtools::ccyy_to_yyyy(dname),
172+
cname = purrr::map_chr(dname, lissyrtools::ccyy_to_cname),
173+
year = as.integer(purrr::map_chr(dname, lissyrtools::ccyy_to_yyyy)),
172174
category = stringr::str_remove(category, "^\\[\\d+\\]"),
173175
year = as.integer(year)
174176
) %>%
@@ -181,11 +183,11 @@ structure_to_plot <- function(data_list, print_columns = TRUE) {
181183
all(purrr::map_chr(data_list, ~ class(.x)[1]) == "list")
182184
)
183185
) {
184-
result_df <- purrr::list_rbind(purrr::imap(
186+
result_df <- dplyr::bind_rows(purrr::imap(
185187
data_list,
186188
~ {
187189
outer_name <- stringr::str_sub(.y,1,4)
188-
purrr::list_rbind(purrr::imap(.x, function(sublist, subgroup) {
190+
dplyr::bind_rows(purrr::imap(.x, function(sublist, subgroup) {
189191
# subgroup would be the categorical variable in run_weighted_mean or run_weighted_percentiles
190192
tibble::enframe(sublist, name = "vector_names", value = "value") %>% # vector_names: could be percentiles, shares, averages or the by var in run_weighted_count
191193
dplyr::mutate(
@@ -197,8 +199,8 @@ structure_to_plot <- function(data_list, print_columns = TRUE) {
197199
}
198200
)) %>%
199201
dplyr::mutate(
200-
cname = lissyrtools::ccyy_to_cname(dname),
201-
year = as.integer(lissyrtools::ccyy_to_yyyy(dname))
202+
cname = purrr::map_chr(dname, lissyrtools::ccyy_to_cname),
203+
year = as.integer(purrr::map_chr(dname, lissyrtools::ccyy_to_yyyy))
202204
) %>%
203205
dplyr::select(cname, year, dname, category, name, value)
204206

0 commit comments

Comments
 (0)