Skip to content

Commit b29214d

Browse files
committed
refactor
1 parent df9b87b commit b29214d

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ importFrom(dplyr,inner_join)
2626
importFrom(dplyr,left_join)
2727
importFrom(dplyr,mutate)
2828
importFrom(dplyr,n)
29+
importFrom(dplyr,pick)
2930
importFrom(dplyr,pull)
3031
importFrom(dplyr,select)
3132
importFrom(dplyr,summarise)
@@ -88,6 +89,7 @@ importFrom(tibble,tribble)
8889
importFrom(tidyr,drop_na)
8990
importFrom(tidyr,pivot_wider)
9091
importFrom(tidyselect,all_of)
92+
importFrom(tidyselect,everything)
9193
importFrom(tidyselect,matches)
9294
importFrom(tiltIndicator,example_data_factory)
9395
importFrom(tiltIndicatorAfter,profile_emissions)

R/prepare_geo_data.R

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ prepare_geo_data <- function(data,
1414
scenario = scenarios(),
1515
year = years(),
1616
risk_category = risk_category()) {
17-
grouping_emission <- arg_match(grouping_emission)
1817
risk_category <- arg_match(risk_category)
1918
country_code <- arg_match(country_code)
20-
scenario <- arg_match(scenario)
21-
year <- year
2219

2320
crucial <- c(
2421
aka("risk_category"),
@@ -32,10 +29,11 @@ prepare_geo_data <- function(data,
3229

3330
shp_0 <- get_eurostat_geospatial(
3431
resolution = 10,
35-
nuts_level = 3,
36-
year = 2016,
32+
nuts_level = "all",
33+
year = 2021,
3734
crs = 3035
38-
)
35+
) |>
36+
filter(!(geo %in% c("FRY10", "FRY20", "FRY30", "FRY40", "FRY50")))
3937

4038
# filter for the specified country
4139
shp_1 <- shp_0 |>
@@ -48,13 +46,27 @@ prepare_geo_data <- function(data,
4846
shp_1 <- shp_1 |>
4947
inner_join(nuts_all, by = "geo")
5048

49+
filtered_geo_data <- data
50+
51+
# Apply filtering only if the corresponding argument is not NULL
52+
if (!is.null(grouping_emission)) {
53+
filtered_geo_data <- filtered_geo_data |>
54+
filter(.data$grouping_emission == .env$grouping_emission)
55+
}
56+
57+
if (!is.null(scenario)) {
58+
filtered_geo_data <- filtered_geo_data |>
59+
filter(.data$scenario == .env$scenario)
60+
}
61+
62+
if (!is.null(year)) {
63+
filtered_geo_data <- filtered_geo_data |>
64+
filter(.data$year == .env$year)
65+
}
66+
5167
# merge shapefile with financial data
52-
geo <- data |>
53-
filter(
54-
.data$grouping_emission == .env$grouping_emission,
55-
.data$scenario == .env$scenario,
56-
.data$year == .env$year
57-
) |>
68+
geo <- filtered_geo_data |>
69+
distinct() |>
5870
left_join(shp_1, by = "postcode") |>
5971
st_as_sf()
6072

@@ -77,15 +89,15 @@ prepare_geo_data <- function(data,
7789
#' library(tibble)
7890
#'
7991
#' # Create a sample geo_data tibble
80-
#' geo <- tibble(
92+
#' geo_example <- tibble(
8193
#' postcode = c("1", "2", "3"),
8294
#' company_name = c("A", "B", "C"),
8395
#' emission_profile = factor(c("low", "medium", "high"),
8496
#' levels = c("low", "medium", "high")
8597
#' )
8698
#' )
8799
#'
88-
#' aggregate_geo(geo, mode = "emissions_profile_worst_case", risk_category = "emission_category")
100+
#' aggregate_geo(geo_example, mode = "emissions_profile_worst_case", risk_category = "emission_category")
89101
aggregate_geo <- function(geo, mode, risk_category) {
90102
aggregated_data <- geo |>
91103
group_by(.data$postcode, .data[[risk_category]]) |>
@@ -105,9 +117,9 @@ aggregate_geo <- function(geo, mode, risk_category) {
105117
summarise(
106118
total_mode = add(.data$total_mode),
107119
geometry = first(.data$geometry),
108-
low = add(.data$low),
109-
medium = add(.data$medium),
110-
high = add(.data$high)
120+
low = if ("low" %in% names(pick(everything()))) add(.data$low) else 0.0,
121+
medium = if ("medium" %in% names(pick(everything()))) add(.data$medium) else 0.0,
122+
high = if ("high" %in% names(pick(everything()))) add(.data$high) else 0.0
111123
) |>
112124
mutate(color = pmap(list(.data$high, .data$medium, .data$low), custom_gradient_color))
113125
aggregated_data

R/tiltPlot-package.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @importFrom dplyr left_join
1414
#' @importFrom dplyr mutate
1515
#' @importFrom dplyr n
16+
#' @importFrom dplyr pick
1617
#' @importFrom dplyr pull
1718
#' @importFrom dplyr select
1819
#' @importFrom dplyr summarise
@@ -75,6 +76,7 @@
7576
#' @importFrom tidyr drop_na
7677
#' @importFrom tidyr pivot_wider
7778
#' @importFrom tidyselect all_of
79+
#' @importFrom tidyselect everything
7880
#' @importFrom tidyselect matches
7981
#' @importFrom tiltIndicator example_data_factory
8082
#' @importFrom tiltIndicatorAfter profile_emissions

R/utils.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ modes <- function() {
215215
scenarios <- function() {
216216
c(
217217
"1.5C RPS",
218-
"NZ 2050"
218+
"NZ 2050",
219+
NULL
219220
)
220221
}
221222

@@ -237,7 +238,8 @@ scenarios_financial <- function() {
237238
years <- function() {
238239
c(
239240
2030,
240-
2050
241+
2050,
242+
NULL
241243
)
242244
}
243245

@@ -266,6 +268,7 @@ grouping_emission <- function() {
266268
"tilt_sector",
267269
"unit",
268270
"unit_isic_4digit",
269-
"unit_tilt_sector"
271+
"unit_tilt_sector",
272+
NULL
270273
)
271274
}

0 commit comments

Comments
 (0)