Site icon R-bloggers

wbstats is back on CRAN

[This article was first published on https://pacha.dev/blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< !DOCTYPE html> < charset="utf-8"> < http-equiv="X-UA-Compatible" content="IE=edge"> < name="viewport" content="width=device-width, initial-scale=1.0"> pacha.dev/blog < !-- MathJax Configuration --> < !-- Smart header: libraries detected based on content --> < !-- File: /tmp/tmp.LYl6Tz1CkF/index.html -->
  • < !-- DEBUG: Found sourceCode --> < !-- Load custom CSS after any library CSS to ensure proper precedence -->
  • < header class="site-top">

    Mauricio “Pachá” Vargas Sepúlveda

    Blog with notes about R, Shiny, SQL, Python, Linux and C++. This blog is listed on R-Bloggers.

    HOME 🏠
    < !-- categories are printed below this--> < nav class="sidebar-nav">

    Categories

    < header id="title-block-header" class="quarto-title-block default">

    wbstats is back on CRAN

    Search and download data from the World Bank Data API.
    Author

    Mauricio “Pachá” Vargas S.

    Published

    September 14, 2025

    If this post is useful to you I kindly ask a minimal donation on Buy Me a Coffee. It shall be used to continue my Open Source efforts. The full explanation is here: A Personal Message from an Open Source Contributor.

    You can send me questions for the blog using this form and subscribe to receive an email when there is a new post.

    Last week I needed to use wbstats to obtain some time series, and I realised it was removed from CRAN. However, it’s back now!

    I contacted the maintainer, who explained that CRAN was asking for some time consuming changes to the package’s structure and documentation, and it was removed over CRAN’s policy on internet access.

    Something similar happened to the tradestatistics package I created and that I have maintained in the last nine years. I adapted the tradestatistics approach to comply with CRAN’s policies for wbstats, which consisted in using mocked tests with pre-recorded API queries thanks to the vcr package, and now it’s back to CRAN. Other required changes where tiny changes about the documentation and the package structure.

    I’ll be maintaining this package now, so please send me any issues or feature requests you may have on the package’s GitHub repository.

    You can install:

    The latest release version from CRAN with

    if (!require(wbstats)) install.packages("wbstats")
    Loading required package: wbstats

    or

    The latest development version from github with

    remotes::install_github("pachadotdev/wbstats")
    < section id="downloading-data-from-the-world-bank" class="level1">

    Downloading data from the World Bank

    library(wbstats)
    
    # Population for every country from 1960 until present
    d <- wb_data("SP.POP.TOTL")
        
    head(d)
    # A tibble: 6 × 9
      iso2c iso3c country    date SP.POP.TOTL unit  obs_status footnote last_updated
      <chr> <chr> <chr>     <dbl>       <dbl> <chr> <chr>      <chr>    <date>      
    1 AF    AFG   Afghanis…  2024    42647492 <NA>  <NA>       <NA>     2025-12-04  
    2 AF    AFG   Afghanis…  2023    41454761 <NA>  <NA>       <NA>     2025-12-04  
    3 AF    AFG   Afghanis…  2022    40578842 <NA>  <NA>       <NA>     2025-12-04  
    4 AF    AFG   Afghanis…  2021    40000412 <NA>  <NA>       <NA>     2025-12-04  
    5 AF    AFG   Afghanis…  2020    39068979 <NA>  <NA>       <NA>     2025-12-04  
    6 AF    AFG   Afghanis…  2019    37856121 <NA>  <NA>       <NA>     2025-12-04  
    < section id="hans-roslings-gapminder-using-wbstats" class="level2">

    Hans Rosling’s Gapminder using wbstats

    if (!require(tidyverse)) install.packages("tidyverse")
    library(tidyverse)
    
    library(wbstats)
    
    my_indicators <- c(
      life_exp = "SP.DYN.LE00.IN", 
      gdp_capita ="NY.GDP.PCAP.CD", 
      pop = "SP.POP.TOTL"
      )
    
    d <- wb_data(my_indicators, start_date = 2016)
    
    d %>%
      left_join(wb_countries(), "iso3c") %>%
      ggplot() +
      geom_point(
        aes(
          x = gdp_capita, 
          y = life_exp, 
          size = pop, 
          color = region
          )
        ) +
      scale_x_continuous(
        labels = scales::dollar_format(),
        breaks = scales::log_breaks(n = 10)
        ) +
      coord_trans(x = 'log10') +
      scale_size_continuous(
        labels = scales::number_format(scale = 1/1e6, suffix = "m"),
        breaks = seq(1e8,1e9, 2e8),
        range = c(1,20)
        ) +
      theme_minimal() +
      labs(
        title = "An Example of Hans Rosling's Gapminder using wbstats",
        x = "GDP per Capita (log scale)",
        y = "Life Expectancy at Birth",
        size = "Population",
        color = NULL,
        caption = "Source: World Bank"
      ) 

    < section id="using-ggplot2-to-map-wbstats-data" class="level2">

    Using ggplot2 to map wbstats data

    if (!require(rnaturalearth)) install.packages("rnaturalearth")
    if (!require(tidyverse)) install.packages("tidyverse")
    if (!require(wbstats)) install.packages("wbstats")
    
    library(rnaturalearth)
    library(tidyverse)
    library(wbstats)
    
    ind <- "SL.EMP.SELF.ZS"
    indicator_info <- filter(wb_cachelist$indicators, indicator_id == ind)
    
    ne_countries(returnclass = "sf") %>%
      left_join(
        wb_data(
          c(self_employed = ind), 
             mrnev = 1
              ),
        c("iso_a3" = "iso3c")
      ) %>%
      filter(iso_a3 != "ATA") %>% # remove Antarctica
      ggplot(aes(fill = self_employed)) +
      geom_sf() +
      scale_fill_viridis_c(labels = scales::percent_format(scale = 1)) +
      theme(legend.position="bottom") +
      labs(
        title = indicator_info$indicator,
        fill = NULL,
        caption = paste("Source:", indicator_info$source_org) 
      )

    < footer>

    Loading…

  • < !-- Load shared sidebar -->
    To leave a comment for the author, please follow the link and comment on their blog: https://pacha.dev/blog.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
    Exit mobile version