简体   繁体   English

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

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

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. 我发现此程序包“ sjPlot”具有很好的色彩输出并满足了我的要求。 I used this package to generate the output in Shiny. 我使用此包在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 invisibly returns a list that contains the html and css used to generate the table that you see in the viewer. sjt.xtab不可见地返回一个列表,其中包含用于生成您在查看器中看到的表的html和css。

You can use htmlOutput on the ui side and renderUI on the server side to display it. 您可以使用htmlOutput在UI端和renderUI在服务器端进行显示。

In your ui.R , you can use: ui.R ,您可以使用:

htmlOutput("crosstab2")

In your server.R : 在您的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() 这是我的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. 我怀疑我们没有与您在示例中使用的sjPlot的参数已更改名称的sjt.xtab版本相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM