Creating Aesthetic GT Tables with Parameterized Markdown: Combining Currency Exchange Rates and Country Information

economics
analysis
code
data wrangling
R
data visualization
web scraping
data engineering
Exploring the synergy of web scraping and data visualization, I created an aesthetically pleasing GT table using a custom color palette. Leveraging currency exchange rate and country data, this project showcases the power of combining data sources
Published

March 6, 2023

In this post, I decided to delve into the realm of parameterized markdown and explore the art of crafting visually appealing GT tables. To kickstart this project, I began by creating a personalized color palette, inspired by the colors used in my website theme. This palette adds a touch of visual elegance to my tables.

To streamline the process of accessing the color hex codes, I developed a custom function. This function allowed me to easily retrieve the hex codes associated with common color names that I had assigned. Now armed with my custom color palette, I was ready to embark on creating captivating GT tables.

My first step was to utilize the parameter base = "USD" to generate a GT table sourced from a currency exchange rate API. This table provided valuable information on currency exchange rates relative to the US dollar. However, I wanted to enhance its value by merging it with another table I had scraped, containing country names and currency symbols for various foreign currencies. This integration would offer a comprehensive view of the currencies in a visually pleasing format.

Finally, it was time to bring all the elements together and create an aesthetically pleasing GT table. Leveraging the power of the gt package in R, I utilized the color palette function I had created to infuse my table with visually appealing hues. The combination of data from multiple sources, enriched by the vibrant color palette, resulted in a visually captivating and informative table.

Projects that involve web scraping always ignite my excitement, and this endeavor was no exception. The opportunity to create visually pleasing tables by amalgamating data from diverse sources provides the perfect excuse to sharpen my web scraping skills. Rest assured, there will be more exciting projects on the horizon.

Stay tuned for further explorations, as I continue to merge data sources and experiment with aesthetics.

Look at the code
# dependencies
library(tidyverse)
library(rvest)
library(jsonlite)
library(gt)

# creating color palette
p_pal <- c(
  `white`= "#FFFFFF",
  `mid-blue` = "#5688c1",
  `light-blue` = "#b8cee6",
  `dark-blue` = "#0454a4",
  `light-mid-blue` = "#7ca4d4")

# creating function to call upon colors in my custom palette
my_pal <- function(...) {
  cols <- c(...)
  if (is.null(cols))
  return (p_pal)
  p_pal[cols]
}



# accessing API
url <- URLencode(paste0('https://api.exchangerate.host/latest/?base=', params$base))
  # get JSON
  currencies <- jsonlite::fromJSON(url)
  # turn data into tibble
  rates <- tibble::as_tibble(currencies$rates)
  # put in better format
  rates <- rates %>% 
    tidyr::pivot_longer(everything(), names_to = "currency", values_to = "rate") %>% 
    dplyr::mutate(base = paste0(params$base))
  
# read HTML code from webpage with currency symbols
 symbols <- read_html("https://www.eurochange.co.uk/travel/tips/world-currency-abbreviations-symbols-and-codes-travel-money") %>%
  html_table

# select rows from table that match currency rates
symbols <- symbols[[1]]
symbols <- rename(symbols, currency = `Currency Code`)

# join two tables together
rates <- right_join(rates, symbols, by = "currency")
rates <- rates[, c(3, 2, 1, 4, 5)]

# create GT table  
rates %>% 
  gt() %>% 
  # add title and subtitle
  tab_header(
    title = md(paste0("**Currency Rates Relative to ", params$base,"**")),
    subtitle = ""
  ) %>%
  # align the text to center
  cols_align(
    align = "center") %>% 
  # change names of columns
  cols_label(
    currency = "Currency Code",
    rate = md("**Exchange Rate**"),
    base = "Base"
  ) %>%
  # change colors of headers and column using custom palette function
  tab_options(column_labels.background.color = my_pal("mid-blue"),
              heading.background.color = my_pal("dark-blue")) %>% 
  tab_style(
    style = list(
      cell_fill(my_pal("light-blue")),
      cell_text(weight = "bold")
    ),
    locations = cells_body(
      columns = rate
    )
  )  %>% 
  # add reference footnote
  tab_source_note(
    source_note = html("Source: <b>2023 World Currency Symbols</b>, <a href ='https://www.eurochange.co.uk/travel/tips/world-currency-abbreviations-symbols-and-codes-travel-money'>eurochange.co.uk</a>.<br>Source: <b>Foreign & Crypto Exchange Rates API</b>, <a href ='https://exchangerate.host/'>exchangerate.host</a>.")
  )
Currency Rates Relative to USD
Base Exchange Rate Currency Code Country and Currency Symbol
USD 3.673551 AED UAE-Dirham د.إ
USD 87.626888 AFN Afghanistan Afghani ؋
USD 102.730259 ALL Albania Lek Lek
USD 1.801800 ANG Netherlands Antilles Guilder ƒ
USD 239.444952 ARS Argentina Peso $
USD 1.538478 AUD Australia Dollar $
USD 1.802126 AWG Aruba Guilder ƒ
USD 1.700647 AZN Azerbaijan Manat
USD 1.822115 BAM Bosnia and Herzegovina Convertible Mark KM
USD 2.000718 BBD Barbados Dollar $
USD 1.829800 BGN Bulgaria Lev лв
USD 1.000117 BMD Bermuda Dollar $
USD 1.355563 BND Brunei Darussalam Dollar $
USD 6.907316 BOB Bolivia Bolíviano $b
USD 5.055514 BRL Brazil Real R$
USD 0.999977 BSD Bahamas Dollar $
USD 13.787583 BWP Botswana Pula P
USD 2.523183 BYN Belarus Ruble Br
USD 2.015221 BZD Belize Dollar BZ$
USD 1.357279 CAD Canada Dollar $
USD 0.910736 CHF Switzerland Franc CHF
USD 806.386307 CLP Chile Peso $
USD 7.111425 CNY China Yuan Renminbi ¥
USD 4389.569348 COP Colombia Peso $
USD 539.709769 CRC Costa Rica Colon
USD 25.748573 CUP Cuba Peso
USD 22.179874 CZK Czech Republic Koruna
USD 6.964165 DKK Denmark Krone kr
USD 54.558359 DOP Dominican Republic Peso RD$
USD 30.893718 EGP Egypt Pound £
USD 0.935531 EUR Euro Member Countries
USD 2.251981 FJD Fiji Dollar $
USD 0.803724 FKP Falkland Islands (Malvinas) Pound £
USD 0.803802 GBP United Kingdom Pound £
USD 0.804028 GGP Guernsey Pound £
USD 11.332001 GHS Ghana Cedi ¢
USD 0.803884 GIP Gibraltar Pound £
USD 7.816208 GTQ Guatemala Quetzal Q
USD 211.390661 GYD Guyana Dollar $
USD 7.830609 HKD Hong Kong Dollar $
USD 24.650289 HNL Honduras Lempira L
USD 7.046178 HRK Croatia Kuna kn
USD 346.609397 HUF Hungary Forint Ft
USD 14989.766595 IDR Indonesia Rupiah Rp
USD 3.735017 ILS Israel Shekel
USD 0.803874 IMP Isle of Man Pound £
USD 82.660736 INR India Rupee
USD 42296.519785 IRR Iran Rial
USD 139.978603 ISK Iceland Krona kr
USD 0.803349 JEP Jersey Pound £
USD 154.361075 JMD Jamaica Dollar J$
USD 139.233929 JPY Japan Yen ¥
USD 87.583677 KGS Kyrgyzstan Som лв
USD 4116.409317 KHR Cambodia Riel
USD 899.926722 KPW Korea (North) Won
USD 1319.979329 KRW Korea (South) Won
USD 1319.979329 KRW South Korean Won
USD 0.833276 KYD Cayman Islands Dollar $
USD 447.564628 KZT Kazakhstan Tenge лв
USD 17757.530195 LAK Laos Kip
USD 15098.757575 LBP Lebanon Pound £
USD 293.934903 LKR Sri Lanka Rupee
USD 169.111430 LRD Liberia Dollar $
USD 57.642500 MKD Macedonia Denar ден
USD 3518.711094 MNT Mongolia Tughrik
USD 3518.711094 MNT Moroccan-dirham د.إ
USD 45.692574 MUR Mauritius Rupee
USD 17.685800 MXN Mexico Peso $
USD 4.610431 MYR Malaysia Ringgit RM
USD 63.895728 MZN Mozambique Metical MT
USD 19.718237 NAD Namibia Dollar $
USD 461.012654 NGN Nigeria Naira
USD 36.559303 NIO Nicaragua Cordoba C$
USD 11.063370 NOK Norway Krone kr
USD 132.267272 NPR Nepal Rupee
USD 1.661563 NZD New Zealand Dollar $
USD 0.385776 OMR Oman Rial
USD 0.999836 PAB Panama Balboa B/.
USD 3.672792 PEN Peru Sol S/.
USD 56.351964 PHP Philippines Peso
USD 285.392034 PKR Pakistan Rupee
USD 4.233853 PLN Poland Zloty
USD 7212.006005 PYG Paraguay Guarani Gs
USD 3.641894 QAR Qatar Riyal
USD 4.639808 RON Romania Leu lei
USD 109.628176 RSD Serbia Dinar Дин.
USD 81.293483 RUB Russia Ruble
USD 3.751395 SAR Saudi Arabia Riyal
USD 8.333984 SBD Solomon Islands Dollar $
USD 13.998694 SCR Seychelles Rupee
USD 10.841401 SEK Sweden Krona kr
USD 1.351548 SGD Singapore Dollar $
USD 0.803509 SHP Saint Helena Pound £
USD 568.228733 SOS Somalia Shilling S
USD 37.254238 SRD Suriname Dollar $
USD 8.746670 SVC El Salvador Colon $
USD 2512.323572 SYP Syria Pound £
USD 34.648329 THB Thailand Baht ฿
USD 20.758884 TRY Turkey Lira
USD 6.787677 TTD Trinidad and Tobago Dollar TT$
USD 30.712082 TWD Taiwan New Dollar NT$
USD 36.908097 UAH Ukraine Hryvnia
USD 1.000000 USD United States Dollar $
USD 38.755819 UYU Uruguay Peso $U
USD 11406.308791 UZS Uzbekistan Som лв
USD 23491.550758 VND Viet Nam Dong
USD 2.703127 XCD East Caribbean Dollar $
USD 250.329360 YER Yemen Rial
USD 19.710999 ZAR South Africa Rand R
NA NA TVD Tuvalu Dollar $
NA NA VEF Venezuela Bolívar Bs
NA NA ZWD Zimbabwe Dollar Z$
Source: 2023 World Currency Symbols, eurochange.co.uk.
Source: Foreign & Crypto Exchange Rates API, exchangerate.host.