【问题标题】:crosstab output getting displayed in viewer pane only and not in Shiny app交叉表输出仅显示在查看器窗格中,而不显示在 Shiny 应用程序中
【发布时间】:2016-12-06 08:47:04
【问题描述】:

我正在生成一个交叉表输出(列联表)。我的要求是列百分比以及行和列总计。我发现这个包 "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)

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

【问题讨论】:

  • 我从来没有使用过sjPlot 包,所以当你简单地在基础 R 中绘制它时,你如何处理关于未使用参数的错误?
  • @Pork,我已经在 r studio 中运行了上面的代码,我没有收到任何错误。输出在查看器窗格中生成,而不是在 Shiny 应用程序中生成

标签: r shiny crosstab sjplot


【解决方案1】:

sjt.xtabinvisibly 返回一个列表,其中包含用于生成您在查看器中看到的表格的 html 和 css。

你可以在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 的参数已更改名称。

【讨论】:

  • 非常感谢您的回答!!你已经准备好帮忙了:) !!是的,我的 sjPlot 版本是 2.1.2,因此参数可能会有所不同。根据我的理解,尽管我们正在生成一个表格,但您能否确认您为什么使用 renderUI。考虑到我们正在生成表输出,我正在使用 renderDataTable。我们应该如何断定使用哪个渲染函数?那你怎么知道 sjt.xtab 不可见地返回了一个包含用于生成表格的 html 和 css 的列表。再次感谢您!
  • renderDataTable是用来渲染数据帧的,如果你看?sjt.xtab,在Value部分你会看到它没有返回数据帧所以renderDataTable是不对的渲染功能。该函数返回 html,因此正确的函数是 renderUi/htmlOutput。我编辑了代码,你可以直接使用custom_table$knitr,因为它有inlince CSS。
  • 谢谢!!我明白。这也适用于动态变量选择。表格正在更新!
猜你喜欢
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 2019-01-07
  • 2020-10-16
相关资源
最近更新 更多