Using APIs in R to obtain Indicators for interactive visualizations
To run this project locally, please follow these three steps below to restore project environment using {renv} library:
1-2. Clone RPYSOC_2025_Using_APIs_in_R repo using git on your IDE or terminal using local clone HTTPS option: https://github.com/Pablo-source/RPYSOC_2025_Using_APIs_in_R.git
Navigate to the cloned repo, then open Rproject by clicking on the Basic-Shiny-app.Rproj file. This will display the Shiny app files on your "Files" tab in RStudio.
2-2. Run renv::restore() in a new Rscript. The first time the app finshed running, I captured its final state using renv::snapshot() To ensure all required packages are loaded, we reinstall exact packages declared in the project lockfile renv.lock Then we run renv::restore(), this ensures we have all required packages loaded and ready in our local R environment. renv::restore() If prompted, after running restore() function, choose "1: Activate the project and use the project library." from menu displayed in the R Console.
- Conference talk slides on GitHub: https://pablo-source.github.io/RPySOC2025talk.html#/title-slide
I will showcase how to use Application Programming Interfaces (APIs) in R to gather Health, Social and Weather data.
First I introduce what are APIs, making requests to an API, understanding its response, and transforming it into a JSON file,
Ceate a new set of visualizations using ggplot and plotly libraries in R displaying MIN/MAX daily temperatures in Spain for the last 10 years using AEMET OpenDATA API for these cities: Valencia, Sevilla, Barcelona and Madrid, to display effect of extreme weather in Spain.
Using API provided by AEMET: State Meteorological Agency – AEMET – Spanish Government
https://opendata.aemet.es/centrodedescargas/inicio
We will start by requesting our API key providing and email address, they will send us the API key to that email: Selecting the Obtaining API Key from the menu below: https://opendata.aemet.es/centrodedescargas/altaUsuario?
We need to fill in the form below:

Then we will receive our API Key for AEMET OpenData API in our Inbox
From the AEMET main website, I can validate the API_Key they previously emailed to me to obtain daily temperatures data for specific Spanish cities:
Also I can test it on a new browser tab using this URL (providing my newly emailed API key): <https://opendata.aemet.es/opendata/api/observacion/convencional/todas?api_key= MY_NEW_API_KEY>
And I obtain a successful reply from AEMET API:
- After requesting and obtaining my API Key (it's sent via email), I can start querying specific Weather information using the AEMET OpenData API, such as daily temperatures for specific cities in Spain:
https://opendata.aemet.es/dist/index.html?#/predicciones-especificas/Predicci%C3%B3n%20por%20municipios%20diaria.%20Tiempo%20actual

-
See script O4 AEMET weather data API.R for details on how to use an API key and create this climate stripes charts for Valencia city.
-
GitHub {climaemet} package for reference and documentation: https://github.com/rOpenSpain/climaemet
-
The goal of climaemet is to serve as an interface to download the climatic data of the Spanish Meteorological Agency (AEMET) directly from R using their API and create scientific graphs.
- Then I can use the API Key I obtained previously from AEMET ApenData API, to extract temperature dat for specific locations with this package
- This is an example on how to use the API to obtain a Climate Stripes graph for the city of Valencia
- First we need to obtain the AEMET weather station ID for the city of Valencia from this website: https://ropenspain.github.io/climaemet/articles/aemet_stations.html
-
Then we need to setup our API using our API Key to query AEMET OpenData API to obtain specific temperature values for this particular Valencia weather station:
-
1.2 Set your API key using this first method aemet_api_key() function:
-
In the next two days I will provide an example on how to use an environment variable to store our API key as described in section below in this project
-
2.2 Set you API key with {usethis} package to include securely my API key in the .Rprofile file
-
In .Rprofile file intoduce this line: 'options(this_is_my_key = "XXXX")'
-
Remember to Restart your R session for the .Pprofile changes to take effect
-
Now I can check with {httr2} that I am able to connect to AEMET Open Data API using {hhtr2} library.
- As it returns status 200 meaning the connection has been successfully. Now I can start querying data using httr2 package* with my API KEY
-
Now I proceed to obtain weather data for Valencia using the API
-
Make sure .Rprofile is included in your .gitignore file, to avoid uploading it to Github
-
See script "04 AEMET climate stripes plots.R" for details of the ggstrips charts below:
-
Submit a query to the API using previous API key to obtain a Climate Strips chart for Valencia
- Finally this is the Climate Strtips chart for Valencia and Zaragoza as final output, the data used in this Climaet Strip plot, has been obtained performing an API query to AEMETOpenData API:https://opendata.aemet.es/centrodedescargas/productosAEMET?
- See R Script: 04 AEMET weather data API.R for details on how I created the above Climate strips chart for Valencia city.
Using {httr2} and {rsdmx} R packages to make a request to the API, interpret the response building multiple function calls, {httr2} package is the successor to {httr}.
-
httr2 Main website: https://httr2.r-lib.org/
-
httr2 Reference website: https://httr2.r-lib.org/reference/index.html
Later on data downloaded as tibble or dataframe from any API can be used in {ggplot2} and {plotly} charts, {gt} tables and {leaflet} interactive maps.
Also, In future weeks I will include materials on how to create an API request for OECD data through a RESTful API based on SDMX standard. Using {rsdmx} package to fetch data programmatically. Providing access to datasets within the catalogue of databases in SDMX format. The information downloaded from APIs can be used to create Shiny Dashboard. Using data from APIS we can feed information to dashboards displaying geographical information retrieved as an interactive {leaflet } or {ggplot2} map, and related indicators as Time series {plotly} charts and interactive {GT} tables.
Later I will create an API request string as the URL with parameters specifying the data subset to access. I will provide examples using {httr2} to communicate with any API through R, returning several indicators as a JSON file.
The talk documentation will be created as Quarto files and hosted alongside all the R script on my personal GitHub repository for people to clone and run them.
API keys are used to control how the API is being used. After requesting access to an API, you obtain a unique set of API keys assigned to your user, they authenticate and authorize you as an API user.
Some recommendations to keep your API keys secure:
- You MUST avoid committing your API Keys to a public GitHub page.
- A recommended method is to store your API keys in a special file called .Rprofile. Making use of the edit_r_profile from {usethis} package to store and edit your .Rprofile
- Make sure .Rprofile is included in your .gitignore file, otherwise it will be synced with Github
Creating a .Rprofile file in the same place as your .Rproj file:
- 'usethis::edit_r_profile(scope = "project")'
- Then you can securely enter your API Key details in the .Rprofile file
- In .Rprofile file intoduce this line: 'options(this_is_my_key = "XXXX")'
- later, in the R script: You can securely call your API keys this way: 'key <- getOption("this_is_my_key")'
Important considerations when creating your .Rprofile file:
- Make sure your .Rprofile ends with a blank line
- Make sure .Rprofile is included in your .gitignore file, otherwise it will be synced with Github
- Restart RStudio after modifying .Rprofile in order to load any new keys into memory
- Spelling is important when you set the option in your .Rprofile
This diagram from https://cran.r-project.org/web/packages/nixtlar/vignettes/setting-up-your-api-key.html explains a secure way of saving your API keys
- Storing security you API keys on your .Rprofile
Read mode about it:
- API authentication: https://info5001.infosci.cornell.edu/tutorials/store-api-keys.html
- How to store and use webservice keys and authentication details with R: https://www.r-bloggers.com/2015/11/how-to-store-and-use-webservice-keys-and-authentication-details-with-r/
We can use {usethis} package to open and save your API keys on the .Rprofile file
- install.packages("usethis")
- usethis::edit_r_environ()
- Then you only have to paste this onto your .Rprofile file:
- MY_APIKEY <- "here_goes_your_API_key"
This is a secure way of storing your API keys and making them available when working on your project
- Remeber to include this .Rprofile file on your .gitignore file.
List of online resources used to create this presentation
-
Tidyverse. httr2 1.0.0 https://tidyverse.org/blog/2023/11/httr2-1-0-0/
-
Key API concepts. Geeksforgeeks https://www.geeksforgeeks.org/r-language/access-collect-data-with-apis-in-r/
-
National Weather Service API https://www.weather.gov/documentation/services-web-api
-
How to Get Data From APIs with R and {httr2}.Author: Albert Rapp https://www.youtube.com/watch?v=hmtE4QGIOuk&t=1347s
-
How to GET data from API using R in RStudio. Author: Dean Chereden https://www.youtube.com/watch?v=AhZ42vSmDmE&t=366s