【问题标题】:Shiny App Error in ObserveEvent? Error in [.default: invalid subscript type 'list'ObserveEvent 中的闪亮应用程序错误? [.default: 无效下标类型“列表”中的错误
【发布时间】:2023-03-29 15:57:01
【问题描述】:

我正在编写一个闪亮的应用程序,其中一部分包括用户输入文本以模仿 R 代码以及应用程序本身从该输入中挑选出某些单词以打印与用户调用的内容相关的向量。但是,当我尝试在应用程序中输入任何单词并按下操作按钮时,它会使程序崩溃并返回错误:Warning: E​​rror in [.default: invalid subscript type '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 <- 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

预期输出:

【问题讨论】:

  • 您的用户界面有一些格式错误...可能忘记了sidebarPanel()
  • 糟糕,抱歉,我在将代码粘贴到此处时不小心删除了该内容,因为应用程序/ui 中还有其他我未包含的内容。不过,这不是问题。
  • 部分问题是cominput 是一个列表,您没有使用此行正确索引:c(my.data[cominput[i]])。但是你想看到什么行为?你能提供预期的输入和输出吗?
  • 哦,是的,我明白了——我想最初我计划将它作为向量传递(这不起作用),当我将它放入列表时我没有正确更改它。它似乎仍然没有像我希望的那样做。我添加了一个示例代码,没有 Shiny,我希望它打印。 “dubbo”充当用户在实际应用中输入的字符串/文本。

标签: r list shiny stringr shiny-reactivity


【解决方案1】:

您在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)

【讨论】:

  • 几乎!它确实会打印,但由于某种原因,它只会打印列表中调用的第一个变量。我认为通过取出 printables[i]
  • 请看我的编辑。虽然,我仍然不确定预期的输出。也许您可以为您的问题添加一个示例?
  • 嗨,我编辑了开头的帖子以包含我希望它打印的屏幕截图,以及打印它的代码,没有 Shiny。 “Dubbo”充当用户在 Shiny 中输入的输入/字符串。我希望这有助于澄清我的意思。
  • 代码现在应该可以工作了。我在上面做了一些小的编辑(一旦它们被批准) - 你只得到一个变量的数据,因为在1:length(compinput) 中,长度始终为 1,尽管 1 个元素具有与命名变量一样多的组件。使用unlist() 得到了正确的号码。其次,出于同样的原因,您总是希望访问cominput[[1]],但需要ith 组件来匹配您的变量。 @ismirsehregal 非常接近,所以我只是在此处提供了编辑以提供工作代码。
猜你喜欢
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 2015-11-01
  • 2019-11-16
相关资源
最近更新 更多