knitr::opts_chunk$set( comment = "#>", message=FALSE, warning=FALSE, #fig.retina = 2, fig.width = 8, fig.asp = 0.718, # fig.align = "center", # dev = "ragg_png", out.width = "90%" ) unlink(".RData") library(testthat) library(ggplot2) library(tidyverse) library(scales) library(unhcrthemes) # library(extrafont) # font_import() # loadfonts() # font_install('fontcm')
# Load already included functions if relevant pkgload::load_all(export_all = FALSE)
Templates are re-built notebook that includes all the plotting functions above and are integrated with report parameters. Templates are available both as html report (that can be converted to PDF) and as PowerPoint presentations, all defined from UNHCR standard brand (cf unhcrdown. The templates are available either for countries or regions.
# usethis::use_rmarkdown_template( # template_name = "Country_Factsheet", # template_dir = NULL, # template_description = "Key Statistical Facts for each Country", # template_create_dir = TRUE # ) #' Generate an html / pdf country factsheet #' #' This functions provides a quick access to a basic Statistical Fact sheet #' template that can be customised according to the needs #' #' @param year Numeric value of the year (for instance 2022) #' @param country_asylum_iso3c Character value with the ISO-3 character code of the Country of Asylum #' @param folder folder within your project where to put the generated report. #' Folder will be created if it does not exist #' #' @importFrom unhcrdown paged_simple #' @importFrom dplyr filter select pull #' @importFrom rmarkdown render #' @importFrom here here #' #' @return nothing the file for the report is generated #' #' @export #' template_CtryFactsheet <- function(year = 2022, country_asylum_iso3c, folder = "Report") { ## Create the outfolder if it does not exist output_dir <- paste0(getwd(),"/",folder) if (!dir.exists(output_dir)) {dir.create(output_dir)} ctrname <- ForcedDisplacementStat::reference |> dplyr::filter( iso_3 == country_asylum_iso3c) |> dplyr::select(ctryname) |> dplyr::pull() rmarkdown::render( system.file("rmarkdown/templates/country_factsheet/skeleton/skeleton.Rmd", package = "unhcrdatapackage"), output_file = here::here(folder, paste0('StatFactsheetCtr-', country_asylum_iso3c, '-', year, '.html') ), params = list(countryname= ctrname, country = country_asylum_iso3c, year = year) ) }
## generate for one country # template_CtryFactsheet(year = 2022, country_asylum_iso3c = "USA", folder = "Report") # ## Generate for a specific region # region <- "Americas" # year <- 2022 # library(tidyverse) # ## get all countries with more than 1000 Reported individuals # ctr <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, # y= ForcedDisplacementStat::reference, # by = c("CountryAsylumCode" = "iso_3")) |> # filter(Year == year & # UNHCRBureau == region ) |> # group_by( CountryAsylumName, CountryAsylumCode ) |> # summarise(Value = sum(Value) ) |> # ungroup() |> # filter( Value > 1000 ) # # for ( i in (1:nrow(ctr))) { # # i <- 1 # country_asylum_iso3ci = as.character(ctr[i ,2 ]) # cat(paste0(country_asylum_iso3ci, "\n")) # unhcrdatapackage::template_CtryFactsheet(year = 2022, # country_asylum_iso3c = country_asylum_iso3ci, # folder = "Report") }
# usethis::use_rmarkdown_template( # template_name = "country_prez", # template_dir = NULL, # template_description = "Key Statistical Facts for each Country", # template_create_dir = TRUE # ) #' Generate all country factsheet #' #' @param year Numeric value of the year (for instance 2022) #' @param country_asylum_iso3c Character value with the ISO-3 character code of the Country #' @param folder folder within your project where to put the generated report. #' Folder will be created if it does not exist #' #' @importFrom unhcrdown pptx_slides #' @importFrom dplyr filter select pull #' @importFrom rmarkdown render #' @importFrom here here #' #' @return nothing the file for the report is generated #' #' @export #' template_CtryPrez <- function(year = 2022, country_asylum_iso3c, folder = "Report") { ## Create the outfolder if it does not exist output_dir <- paste0(getwd(),"/",folder) if (!dir.exists(output_dir)) {dir.create(output_dir)} ctrname <- ForcedDisplacementStat::reference |> dplyr::filter( iso_3 == country_asylum_iso3c) |> dplyr::select(ctryname) |> dplyr::pull() rmarkdown::render( system.file("rmarkdown/templates/country_prez/skeleton/skeleton.Rmd", package = "unhcrdatapackage"), output_file = here::here(folder, paste0('StatFactsheet-', country_asylum_iso3c, '-', year, '.pptx') ), params = list(countryname= ctrname, country = country_asylum_iso3c, year = year) ) }
## generate for one country # unhcrdatapackage::template_CtryPrez(year = 2022, # country_asylum_iso3c = "CHL", # folder = "Report") # ## Generate for a specific region # region <- "Americas" # year <- 2022 # library(tidyverse) # ## get all countries with more than 1000 Reported individuals # ctr <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, # y= ForcedDisplacementStat::reference, # by = c("CountryAsylumCode" = "iso_3")) |> # filter(Year == year & # UNHCRBureau == region ) |> # group_by( CountryAsylumName, CountryAsylumCode ) |> # summarise(Value = sum(Value) ) |> # ungroup() |> # filter( Value > 1000 ) # # for ( i in (1:nrow(ctr))) { # # i <- 1 # country_asylum_iso3ci = as.character(ctr[i ,2 ]) # cat(paste0(country_asylum_iso3ci, "\n")) # unhcrdatapackage::template_CtryFactsheet(year = 2022, # country_asylum_iso3c = country_asylum_iso3ci, # folder = "Report") }
# usethis::use_rmarkdown_template( # template_name = "country_slides", # template_dir = NULL, # template_description = "Slide Deck - Statistical Facts", # template_create_dir = TRUE # ) #' Generate all country factsheet #' #' Output is PDF slide deck that preserves well the humanitarian icons #' #' @param year Numeric value of the year (for instance 2022) #' @param country_asylum_iso3c Character value with the ISO-3 character code of the Country #' @param folder folder within your project where to put the generated report. #' Folder will be created if it does not exist #' #' @importFrom unhcrdown html_slides #' @importFrom dplyr filter select pull #' @importFrom rmarkdown render #' @importFrom here here #' #' @return nothing the file for the report is generated #' #' @export #' template_Ctryslides <- function(year = 2022, country_asylum_iso3c, folder = "Report") { ## Create the outfolder if it does not exist output_dir <- paste0(getwd(),"/",folder) if (!dir.exists(output_dir)) {dir.create(output_dir)} ctrname <- ForcedDisplacementStat::reference |> dplyr::filter( iso_3 == country_asylum_iso3c) |> dplyr::select(ctryname) |> dplyr::pull() rmarkdown::render( system.file("rmarkdown/templates/country_slides/skeleton/skeleton.Rmd", package = "unhcrdatapackage"), output_file = here::here(folder, paste0('StatFactsheet-', country_asylum_iso3c, '-', year, '.html') ), params = list(countryname= ctrname, country = country_asylum_iso3c, year = year) ) }
## generate for one country # unhcrdatapackage::template_Ctryslides(year = 2022, # country_asylum_iso3c = "CHL", # folder = "Report") # ## Generate for a specific region # region <- "Americas" # year <- 2022 # library(tidyverse) # ## get all countries with more than 1000 Reported individuals # ctr <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, # y= ForcedDisplacementStat::reference, # by = c("CountryAsylumCode" = "iso_3")) |> # filter(Year == year & # UNHCRBureau == region ) |> # group_by( CountryAsylumName, CountryAsylumCode ) |> # summarise(Value = sum(Value) ) |> # ungroup() |> # filter( Value > 1000 ) # # for ( i in (1:nrow(ctr))) { # # i <- 1 # country_asylum_iso3ci = as.character(ctr[i ,2 ]) # cat(paste0(country_asylum_iso3ci, "\n")) # unhcrdatapackage::template_CtryFactsheet(year = 2022, # country_asylum_iso3c = country_asylum_iso3ci, # folder = "docs/factsheet") }
# usethis::use_rmarkdown_template( # template_name = "regional_factsheet", # template_dir = NULL, # template_description = "Key Statistical Facts for each Country", # template_create_dir = TRUE # ) #' Generate all country factsheet #' #' @param year Numeric value of the year (for instance 2020) #' @param region Bureau that covers all the countrie factsheet to generate #' @param lag Number of year to used as comparison base #' @param folder folder within your project where to put the generated report. #' Folder will be created if it does not exist #' #' @importFrom unhcrdown paged_simple #' @importFrom dplyr filter select pull #' @importFrom rmarkdown render #' @importFrom here here #' #' @return nothing the file for the report is generated #' #' @export #' template_RegFactsheet <- function(year = 2022, region = "Americas", lag = 10, folder = "Report") { ## Create the outfolder if it does not exist output_dir <- paste0(getwd(),"/",folder) if (!dir.exists(output_dir)) {dir.create(output_dir)} regionname <- dplyr::case_when( region == "Americas" ~ "Americas", region == "Asia" ~ "Asia & the Pacific", region == "EastAfrica" ~ "Eastern Africa", region =="Europe" ~ "Europe", region == "MENA" ~ "Middle East & North Africa", region == "SouthAfrica" ~ "Southern Africa", region == "WestAfrica" ~ "Western Africa") rmarkdown::render( system.file("rmarkdown/templates/regional_factsheet/skeleton/skeleton.Rmd", package = "unhcrdatapackage"), output_file = here::here(folder, paste0('StatFactsheet-', region, '-', year, '.html') ), params = list(region=region, regionname = regionname, year = year, lag = lag) ) }
# template_RegFactsheet(year = 2022, # region = "Europe", lag = 10, # folder = "Report") ## We can also generate all factsheets in a loop for 2022 # region <- ForcedDisplacementStat::reference |> # dplyr::distinct(UNHCRBureau) |> # dplyr::filter(!(is.na(UNHCRBureau))) |> # dplyr::pull() # # for( reg in region) { # unhcrdatapackage::template_RegFactsheet(year = 2022, # region = reg, lag = 10, # folder = "Report") # }
# usethis::use_rmarkdown_template( # template_name = "regional_prez", # template_dir = NULL, # template_description = "Key Statistical Facts for each Region", # template_create_dir = TRUE # ) #' Generate all country factsheet #' #' @param year Numeric value of the year (for instance 2020) #' @param region Bureau that covers all the countrie factsheet to generate #' @param lag Number of year to used as comparison base #' #' @param folder folder within your project where to put the generated report. #' Folder will be created if it does not exist #' #' @importFrom unhcrdown pptx_slides #' @importFrom dplyr filter select pull #' @importFrom rmarkdown render #' @importFrom here here #' #' #' @return nothing the file for the report is generated #' #' @export #' template_RegPrez <- function(year = 2022, region = "Americas", lag = 10, folder = "Report") { ## Create the outfolder if it does not exist output_dir <- paste0(getwd(),"/",folder) if (!dir.exists(output_dir)) {dir.create(output_dir)} regionname <- dplyr::case_when( region == "Americas" ~ "Americas", region == "Asia" ~ "Asia & the Pacific", region == "EastAfrica" ~ "Eastern Africa", region =="Europe" ~ "Europe", region == "MENA" ~ "Middle East & North Africa", region == "SouthAfrica" ~ "Southern Africa", region == "WestAfrica" ~ "Western Africa") rmarkdown::render( system.file("rmarkdown/templates/regional_prez/skeleton/skeleton.Rmd", package = "unhcrdatapackage"), output_file = here::here(folder, paste0('StatFactsheet-', region, '-', year, '.pptx') ), params = list(region=region, regionname = regionname, year = year, lag = lag) ) }
# template_RegPrez(year = 2022, region = "Americas", lag = 10, folder = "Report") # # Generate for a specific region # region <- "Americas" # year <- 2022 # library(tidyverse) # ## get all countries with more than 1000 Reported individuals # ctr <- dplyr::left_join( x= ForcedDisplacementStat::end_year_population_totals_long, # y= ForcedDisplacementStat::reference, # by = c("CountryAsylumCode" = "iso_3")) |> # filter(Year == year & # UNHCRBureau == region ) |> # group_by( CountryAsylumName, CountryAsylumCode ) |> # summarise(Value = sum(Value) ) |> # ungroup() |> # filter( Value > 1000 ) # # for ( i in (1:nrow(ctr))) { # # i <- 1 # country_asylum_iso3c = as.character(ctr[i ,2 ]) # cat(paste0(country_asylum_iso3c, "\n")) # unhcrdatapackage::template_CtryPrez(year = 2022, # country_asylum_iso3c = country_asylum_iso3c, # folder = "Report") # }
# Keep eval=FALSE to avoid infinite loop in case you hit the knit button # Execute in the console directly fusen::inflate(flat_file = "dev/dev_template.Rmd", vignette_name = "Report Template", overwrite = 'yes')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.