ShinyApp中无法显示表格行名的技术求助
Hey there! I noticed your Shiny app isn’t displaying row names for your table—this is a quick fix since DataTables don’t show row names by default. Here’s how to resolve it:
The Issue
By default, the renderDataTable() function (which uses the DT package under the hood) hides row names to keep tables clean. To make your custom row labels (a-f) visible, you just need to explicitly enable this setting.
Solution 1: Quick Toggle with rownames = TRUE
You can directly pass the rownames parameter to renderDataTable() to turn row names on. Here’s your modified server code:
server <- function(input, output) { output$table <- renderDataTable({ myData }, rownames = TRUE) # This line enables row names }
Solution 2: Full Control with datatable()
If you want more flexibility over table styling or options, use the datatable() function from the DT package (just remember to load it first). This lets you explicitly enable row names and tweak other settings later:
library(shinydashboard) library(shiny) library(DT) # Add this line to load the DT package myData <- matrix( data=c('13,867','$229,153','30,128','$16.53','98.17%','39.69%'), nrow = 6, ncol = 1, dimnames = list(letters[1:6], c("Metrics")) ) ui <- dashboardPage( dashboardHeader(), dashboardSidebar(), dashboardBody( box(dataTableOutput("table")) ) ) server <- function(input, output) { output$table <- renderDataTable({ datatable(myData, rownames = TRUE) # Enable row names here }) } shinyApp(ui, server)
Either approach will make your row names a through f appear alongside your metrics data.
内容的提问来源于stack exchange,提问作者AM_123

