1

I have simple application to calculate summary , the summary part works fine. how ever i want output the results back to the UI as html. I cant get it to work with renderUI and htmloutput. if i use rendertable and tableoutput i get a partial output with headers missing and html tables are not resolved. Any pointers?

library(qwraps2) #for the summary table
library(shiny)
library(dplyr)

ui <- fluidPage( tabsetPanel(
   tabPanel("Summary", 
      # mainPanel(tableOutput('summarytab'))
      mainPanel(htmlOutput('summarytab'))
       )
  )
  )

server <- function(input, output){

  output$summarytab <- renderUI({

   our_summary1 <- 
    list("Miles per gallon" = 
         list("min" = ~ min(mpg), 
              "max" = ~ max(mpg),
              "mean (sd)" = ~ qwraps2::mean_sd(mpg)),
        "Cylinder" = 
         list("min" = ~ min(cyl), 
              "max" = ~ max(cyl),
              "mean (sd)" = ~ qwraps2::mean_sd(cyl))

    )  
   summary_table(filter(mtcars),our_summary1)
})

 } 

shinyApp(ui = ui, server = server)
Peter
  • 7,460
  • 2
  • 47
  • 68
Ashr
  • 33
  • 4

1 Answers1

3

Wow, it was hard. Here is the solution I've found.

library(qwraps2) 
options(qwraps2_markup = "markdown")
library(shiny)
library(dplyr)
library(knitr) # for knit2html

ui <- fluidPage( tabsetPanel(
  tabPanel("Summary", 
           # mainPanel(tableOutput('summarytab'))
           mainPanel(uiOutput('summarytab'))
  )
)
)

server <- function(input, output){

  output$summarytab <- renderUI({

    our_summary1 <- 
      list("Miles per gallon" = 
             list("min" = ~ min(mpg), 
                  "max" = ~ max(mpg),
                  "mean (sd)" = ~ qwraps2::mean_sd(mpg)),
           "Cylinder" = 
             list("min" = ~ min(cyl), 
                  "max" = ~ max(cyl),
                  "mean (sd)" = ~ qwraps2::mean_sd(cyl))

      )  
    stable <- summary_table(filter(mtcars),our_summary1)
    HTML(knit2html(text=capture.output(stable), fragment.only=TRUE))
    # do not forget 'capture.output'
  })

} 

shinyApp(ui = ui, server = server)

The rendering is nice:

enter image description here

PS: Looks like there's another possible rendering. But I don't know how to do it, and whether it is possible with shiny.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225