交叉表输出仅显示在查看器窗格中,而不显示在Shiny应用程序中 [英] crosstab output getting displayed in viewer pane only and not in Shiny app

查看:137
本文介绍了交叉表输出仅显示在查看器窗格中,而不显示在Shiny应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成一个交叉表输出(列联表)。我的要求是具有列百分比以及行和列的总计。我发现该软件包 sjPlot 具有很好的色彩输出,并且满足了我的要求。我使用此包在Shiny中生成输出。但是令我惊讶的是,仅在查看器窗格中而不是在应用程序中生成了输出。以下是我的代码的一小部分。

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.

推荐答案

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

sjt.xtabinvisibly returns a list that contains the html and css used to generate the table that you see in the viewer.

您可以在ui端使用 htmlOutput renderUI 上显示它。

You can use htmlOutput on the ui side and renderUI on the server side to display it.

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

In your ui.R, you can use:

htmlOutput("crosstab2")

在您的服务器中。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 的参数已更改名称。

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.

这篇关于交叉表输出仅显示在查看器窗格中,而不显示在Shiny应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆