简体   繁体   English

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

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

I am writing a Shiny App, part of which includes the user inputting text to mimic R code and the app itself picking out certain words from this input to print a vector related to what the user is calling. 我正在编写一个Shiny App,该应用的一部分包括用户输入文本来模仿R代码,并且该应用本身从该输入中挑选出某些单词来打印与用户正在调用的内容有关的向量。 However, when I try to input any words into the app and press the action button, it will crash the program and return the error: Warning: Error in [.default: invalid subscript type 'list', with an indication that it is in the observeEvent handler. 但是,当我尝试向应用程序中输入任何单词并按操作按钮时,它将使程序崩溃并返回错误:警告:[.default:错误的下标类型'list'中存在错误,并指出它位于watchEvent处理程序。 There is a list within the event, but I unlist it at one point as I cannot work with it in the way I intended otherwise, and I am not sure how this is interfering with or crashing the app. 该事件中有一个列表,但由于无法按照我原本打算的方式使用它,因此我暂时取消了列表的列表,而且我不确定这是如何干扰或崩溃该应用程序的。 I provided the relevant part of the app code below: 我在下面提供了应用代码的相关部分:

 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)

All of this works as intended without the Shiny elements incorporated, so it is something to do with the Shiny reactivity not handling some element of this. 所有这些工作都按预期方式进行,而没有合并Shiny元素,因此这与Shiny反应性没有处理其中的某些元素有关。 Any help would be appreciated! 任何帮助,将不胜感激!

Edit: Below I included a screenshot of some of the expected output, using the code before it is passed to Shiny. 编辑:在下面,我包括了一些预期输出的屏幕快照,使用了将代码传递给Shiny之前的代码。 It should be able to take any of the variable names ("site," "temp," "growth") etc., and smash them together and print them as a long vector, to simulate what would happen if you just tried to combine them with c(). 它应该能够接受任何变量名(“ site”,“ temp”,“ growth”)等,然后将它们粉碎在一起并打印为长向量,以模拟如果您尝试将其组合在一起会发生的情况他们与c()。 The demo code for this output is as follows: 此输出的演示代码如下:

   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

Expected Output: 预期产量: 在此处输入图片说明

You are missing some error handling after str_extract_all and you were trying to access the elements of cominput (which is a list() ) in a wrong way. str_extract_all之后,您缺少一些错误处理,并且试图以错误的方式访问cominput的元素(这是一个list() )。

Does this do what you expect?: 这符合您的期望吗?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM