Look at the code
# dependencies
library(tidyverse)
library(rvest)
library(jsonlite)
library(gt)
# creating color palette
<- c(
p_pal `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
<- function(...) {
my_pal <- c(...)
cols if (is.null(cols))
return (p_pal)
p_pal[cols]
}
# accessing API
<- URLencode(paste0('https://api.exchangerate.host/latest/?base=', params$base))
url # get JSON
<- jsonlite::fromJSON(url)
currencies # turn data into tibble
<- tibble::as_tibble(currencies$rates)
rates # put in better format
<- rates %>%
rates ::pivot_longer(everything(), names_to = "currency", values_to = "rate") %>%
tidyr::mutate(base = paste0(params$base))
dplyr
# read HTML code from webpage with currency symbols
<- read_html("https://www.eurochange.co.uk/travel/tips/world-currency-abbreviations-symbols-and-codes-travel-money") %>%
symbols
html_table
# select rows from table that match currency rates
<- symbols[[1]]
symbols <- rename(symbols, currency = `Currency Code`)
symbols
# join two tables together
<- right_join(rates, symbols, by = "currency")
rates <- rates[, c(3, 2, 1, 4, 5)]
rates
# 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 | Kč |
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 | zł |
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. |