Required Packages
To work with the GSS StatsBank API in R, you’ll need two key packages:Install httr2
The
httr2 package handles HTTP requests and responses, allowing you to communicate with the API.Install tidyverse
The
tidyverse package provides tools for data manipulation and transformation, which you’ll use to clean and reshape the API responses.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 requestreq_url_path_append()- Build URL paths incrementallyreq_body_json()- Attach JSON query data to requestsreq_perform()- Execute the HTTP requestresp_body_json()- Parse JSON responsesresp_body_raw()- Get raw response data (for CSV responses)
tidyverse
Thetidyverse 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 datatidyr- 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:Make sure you have R version 4.0 or higher installed. You can check your R version with
R.version.string.