Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

Revision a93e442dfb67053f3de2e74ee06ceb99e53a4524 authored by Pierre Masselot on 07 November 2024, 17:08:33 UTC, committed by Pierre Masselot on 07 November 2024, 17:08:33 UTC
Zenodo repo pub
1 parent a4f5dec
  • Files
  • Changes
  • 58173d1
  • /
  • 04_tables.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
revision badge
swh:1:rev:a93e442dfb67053f3de2e74ee06ceb99e53a4524
directory badge
swh:1:dir:58173d1bde519d4f834511fd10e457d9220f5825
content badge
swh:1:cnt:70bdff9a147d3844fe158953b88795fe18c8037c

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
04_tables.R
################################################################################
# 
# Contrasting future heat and cold-related mortality under climate change, 
# demographic and adaptation scenarios in 854 European cities
#
# R Code Part 4: Main Table of results
#
# Pierre Masselot & Antonio Gasparrini
#
################################################################################

# If the session has been ended before
if (length(ls()) == 0){
  source("01_pkg_params.R")
  source("02_prep_data.R")
  
  # Read
  nmlist <- expand.grid(c("city", "country", "region", "eu"), 
      c("period", "level")) |>
    apply(1, paste, collapse = "_")
  resdir <- "results_parquet"
  flist <- sprintf("%s/%s.parquet", resdir, nmlist)
  
  # Read all results
  finalres <- lapply(flist, read_parquet)
  names(finalres) <- nmlist
}

if (!dir.exists("figures")) dir.create("figures")

#--------------------------
# Table 1: Big table of rates
#--------------------------

#----- Parameters

# Selected period
pertab <- c(2050, 2095)

# Selected scenario for warming levels
ssptab <- 3
adatab <- "0%"

#----- Select data

# Data label
tablab <- "country_rates"

# Get country-level data by period
bigtabdata <- finalres$country_period |> 
  subset(agegroup == "all" & sc == "clim" & ssp %in% ssptab & 
      adapt %in% adatab & period %in% pertab)

# Add info about countries
cntr_info <- group_by(cities, CNTR_CODE) |> 
  summarise(cntr_name = cntr_name[1], region = region[1], lat = mean(lat))
bigtabdata <- merge(bigtabdata, cntr_info, by.x = "country", by.y = "CNTR_CODE")

#----- Add totals

# Select regional totals
regper <- finalres$region_period |> 
  subset(agegroup == "all" & sc == "clim" & ssp %in% ssptab & 
      adapt %in% adatab & period %in% pertab) |> 
  mutate(lat = -Inf, country = region, cntr_name = as.character(region))

# Select European totals
euper <- finalres$eu_period |> 
  subset(agegroup == "all" & sc == "clim" & ssp %in% ssptab & 
      adapt %in% adatab & period %in% pertab & gcm == "ens") |>
  mutate(country = "Total", lat = -Inf, gcm = NULL, region = "", 
    cntr_name = "Total")

# Put everything together
bigtabdata <- rbind(bigtabdata, regper, euper)

#----- Prepare table

# Tidy cell values
bigtabdata <- mutate(bigtabdata, 
  measure = sprintf("%.1f\n(%.1f to %.1f)", 
    rate_est * byrate, rate_low * byrate, rate_high * byrate),
  range = factor(range, levels = names(rnglabs), labels = rnglabs))

# Pivot period to wide
bigtab <- pivot_wider(bigtabdata, 
  id_cols = c("country", "region", "lat", "cntr_name"), 
  names_from =  c("range", "period", "ssp"), values_from = "measure")

# Order rows and columns
bigtab <- mutate(bigtab, region = factor(region, ordreg)) |>
  arrange(region, desc(lat)) |>
  select(cntr_name, contains(as.character(pertab)))

#----- Format table for word

# Create table with flextable
outtab <- flextable(bigtab)

# Highlight totals
outtab <- bold(outtab, ~ cntr_name %in% c("Total", ordreg))

# Relabel header
outtab <- set_header_labels(outtab, values = as.list(c(
  cntr_name = "", 
  "names<-"(str_split_i(names(bigtab)[-1], "_", 1), names(bigtab)[-1])
)))

# Add header row
if(length(pertab) > 1) outtab <- add_header_row(outtab, 
  colwidth = c(1, rep(3, length(pertab) * length(ssptab))),
  values = c("", sprintf("%i-%s", pertab, substr(pertab + perlen - 1, 3, 4))))
if(length(ssptab) > 1) outtab <- add_header_row(outtab, 
  colwidth = c(1, rep(3, length(pertab) * length(ssptab))),
  values = c("", ssptab))

# Column alignment
outtab <- align(outtab, j = seq_len(ncol(bigtab))[-1], align = "right") |>
  align(i = seq_len(1 + (length(pertab) > 1) + (length(ssptab) > 1)), 
    align = "center", part = "header")

# Page fitting
outtab <- autofit(outtab) |> fit_to_width(7)

#----- Export

save_as_docx(outtab, path = "figures/Tab1.docx")

The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API