如何在Shiny App中正确创建反应性数据集列表?

时间:2019-06-05 13:58:34

标签: r shiny

我目前有几个由我的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]]

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

0 个答案:

没有答案