Skip to main content

Required Packages

To work with the GSS StatsBank API in R, you’ll need two key packages:
1

Install httr2

The httr2 package handles HTTP requests and responses, allowing you to communicate with the API.
install.packages("httr2")
2

Install tidyverse

The tidyverse package provides tools for data manipulation and transformation, which you’ll use to clean and reshape the API responses.
install.packages("tidyverse")
3

Load the packages

Once installed, load both packages at the beginning of your script:
library(httr2)
library(tidyverse)

Package Overview

httr2

httr2 is a modern HTTP client for R that provides a clean, pipe-friendly interface for making API requests. Key functions you’ll use:
  • request() - Initialize an API request
  • req_url_path_append() - Build URL paths incrementally
  • req_body_json() - Attach JSON query data to requests
  • req_perform() - Execute the HTTP request
  • resp_body_json() - Parse JSON responses
  • resp_body_raw() - Get raw response data (for CSV responses)
The httr2 package uses a pipe-friendly design, making it easy to chain operations together with |> or %>%.

tidyverse

The tidyverse is a collection of R packages for data science. For this guide, you’ll primarily use:
  • purrr - For functional programming (mapping, reducing)
  • readr - For reading CSV data
  • tidyr - For reshaping data (pivoting wide to long format)
  • dplyr - For data manipulation (renaming, filtering)

Verify Installation

You can verify that the packages are installed correctly by running:
packageVersion("httr2")
packageVersion("tidyverse")
If both commands return version numbers without errors, you’re ready to proceed.
Make sure you have R version 4.0 or higher installed. You can check your R version with R.version.string.

Next Steps

With your environment set up, you’re ready to start retrieving data. Continue to the Quick Start guide to make your first API request.

Build docs developers (and LLMs) love