jrhartley
jrhartley

Reputation: 55

Display reactive htmlTable table in Shiny

I am making my first Shiny app but can't find any examples of how to display a table created using the htmlTable package. I basically want to create a table when a button is pressed and display it.

Shiny displays the html code rather than the table. I don't know what to replace "renderTable" with in the server-part and tableOutput in the ui-part.

I haven't used a normal table because I need the column spanning functionality offered in the htmlTable package.

library(shiny)
library(htmlTable)

####################  ui part ##############################################
ui <- pageWithSidebar(
headerPanel("Tables"),

sidebarPanel(
    actionButton("goButton", "Run Table")
  ),
  mainPanel(
    tableOutput("filetable")
  )
)

####################  server part #######################################    
server <- function(input,output)
{

  selectedData <- eventReactive(input$goButton, {

    # Create the table (using table from htmlTables doc as example)
    htmlTable(matrix(paste("Content", LETTERS[1:16]), 
                 ncol=4, byrow = TRUE),
          header =  paste(c("1st", "2nd",
                            "3rd", "4th"), "header"),
          rnames = paste(c("1st", "2nd",
                           "3rd", "4th"), "row"),
          rgroup = c("Group A",
                     "Group B"),
          n.rgroup = c(2,2),
          cgroup = c("Cgroup 1", "Cgroup 2&dagger;"),
          n.cgroup = c(2,2), 
          caption="Basic table with both column spanners (groups) and row 
groups",
          tfoot="&dagger; A table footer commment") 
  })

  output$filetable <- renderTable({selectedData()})

}

shinyApp(ui,server)

Upvotes: 2

Views: 5203

Answers (1)

5th
5th

Reputation: 2375

I changed two things:

  1. You need the renderUI and htmlOutput pair.
  2. I wrapped the htmlTable into a HTML-function from shiny, to tell shiny that every inside the ()-brackets is in html.

For future shiny-apps, I recommend using the cheatsheet by RStudio: https://shiny.rstudio.com/images/shiny-cheatsheet.pdf

library(shiny)

library(htmlTable)

####################  ui part ##############################################
ui <- pageWithSidebar(
  headerPanel("Tables"),

  sidebarPanel(
    actionButton("goButton", "Run Table")
  ),
  mainPanel(
    htmlOutput("filetable")
  )
)

####################  server part #######################################    
server <- function(input,output)
{

  selectedData <- eventReactive(input$goButton, {

    # Create the table (using table from htmlTables doc as example)
    HTML(
    htmlTable(matrix(paste("Content", LETTERS[1:16]), 
                     ncol=4, byrow = TRUE),
              header =  paste(c("1st", "2nd",
                                "3rd", "4th"), "header"),
              rnames = paste(c("1st", "2nd",
                               "3rd", "4th"), "row"),
              rgroup = c("Group A",
                         "Group B"),
              n.rgroup = c(2,2),
              cgroup = c("Cgroup 1", "Cgroup 2&dagger;"),
              n.cgroup = c(2,2), 
              caption="Basic table with both column spanners (groups) and row 
              groups",
              tfoot="&dagger; A table footer commment") 
    )

  })

  output$filetable <- renderUI({selectedData()})

}

shinyApp(ui,server)

Upvotes: 2

Related Questions