How to add currency symbols to columns of a table with gt()
Introduction
The gt package allows you to create beautiful, publication-ready tables in R with extensive formatting options. Adding currency symbols to numeric columns is essential when presenting financial data, making your tables more professional and easier to interpret at a glance.
Getting Started
library(gt)
library(dplyr)Example 1: Basic Currency Formatting
The Problem
You have a simple dataset with numeric values that represent monetary amounts, but they display as plain numbers without any currency context.
Step 1: Create sample data
Let’s start with basic car price data from the mtcars dataset.
car_prices <- mtcars |>
select(mpg, cyl, hp) |>
mutate(price = hp * 45 + runif(n(), 15000, 25000)) |>
slice_head(n = 5)This creates a dataset with horsepower-based pricing plus some random variation.
Step 2: Create basic table
We’ll create an initial table to see the unformatted numbers.
basic_table <- car_prices |>
gt() |>
tab_header(title = "Car Specifications and Pricing")The price column shows as plain decimal numbers without currency formatting.
Step 3: Add dollar signs
Now we’ll format the price column with US dollar symbols.
formatted_table <- basic_table |>
fmt_currency(
columns = price,
currency = "USD"
)The price column now displays with dollar signs and proper comma separators for thousands.
Example 2: Multiple Currencies and Advanced Formatting
The Problem
You’re working with international sales data that requires different currency symbols for different regions, along with custom formatting options like decimal places and number separators.
Step 1: Create international sales data
Let’s simulate sales data for different regions with varying amounts.
sales_data <- data.frame(
region = c("North America", "Europe", "Asia", "UK", "Japan"),
q1_sales = c(125000, 95000, 180000, 75000, 110000),
q2_sales = c(140000, 105000, 195000, 82000, 125000)
)This creates quarterly sales figures for different international markets.
Step 2: Apply different currencies by column
We’ll format each quarter with appropriate regional currencies.
multi_currency_table <- sales_data |>
gt() |>
fmt_currency(
columns = q1_sales,
currency = "USD",
decimals = 0
)The first quarter shows in US dollars with no decimal places for cleaner appearance.
Step 3: Add European currency formatting
Now we’ll format the second quarter data with Euro symbols.
final_table <- multi_currency_table |>
fmt_currency(
columns = q2_sales,
currency = "EUR",
decimals = 0,
sep_mark = "."
)The second quarter displays in Euros with period separators for European number formatting.
Step 4: Customize currency symbols
For more control, we can specify custom currency symbols and placement.
custom_table <- sales_data |>
gt() |>
fmt_currency(
columns = c(q1_sales, q2_sales),
currency = "USD",
placement = "left",
incl_space = TRUE
)This applies consistent dollar formatting with space between symbol and numbers across both columns.
Step 5: Add finishing touches
Complete the table with headers and styling for professional presentation.
polished_table <- custom_table |>
tab_header(
title = "International Sales Report",
subtitle = "Quarterly performance by region"
) |>
cols_label(
q1_sales = "Q1 Sales",
q2_sales = "Q2 Sales"
)The final table includes descriptive headers and properly labeled currency columns.
Summary
- Use
fmt_currency()to add currency symbols to numeric columns in gt tables - Specify different currencies with the
currencyparameter (USD, EUR, GBP, etc.) - Control decimal places with
decimalsparameter for whole numbers or precise amounts
- Customize number separators using
sep_markfor international formatting standards Position currency symbols with
placementandincl_spacefor consistent styling