leewan
leewan

Reputation: 113

How to use sjPlot to report a html table in R shiny?

Why can't my code run successfully? How do I use sjPlot to create a html table in shiny?

library(shiny)
library(sjPlot)

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(),
        mainPanel(htmlOutput("a"))
    )
)

server <- function(input, output){
  output$a <- renderUI({
    tab_df(iris,title="title",col.header='N',footnote='footnote')
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

When I run the code , the log is "Warning: Error in if: argument is of length zero". I don't know what's wrong with the code.

Thanks.

Upvotes: 5

Views: 411

Answers (1)

MrFlick
MrFlick

Reputation: 206308

The problem is that tab_df seems to write its HTML out to a file rather than return the HTML that you want to use. Looking at the sjPlot:::print.sjTable, it seems like we can get around that with

server <- function(input, output){
  output$a <- renderUI({
    HTML(tab_df(iris,title="title",col.header='N',footnote='footnote')$knitr)
  })
}

Upvotes: 2

Related Questions