Matrix with row and column names not displaying row names in shiny

Hi, I'm new to shiny and thought I had a simple example to get started, but output of a matrix/array has been haunting me. I have been looking through the documentation and examples hoping to figure it out myself. But no luck. I'm having trouble with displaying a matrix/array with row names and column names in a shiny main panel.

In R I get to display what is desired:

RateTable
Present Absent Sum

  •  "0.0450"  "0.0950"  "0.1400"
    
  •   "0.0050"  "0.8550"  "0.8600"
    

Sum "0.0500" "0.9500" "1.0000"

While in shiny, I get:

Present Absent Sum
0.0450 0.0950 0.1400
0.0050 0.8550 0.8600
0.0500 0.9500 1.0000

where the row names are missing.

Here is the current app

library(shiny)

ui <- fluidPage(

*Input() functions,

titlePanel("2x2 table properties"),

sidebarLayout(

sidebarPanel(
  numericInput("sens", "Sens:", 0.90),
  numericInput("spec", "Spec:", 0.90),
  numericInput("prev", "Prev:", 0.05),
),

mainPanel(

*Output() functions

tableOutput("myTable")
),
), )

server <- function(input, output ) {
output$myTable <- renderTable( {

RateTable <- matrix(  c(# 4 calculated elements here), ncol=2, byrow=TRUE) 

colnames(RateTable) <- c("Present", "Absent")
rownames(RateTable) <- c("+", "-")

RateTable <- as.table(RateTable)
RateTable <- addmargins(RateTable)

})
}

Any direction, even to documents I did not find would be appreciated. Thank you. - Jim

Hi,

Welcome to the RStudio community!

It can be a bit of a learning curve, but once you get the hang of it, Shiny is very powerful!
Here is an example how to show the row names of a data frame or matrix in the renderTable function

library(shiny)

ui <- fluidPage(
  
  tableOutput("myTable")
  
)

server <- function(input, output, session) {
  
  output$myTable = renderTable({
    data = matrix(runif(20), nrow = 5)
    colnames(data) = paste("Col", 1:4)
    rownames(data) = paste("Row", 1:5)
    data
  }, rownames = TRUE)
  
}

shinyApp(ui, server)

The key is setting rownames = TRUE in the renderTable function. That's all :slight_smile:

Hope this helps,
PJ

1 Like

Thank you, PJ. That worked great. I'm not sure why I missed that in the documentation. I'll look better next time. Have a great day. - Jim

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.