交叉表输出仅显示在查看器窗格中,而不显示在 Shiny 应用程序中

crosstab output getting displayed in viewer pane only and not in Shiny app

我正在生成交叉 table 输出(应急 table)。我的要求是有列百分比以及行和列总计。我发现这个包 "sjPlot" 提供了非常好的颜色特征输出并且满足了我的要求。我使用这个包在 Shiny 中生成输出。但令我惊讶的是,输出仅在查看器窗格中生成,而不是在应用程序中生成。下面是我的一小部分代码。

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)

这会在查看器窗格中生成所需的输出。如何在应用程序中显示此输出。我无法将此输出保存为图像,因为在我的实际程序中,变量是动态选择的,table 输出相应地发生变化。

sjt.xtab不可见 returns 包含 html 和 css 的列表,用于生成您在查看器中看到的 table。

可以在ui端使用htmlOutput,在服务器端使用renderUI来显示。

在您的 ui.R 中,您可以使用:

htmlOutput("crosstab2")

在你的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)
  })

这是我的(部分)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

我怀疑我们没有相同版本的 sjPlot,因为您在示例中使用的 sjt.xtab 的参数已更改名称。