R Shiny输出动态表数

时间:2015-03-11 20:07:43

标签: r shiny

我正在编写一个Shiny应用程序来创建一个包含数据集中每个变量的表的行。变量的数量将从使用变为使用,理想情况下,应用程序将输出与变量一样多的行和表。我当前的代码是创建正确的行数和文本数,但是重复所有行的最后一个表的表数据。我认为在output[[tablename]]中存储表输出可能不适合创建单独存储的表。下一步是添加一个反应性输入来过滤vardata$category显示的行。

  • 如何为每行中的表填充正确的数据,我需要做什么?
  • 我需要做些什么来过滤哪些行显示有反应性输入?

这里的代码包括数据示例:

library(shiny)    
variable <- c("Q17r01", "Q17r01", "Q17r01", "Q22r03", "Q22r03", "Q22r03", "Q15r01", "Q15r01", "Q15r01", "S03", "S03", "vAge", "vAge", "vAge", "vAge", "vAge", "vAge")
    responses <- c("A_T", "B_M", "C_B", "A_T", "B_M", "C_B", "A_T", "B_M", "C_B", "Female", "Male", "13 - 17", "18 - 24", "25 - 34", "35 - 44", "45 - 54", "55+")
    grp1 <- c(33, 39, 28, 27, 20, 53, 88, 7, 5, 51, 49, 27, 8, 33, 14, 16, 2)
    grp2 <- c(42, 46, 12,41, 45, 13, 64, 32, 4, 44, 56, 9, 22, 39, 13, 12, 4)
    xAgg <- c(32, 49, 19, 26, 48, 26, 51, 38, 11, 45, 55, 12, 16, 30, 17, 14, 11)
    chartdata <- data.frame(variable,responses,grp1,grp2,xAgg,row.names=NULL)

    profvars <- unique(variable)
    varlabel <- c("Q17r01_I_am_overwhelmed_by_the_number_of_apps_available_for_download", "Q22r03_I_feel_overwhelmed_by_the_number_of_digital_communications_I_receive",
                  "Q15r01_When_I_receive_a_message_online_I_tend_to_respond_right_away", "S03_Please_indicate_your_gender", "vAge_Age_breakdown")
    category <- c("a_Causes_of_Stress", "a_Causes_of_Stress", "a_Communication_Availability", "zz_Demo", "zz_Demo")
    vardata <- data.frame(profvars,varlabel,category,row.names=NULL)

    chartdatasplit <- split(chartdata, chartdata$variable)


    server <- function(input, output) {

    assigntables <- reactive({
      for (vars in profvars){
        local({
          var <- vars
          tablename <- paste0("table.",var)
          assign("tabledata",chartdatasplit[[var]],pos=1)  
          output[[tablename]] <- renderTable({tabledata}) ###appears this is only being done for last table
        })
      }
    })

      output$AllVars <- renderUI({
        ##for (i_var in 1:nrow(vardata)) {
        assigntables()
        return(apply(vardata,1,function(vars){
          fluidRow(column(12,offset=1,
            tableOutput(paste("table.",vars['profvars'],sep=''))),
            hr()
          )
        }))
      })
    }

    ui <- navbarPage("Seg Run",
                     tabPanel("Summary",
                              uiOutput("AllVars")
                     )
    )

    shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:2)

我们走了:

library(shiny)
library(xtable)

variable <- c("Q17r01", "Q17r01", "Q17r01", "Q22r03", "Q22r03", "Q22r03", "Q15r01", "Q15r01", "Q15r01", "S03", "S03", "vAge", "vAge", "vAge", "vAge", "vAge", "vAge")
responses <- c("A_T", "B_M", "C_B", "A_T", "B_M", "C_B", "A_T", "B_M", "C_B", "Female", "Male", "13 - 17", "18 - 24", "25 - 34", "35 - 44", "45 - 54", "55+")
grp1 <- c(33, 39, 28, 27, 20, 53, 88, 7, 5, 51, 49, 27, 8, 33, 14, 16, 2)
grp2 <- c(42, 46, 12,41, 45, 13, 64, 32, 4, 44, 56, 9, 22, 39, 13, 12, 4)
xAgg <- c(32, 49, 19, 26, 48, 26, 51, 38, 11, 45, 55, 12, 16, 30, 17, 14, 11)
chartdata <- data.frame(variable,responses,grp1,grp2,xAgg,row.names=NULL)

profvars <- unique(variable)
varlabel <- c("Q17r01_I_am_overwhelmed_by_the_number_of_apps_available_for_download", "Q22r03_I_feel_overwhelmed_by_the_number_of_digital_communications_I_receive",
              "Q15r01_When_I_receive_a_message_online_I_tend_to_respond_right_away", "S03_Please_indicate_your_gender", "vAge_Age_breakdown")
category <- c("a_Causes_of_Stress", "a_Causes_of_Stress", "a_Communication_Availability", "zz_Demo", "zz_Demo")
vardata <- data.frame(profvars,varlabel,category,row.names=NULL)

chartdatasplit <- split(chartdata, chartdata$variable)


server <- function(input, output) {

  tableize <- function(chartdatasplit){  ###can add additional arguments like dimension - add to where this is called also and how tabledata indexes
    tables <- list()
    for (x in names(chartdatasplit)){ ##go through all individually stored variable data frames in chartdatasplit list
      tabledata <- chartdatasplit[[x]]  ###function that returns a dataframe to use in table
      tables[[as.character(x)]] <- 
        print(xtable(tabledata, caption=paste("Variable:",x)),
           type="html", include.rownames = FALSE,
           html.table.attributes='class="data table table-bordered table-condensed"',
           caption.placement="top")
    }
    return(lapply(tables,paste))    
  }

  output$tables <- renderUI({
    out <- unlist(tableize(chartdatasplit))
      return(div(HTML(out),class="shiny-html-output"))
  })
} 

ui <- shinyUI(fluidPage(

  uiOutput("tables")
))


shinyApp(ui = ui, server = server)