首页 > 解决方案 > 如何在 Shiny App 中正确创建反应性数据集列表?

问题描述

我目前有几个不同的反应数据集,它们是由我的 Shiny 应用程序创建的,并且正在尝试构建一个函数来拉取适当的数据集和renderHTML. 我目前正在努力对我正在创建的这个反应数据集列表进行子集化。

我尝试使用listofsurveydata下面的反应性数据集列表 ( ),但总是收到Error in .subset2: no such index at level 1错误消息。

lcSurveyDataSplit <- function(panelnumber){

colind <- grep(input$lcstateselect[panelnumber],colnames(inputTable$data))

predata <- inputTable$data %>%
  filter(
    grepl(search,.[[colind]]),
    is.null(input$lccategoryselect)| Category %in% input$lccategoryselect,
    is.null(input$lcsubcategoryselect)| Sub_category %in% input$lcsubcategoryselect,
    is.null(input$lcquestionselect)| Question %in% input$lcquestionselect
  )
  }

  lcsurveydata1 <- reactive({lcSurveyDataSplit(1)})
  lcsurveydata2 <- reactive({lcSurveyDataSplit(2)})
  lcsurveydata3 <- reactive({lcSurveyDataSplit(3)})
  lcsurveydata4 <- reactive({lcSurveyDataSplit(4)})

  lcSplitPanel <- function(panelnumber){ # sfnumber stands for "surveyFunction number"

listofsurveydata <- list(lcsurveydata1(), lcsurveydata2(), lcsurveydata3(), lcsurveydata4())

surveyFunction <- listofsurveydata[[panelnumber]]

questioncount <- nrow(surveyFunction)

state <- grep(input$lcstateselect[panelnumber], colnames(inputTable$data))

survey <- character(0)

i = 1

while (i <= questioncount) {

  category = surveyFunction[i,2]
  subcategory = surveyFunction[i,3]
  question = surveyFunction[i,4]
  answer = gsub("[\r\n]", "</br>", surveyFunction[i,state])

  if (length(grep(category,survey, fixed = T)) > 0) {

    survey <- paste0(survey,"</br>",
                     "</br><b>", subcategory, "</b>",
                     "</br><b>", question, "</b>",
                     "</br><i>", answer, "</i>","</br>")

  } else {

    survey <- paste0(survey,"</br>",
                     "<b><u>",category, "</u></b>",
                     "</br></br><b>", subcategory, "</b>",
                     "</br><b>", question, "</b>",
                     "</br><i>", answer, "</i>","</br>")
  }

  i = i + 1

}

HTML(paste0("<h4><b>",input$lcstateselect[panelnumber],"</b></h4>",survey))

  }

  output$lcstate1 <- renderUI({lcSplitPanel(1)})  
  output$lcstate2 <- renderUI({lcSplitPanel(2)})
  output$lcstate3 <- renderUI({lcSplitPanel(3)})  
  output$lcstate4 <- renderUI({lcSplitPanel(4)})

我已经能够将错误缩小到这两行,因为如果我不对数据进行子集化并对数据集进行字面引用,则代码可以正常工作listofsurveydata

listofsurveydata <- list(lcsurveydata1(), lcsurveydata2(), lcsurveydata3(), lcsurveydata4())

surveyFunction <- listofsurveydata[[panelnumber]]

理想情况下,我不想为每个输出对象创建一个新函数,所以我想知道应该如何解决这个错误?

标签: rshiny

解决方案


推荐阅读