vcs_raw_data <- read_xlsx("vcs_projects.xlsx", sheet = 1) %>%
clean_names()
# glimpse(vcs_raw_data)
vcs_proj_data <- vcs_raw_data %>%
filter(!is.na(region)) %>%
mutate(world_avg = mean(estimated_annual_emission_reductions,
na.rm = TRUE)) %>%
group_by(region) %>%
summarise(total_emissions_reduction = sum(estimated_annual_emission_reductions,
na.rm = TRUE),
world_avg = mean(world_avg))
# ungroup()
world_avg <- vcs_raw_data %>%
filter(!is.na(region)) %>%
summarise(world_avg = mean(estimated_annual_emission_reductions,
na.rm = TRUE))
reactable(
vcs_proj_data %>%
select(-world_avg),
defaultSorted = "total_emissions_reduction",
columns = list(
region = colDef(
name = "Region"# ,
# footer = "Total"
),
total_emissions_reduction = colDef(
name = "Total Emissions Reduction",
defaultSortOrder = "desc",
format = colFormat(separators = TRUE)#,
# footer = function(values) sprintf("$%.2f", unique(values))
)
)
)
vcs_agg_data <- vcs_raw_data %>%
filter(!is.na(region)) %>%
select(region, country, estimated_annual_emission_reductions) %>%
group_by(region, country) %>%
summarise(total_emissions_reduction = sum(estimated_annual_emission_reductions, na.rm = TRUE))
## `summarise()` has grouped output by 'region'. You can override using the
## `.groups` argument.
reactable(
vcs_agg_data,
groupBy = c("region"),
columns = list(
region = colDef(name = "Region"),
# country = colDef(aggregate = "unique", name = "Country"),
country = colDef(aggregate = "count",
format = list(aggregated = colFormat(suffix = " countries")),
name = "Country"),
total_emissions_reduction = colDef(aggregate = "sum",
name = "Total Emissions Reduction",
format = colFormat(separators = TRUE))
),
bordered = TRUE
)
# Render a bar chart with a label on the left
bar_chart <- function(label, width = "100%", height = "0.875rem", fill = "#00bfc4", background = NULL) {
bar <- div(style = list(background = fill,
width = width,
height = height))
chart <- div(style = list(flexGrow = 1,
marginLeft = "0.375rem",
background = background),
bar)
div(style = list(display = "flex",
marginRight = "6px",
marginLeft = "6px"),
label, chart)
}
reactable(
vcs_proj_data %>%
mutate(world_avg = mean(total_emissions_reduction),
higher_than_avg = if_else(total_emissions_reduction > world_avg, "Yes", "No")) %>%
select(-world_avg),
defaultSorted = "total_emissions_reduction",
columns = list(
region = colDef(
name = "Region"
),
total_emissions_reduction = colDef(
name = "Total Emissions Reduction",
defaultSortOrder = "desc",
format = colFormat(separators = TRUE),
# Render the bar charts using a custom cell render function
cell = function(value) {
width <- paste0(value * 100 / max(vcs_proj_data$total_emissions_reduction), "%")
# Add thousands separators
value <- format(value, big.mark = ",")
# value <- format(value, width = 4, justify = "right")
bar_chart(value, fill = "#3fc1c9")
} ,
# # And left-align the columns
align = "left"
),
higher_than_avg = colDef(cell = function(value) {
# Render as an X mark or check mark
if (value == "Yes") "\u2714\ufe0f Yes" else "\u274c No"
})
)
)
vcs_boxp_data <- vcs_agg_data %>%
mutate(em_red_per_th = total_emissions_reduction/ 1000) %>%
group_by(region) %>%
summarise(region_emissions_reduction = sum(total_emissions_reduction, na.rm = TRUE)
,em_red_per_th = list(em_red_per_th)) %>%
mutate(boxplot = NA)
reactable(
vcs_boxp_data,
defaultSorted = "region_emissions_reduction",
columns = list(
region = colDef(name = "Region"),
region_emissions_reduction = colDef(
name = "Region - Total Emissions Reduction",
# defaultSortOrder = "desc",
format = colFormat(separators = TRUE),
),
em_red_per_th = colDef(show = FALSE),
boxplot = colDef(cell = function(value, index) {
sparkline(vcs_boxp_data$em_red_per_th[[index]], type = "box")
})
)
)
Note: As next steps I would potentially explore {reactablefmtr} package that makes formatting bit more easier and fun.