Huxtable package: How to prevent scientific notation in table footnote year?

I’m having trouble with the huxtable package in R. When I add a footnote with a year to my table, it shows up in scientific notation. Here’s what I’ve tried:

options(scipen = 100, digits = 10)
library(huxtable)
my_table <- huxtable(mtcars[1:5, 1:2])
number_format(my_table) <- 1
add_footnote(my_table, "No automobiles in 1776")

The footnote comes out as “No automobiles in 1.78e+03”. I thought setting number_format() would fix this, but it didn’t work. Removing it makes the whole table use scientific notation.

Putting the year in quotes causes an error. I’m stumped! How can I make the year show up normally in the footnote? Any help would be great!

As someone who’s worked extensively with R for data visualization, I’ve run into this scientific notation issue more times than I care to admit. Here’s a trick that’s always worked for me: use sprintf() to format the year as a string before adding it to the footnote. Try this:

year <- 1776
footnote <- sprintf('No automobiles in %d', year)
my_table <- add_footnote(my_table, footnote)

This approach bypasses R’s automatic number formatting entirely by treating the year as a string from the get-go. It’s simple, reliable, and doesn’t require messing with global options or risking unintended consequences elsewhere in your code. Plus, it gives you more control over how the year is displayed if you need to tweak it later.

hey there mikezhang, i had a similar issue. try wrapping the year in backticks like this: 1776. it tells R to treat it as literal text instead of a number. should fix the scientific notation problem without messing up the rest of ur table. hope this helps!

I encountered a similar issue with huxtable and found a workaround. Instead of using add_footnote(), try creating the footnote text separately and then adding it to the table. Here’s an approach that might work:

footnote_text <- paste0('No automobiles in ', format(1776, scientific = FALSE))
my_table <- add_footnote(my_table, footnote_text)

This method allows you to format the year explicitly before adding it to the footnote. The format() function with scientific = FALSE should prevent scientific notation. Let me know if this solves your problem or if you need further assistance.