[英]R Shiny output dynamic number of tables
I am writing a Shiny app to create a row containing a table for each variable in a dataset. 我正在编写一个Shiny应用程序,以为数据集中的每个变量创建一个包含表的行。 The number of variables will change from use to use, and ideally the app will output as many rows and tables as there are variables.
变量的数量会因使用而异,并且理想情况下,应用程序将输出与变量一样多的行和表。 My current code is creating the correct number of rows and text, but is repeating the table data for the last table for all rows.
我当前的代码正在创建正确数量的行和文本,但是正在为所有行的最后一个表重复表数据。 I believe that the storing of table outputs in
output[[tablename]]
may not be in the right place to create separately stored tables. 我认为,将表输出存储在
output[[tablename]]
可能不适合创建单独存储的表。 The next step will be to add a reactive input to filter the rows shown by vardata$category
. 下一步将添加反应性输入以过滤
vardata$category
所示的行。
Code here includes data example: 这里的代码包括数据示例:
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)
Here we go: 开始了:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.