如何为formattable对象的行名称设置加粗格式?
Bold Row Names in formattable Object
Got it! To make the row names bold in your formattable output, you have a couple of straightforward ways to adjust your code. Here's how you can do it:
Method 1: Target row.names directly in the formatting list
This approach lets you define a specific formatter for row names while keeping your existing cell styling intact:
library(formattable) formattable(mtcars, align = "c", list( # Add formatter for row names to make them bold row.names = formatter("span", style = ~ style(font.weight = "bold")), # Your original cell styling for all columns ~ formatter("span", style = x ~ formattable::style(display = "block", "border-radius" = "2px", "padding" = "5px", "text-align" = "center")) ))
Method 2: Use the row.names.style parameter (simpler)
If you just need basic bold styling for row names, you can use the built-in row.names.style parameter instead for a cleaner setup:
library(formattable) formattable(mtcars, align = "c", # Set row names to bold directly row.names.style = style(font.weight = "bold"), # Your original cell styling list( ~ formatter("span", style = x ~ formattable::style(display = "block", "border-radius" = "2px", "padding" = "5px", "text-align" = "center")) ))
How it works
- In Method 1, we add an entry
row.namesto the formatting list, usingformatter("span")withfont.weight = "bold"to apply bold styling specifically to row names. This gives you more flexibility if you want to add extra styling to row names later. - Method 2 uses the dedicated
row.names.styleparameter to apply the bold style concisely—perfect for simple, one-off styling needs.
Either way, your table's row names (like Mazda RX4, Mazda RX4 Wag, etc.) will now appear in bold, while your existing cell formatting stays exactly as you defined it.
内容的提问来源于stack exchange,提问作者saheed
相关产品推荐
相关产品推荐

