@@ -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")
89101aggregate_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
0 commit comments