Skip to content

Pablo-source/RPYSOC_2025_Using_APIs_in_R

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Using-APIs-in-R-to-obtain-Indicators for visualizations

Using APIs in R to obtain Indicators for interactive visualizations

Cloning and restoring this project locally using {renv} library

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.

image

Presentation slides

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,

New section: Spain State Meteorological Agency - AEMET - OpenDATA API

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

image

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: image

Then we will receive our API Key for AEMET OpenData API in our Inbox

Validating my newly received API key

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:

image

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:

image
  • 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 image

Creating a climate stripes graph for Valencia city using climaemet library

  • 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.

image
  • Then I can use the API Key I obtained previously from AEMET ApenData API, to extract temperature dat for specific locations with this package
image image
  • 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:

image
  • 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")'

image
  • 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.

image
  • As it returns status 200 meaning the connection has been successfully. Now I can start querying data using httr2 package* with my API KEY
image image
  • 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

image
  • 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

image Valencia viveros climate stripes graph
  • See R Script: 04 AEMET weather data API.R for details on how I created the above Climate strips chart for Valencia city.

httr2 package and reference manual

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}.

image

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.

Managing API keys in a secure way

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

image
  • Storing security you API keys on your .Rprofile
image

Read mode about it:

image

Using {usethis} package to store API keys on your .Rprofile file

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.

Online resources

List of online resources used to create this presentation

About

Using APIs in R to obtain Health indicators for interactive visualizations. RPySOC 2025 + Conference Talk Slides + usethis manage api keys

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages