使用grid.table打印表格时,如何正确关闭行名?
grid.table() from gridExtra Ah, I get it— that error happens because grid.table() doesn't accept a row.names argument directly, unlike some base R table-printing functions. Let's fix this properly with the package's built-in theme system, which controls all visual elements of the table.
The Simple Solution
You can directly hide row names by modifying the table theme to turn off the row header (which holds the row names) display:
library(gridExtra) library(grid) d <- head(iris[,1:3]) # Hide row names by disabling the rowhead element in the theme grid.table(d, theme = ttheme_default(rowhead = list(show = FALSE)))
Customizing Further (If You Want)
If you want more control over the table's appearance while hiding row names, you can build a full custom theme. For example, adjusting cell colors or alignment along with hiding row names:
custom_table_theme <- ttheme_default( core = list( bg_params = list(fill = c("white", "#f8f8f8"), alpha = 0.9), fg_params = list(hjust = 0.5, x = 0.5) ), colhead = list( bg_params = list(fill = "#e0e0e0"), fg_params = list(fontface = "bold") ), rowhead = list(show = FALSE) # This line hides the row names ) grid.table(d, theme = custom_table_theme)
Why Your Original Code Failed
The row.names = FALSE argument works for functions like print.data.frame() or knitr::kable(), but grid.table() uses a theme-based system to manage every part of the table's look. Row names are part of the "rowhead" component in the theme, so we have to target that specifically to hide them.
内容的提问来源于stack exchange,提问作者stackinator

