How to format currency in gt tables in R
Introduction
The gt package provides powerful tools for creating beautiful, publication-ready tables in R. One of its most useful features is fmt_currency(), which automatically formats numeric values as currency with proper symbols, decimal places, and locale-specific formatting. This function is essential when creating financial reports, dashboards, or any data presentation involving monetary values.
Loading Required Libraries
First, let’s load the necessary packages for this tutorial:
library(gt)
library(tidyverse)Creating a Basic Data Table
Let’s start with a simple dataset containing stock symbols and their prices:
stock_data <- tibble(
symbol = c("GOOG", "META", "MSFT"),
price = c(168, 465, 425)
)
stock_dataThis creates a basic tibble with three tech stocks and their current prices in dollars.
Converting to a gt Table
Now let’s convert our data frame into a gt table for better presentation:
stock_data |>
gt()The gt() function transforms our tibble into a formatted HTML table, but the prices still appear as plain numbers without currency formatting.
Basic Currency Formatting
To format all numeric columns as currency, we can use fmt_currency() without specifying columns:
stock_data |>
gt() |>
fmt_currency()This applies dollar formatting to all numeric columns, adding the $ symbol and two decimal places.
Targeting Specific Columns
For more control, specify which columns to format using the columns parameter:
stock_data |>
gt() |>
fmt_currency(columns = price)This approach is safer when you have multiple numeric columns but only want to format certain ones as currency.
Working with Multiple Currencies
Let’s create a more complex example with multiple currency columns:
stock_data_multi <- tibble(
symbol = c("GOOG", "META", "MSFT"),
price_USD = c(168, 465, 425)
) |>
mutate(price_INR = price_USD * 80)
stock_data_multiThis creates a dataset with prices in both US dollars and Indian rupees.
Exploring Available Currency Options
Before formatting different currencies, let’s see what currency symbols are available:
info_currencies(type = "symbol")This function shows all supported currency symbols and their corresponding names in the gt package.
Formatting Multiple Currencies
Now let’s format each currency column appropriately:
stock_data_multi |>
gt() |>
fmt_currency(columns = price_USD) |>
fmt_currency(columns = price_INR, currency = "rupee")Each fmt_currency() call can target different columns and apply different currency symbols, making it easy to create multi-currency tables.
Dynamic Currency Formatting
For more advanced use cases, you can format currencies dynamically based on data in your table:
multi_locale_data <- tibble(
amount = rep(50.84, 5),
currency = c("JPY", "USD", "GHS", "KRW", "CNY"),
locale = c("ja", "en", "ee", "ko", "zh")
)
multi_locale_dataThis creates a dataset with the same amount in different currencies and their corresponding locales.
Using Locale-Based Formatting
Finally, let’s format the currencies using their appropriate locales and hide the helper columns:
multi_locale_data |>
gt() |>
fmt_currency(
columns = amount,
locale = from_column(column = "locale")
) |>
cols_hide(columns = locale)The from_column() function tells gt to use values from the locale column to determine formatting, while cols_hide() removes the helper column from the final display.
Summary
The fmt_currency() function in gt provides flexible currency formatting options, from basic dollar formatting to complex multi-locale presentations. Key features include targeting specific columns, using different currency symbols, and dynamic formatting based on data values. These tools make it easy to create professional financial reports and data presentations with properly formatted monetary values.