Reputation: 729
I am generating a cross table output (contingency table). My requirement is to have column percentages along with row and column totals. I found this package "sjPlot" which gives very good output with colour features and was meeting my requirement. I used this package to generate the output in Shiny. But to my surprise the output is getting generated only in the viewer pane and not in the app. Below is a small part of my code.
library("shiny")
library("sjPlot")
ui <- shinyUI(fluidPage(
tabPanel("First Page"),
mainPanel(tabsetPanel(id='mytabs',
tabPanel("Table",tags$b(tags$br("Table Summary" )),tags$br(),dataTableOutput("crosstab2"))
)
)
))
server <- shinyServer(function(input, output,session){
updateTabsetPanel(session = session
,inputId = 'myTabs')
output$crosstab2 <- renderDataTable({
with(mtcars,sjt.xtab(mpg, cyl,
var.labels = c("mpg","cyl"),
show.col.prc = T,
show.summary = F,
show.na = F,
wrap.labels = 50,
tdcol.col = "#f90477",
emph.total = T,
emph.color = "#3aaee0",
#use.viewer = T ##when this is false it generates a html output. Can we capture the html output and display in Shiny?
CSS = list(css.table = "border: 1px solid;",
css.tdata = "border: 1px solid;")))
})
print("created crosstab1")
})
shinyApp(ui = ui, server = server)
This generates the needed output in viewer pane. How can I get this output displayed in the app. I can not save this output as a image as in my actual program the variables are getting selected dynamically and the table output accordingly changes.
Upvotes: 2
Views: 1485
Reputation: 21433
sjt.xtab
invisibly returns a list that contains the html and css used to generate the table that you see in the viewer.
You can use htmlOutput
on the ui side and renderUI
on the server side to display it.
In your ui.R
, you can use:
htmlOutput("crosstab2")
In your server.R
:
output$crosstab2 <- renderUI({
custom_table <- sjt.xtab(mtcars$mpg, mtcars$cyl,variableLabels = c("mpg","cyl"),showColPerc = T,
showSummary = F,showNA=F,highlightTotal = T,tdcol.col = "#f90477", highlightColor ="#3aaee0",
CSS = list(css.table = "border: 1px solid;",
css.tdata = "border: 1px solid;"))
HTML(custom_table$knitr)
})
Here's (part of) my sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] sjPlot_1.9.2 shiny_0.13.1
I suspect we don't have the same version of sjPlot
as the arguments of the sjt.xtab
you use in your example have changed names.
Upvotes: 2