Exercise: Currency Live!

Overview

In this exercise, we’ll explore creating a dynamic document that once published will continually update based on upstream data without needing to be re-run.

We’ll want to use a public web API that does not require any API key and has both HyperText Transfer Protocol Secure (HTTPS) and Cross-origin resource sharing (CORS) enabled. One such API is the Frankfurter API that tracks foreign exchange references.

Just as before, we’ll focus on using the quarto-webr extension. However, you can substitute quarto-webr with quarto-pyodide if you prefer working with Python.

Step-by-step

Web API Background

When working with web APIs, we have access to data by formatting a URL in a specific manner.

In this case, we can directly retrieve information related to a date range and a specific currency (e.g. USD) by using:

https://www.frankfurter.app/2024-05-01..?to=USD
  • 2024-05-01: denotes the start date in the format of Year-Month-Day.
  • ..: denotes a date range up to present day.
  • ?to=USD: denotes a filter to only focus on conversion from the Euro to USD.
  • Optional:
    • You can specify a closed date range using 2024-05-01..2024-05-03
    • You can omit ?to= and receive all currencies.

Downloading Data

When using WebAssembly distributions of R and Python, we have to be careful when working with remote data. In the case of R, this means either using download.file() or url() inside of readLines() to obtain data. Inside of pyodide, there are similar limitations; but, there is built-in support for handling this data call (c.f. pyodide-http).

For R’s case, we have:

This saves the file contents into currency-data.json, which sits on webR’s virtual file system. The response data we have is governed by:

Inside of Python, we need to make a request to download the data using the requests library.

Note

The quarto-pyodide uses a special version of the requests library.

Data Wrangling

From here, we need to data wrangle the JSON data. So, we’ll likely want to include a code cell that loads the necessary JSON handling package and converts the JSON file into a data frame.

For R, the jsonlite package handles the conversion from JSON into an R list through fromJSON() function. The result should be saved into currency_json.

We can further wrangle the data by using:

In Python, we only need to extract the rates key from the dictionary and use pandas construct the data frame containing rates and alongside a date.

Note

We do drop a few variables with this approach.

Once done, the problem defaults to figure out the best way to visualize and model this kind of time series data.

Tasks

Create a Quarto document powered by either webR or Pyodide. The document should:

  1. Download data starting from January 1st, 2024 and run to present day for the USD to EURO.
  2. Create graphics to visualize the time series data.
  3. (Optional) Try to model the time series data.

Finally, publish the document on Quarto Pub.

Extra

In the preceeding exercises, we only focused on working with the EURO to USD conversion rate. How can we improve the analysis (graphics / ) by allowing for all currencies rates to be present? e.g.

https://www.frankfurter.app/2024-05-01..

To help, here’s some data wrangling code: