ObserveEvent 中闪亮的应用程序错误? [.default 中的错误:无效的下标类型 'list'

Shiny App Error in ObserveEvent? Error in [.default: invalid subscript type 'list'

我正在编写一个闪亮的应用程序,其中一部分包括用户输入文本以模仿 R 代码,应用程序本身从该输入中挑选出某些单词以打印与用户正在调用的内容相关的向量。但是,当我尝试在应用程序中输入任何单词并按下操作按钮时,它会使程序崩溃并出现 return 错误:警告:[.default: 无效下标类型 'list' 中的错误,其中指示它在 observeEvent 处理程序中。事件中有一个列表,但我曾一度取消列出它,因为我无法按照我原本打算的方式使用它,而且我不确定这会如何干扰应用程序或使应用程序崩溃。我在下面提供了应用程序代码的相关部分:

 library(shiny)
 library(stringr)

 site <- c(rep("A", 5), rep("B", 5), rep("C", 5), rep("D", 5))
 my.num <- 1:20
 temp <- rnorm(20, 5, 1)
 growth <- 5*temp + rnorm(20, 0, 2)

  my.data <- data.frame(site = site, my.num = my.num, temp = temp, growth = growth)

 ui <- pageWithSidebar(
     headerPanel('Data Wrangler'), 
        sidebarPanel(
       p("It is important to use the right commands to be able to properly format
           your data. Let's see what it looks like when we try to use the combine function (c) tp join our variables
            instead, for instance:"),
   textInput("var.com", "Combine several of the variables using c():", NULL),
    actionButton("go6", "GO!")
   ), 
  mainPanel(
    textOutput("display2")
  ))

 server <- function(input, output, session) {

 buttonValue <- reactiveValues(go6=FALSE)

   observeEvent(input$go6, {

     isolate({
       buttonValue$go6 = TRUE
     })

     games <- names(my.data)
     tofind <- paste(games, collapse="|")

     cominput <- str_extract_all(input$var.com, tofind)

     printables <- NULL


    for (i in 1:length(cominput)){


       printables[i] <- c(my.data[cominput[i]])
       printables

     }

     working <- unlist(printables)




      output$display2 <- renderText(
      is.not.null <- function(x) !is.null(x),

      if (is.not.null(working)) {
        print(working)
      } else {
        print("Sorry, this is incorrect; check your signage.")
      }
    )





    session$onSessionEnded({
     stopApp
   }) 

 })
 }

 shinyApp(ui = ui, server = server)

所有这些都按预期工作,但没有包含 Shiny 元素,因此这与 Shiny 反应性有关,没有处理其中的某些元素。如有任何帮助,我们将不胜感激!

编辑:下面我包含了一些预期输出的屏幕截图,使用的是传递给 Shiny 之前的代码。它应该能够获取任何变量名称("site," "temp," "growth")等,并将它们拼在一起并将它们打印为一个长向量,以模拟如果你会发生什么只是试图将它们与 c() 结合起来。此输出的演示代码如下:

   library(stringr)

   site <- c(rep("A", 5), rep("B", 5), rep("C", 5), rep("D", 5))
   my.num <- 1:20
   temp <- rnorm(20, 5, 1)
   growth <- 5*temp + rnorm(20, 0, 2)

   my.data <- data.frame(site = site, my.num = my.num, temp = temp, growth = growth)

dubbo <- c("temp", "my.num")
 games <- names(my.data)

   tofind <- paste(games, collapse="|")

    secondinput <- str_extract_all(dubbo, tofind)
    printables <- NULL


   for (i in 1:length(secondinput)){


     printables[i] <- c(my.data[secondinput[[i]]])
     printables

    }

  susus <- NULL

   susus <- unlist(printables)
    susus

预期输出:

您在 str_extract_all 之后遗漏了一些错误处理,并且您试图以错误的方式访问 cominput(这是一个 list())的元素。

这是否符合您的预期?:

library(shiny)
library(stringr)

site <- c(rep("A", 5), rep("B", 5), rep("C", 5), rep("D", 5))
my.num <- 1:20
temp <- rnorm(20, 5, 1)
growth <- 5 * temp + rnorm(20, 0, 2)

my.data <-
  data.frame(
    site = site,
    my.num = my.num,
    temp = temp,
    growth = growth
  )

ui <- pageWithSidebar(
  headerPanel('Data Wrangler'),
  sidebarPanel(
    p(
      "It is important to use the right commands to be able to properly format
           your data. Let's see what it looks like when we try to use the combine function (c) tp join our variables
            instead, for instance:"
    ),
    textInput("var.com", "Combine several of the variables using c():", NULL),
    actionButton("go6", "GO!")
  ),
  mainPanel(textOutput("display2"))
)

server <- function(input, output, session) {
  buttonValue <- reactiveValues(go6 = FALSE)

  observeEvent(input$go6, {
    isolate({
      buttonValue$go6 = TRUE
    })

    games <- names(my.data)
    tofind <- paste(games, collapse = "|")

    cominput <- str_extract_all(input$var.com, tofind)

    printables <- list(NULL)

    if (identical(cominput, list(character(0)))) {
      working <- NULL
    } else {
      for (i in 1:length(unlist(cominput))) {
        printables[i] <- c(my.data[cominput[[1]][i]])
      }
      working <- unlist(printables)
    }

    output$display2 <- renderText(if (!is.null(working)) {
      print(working)
    } else {
      print("Sorry, this is incorrect; check your signage.")
    })

    session$onSessionEnded({
      stopApp
    })

  })
}

shinyApp(ui = ui, server = server)