“<-: invalid (NULL) left side of assignment" when editing cells in a dataframe

问题描述 投票:0回答:0

目标是创建一个闪亮的应用程序来记录现场数据。观察者通过 selectinput 或 radioGroupButtons 为每个个体记录物种名称、性别和大小(这里限制为 3 个选择)。一个更新按钮,创建一个绑定到反应表的一行数据框。该表显示在应用程序上以检查潜在的输入错误。该表是可编辑的,以允许观察者在发现错误时直接将数据更正到表中。

问题是当我更新一个新条目编辑表时,版本消失了,旧值又回来了,所以我添加了一个 observeEvent 来更改数据框中的值。不幸的是,我得到:“<-: invalid (NULL) left side of assignment". If i replace the value by the exact same value it doesn't return the error but if i change it the error message appears for example replace a "M" by a "F" in the sexe column.

错误
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "sp", label = "sp", choices = c("espece1", "espece2")),
      radioGroupButtons(inputId = "sexe", label = "sexe", choices = c("M", "F"), status = "custom-class"),
      radioGroupButtons(inputId = "size", label = "size", choices = c("30", "20", "10"), status = "custom-class"),
      actionButton("update", "Update Table")
    ),
    mainPanel(
      dataTableOutput("table1")
    )
  )
)

server <- function(input, output, session) {
  mydata <- reactiveVal(data.frame(sp = character(),
                                  sexe = character(),
                                  size = character(),
                                  stringsAsFactors = FALSE))
  
  observeEvent(input$update, {
    to_add <- data.frame(sp = input$sp,
                         sexe = input$sexe,
                         size = input$size)
    mydata(rbind(mydata(), to_add))
  })
  
  output$table1 <- DT::renderDataTable({
    datatable(mydata(), editable = "cell",
              options = list(pageLength = 50,
                             paging = FALSE))
  })
 
observeEvent(input$table1_cell_edit, {
    info <- input$table1_cell_edit
    row <- info$row
    col <- info$col
    value <- info$value
 
    mydata()[row,col] <- value
  })

}

shinyApp(ui, server)

以下是我的一些尝试:

  • 在 info$row 和 info$col 添加 +1 即使 rowname = TRUE 我也得到同样的错误。
  • mydata()\[row,col\] \<\<- isolate(DT::coerceValue(value, mydata()\[row,col\]))
    仍然是同样的错误。
  • 将我的反应性数据框转换为非反应性数据框我得到:
    "Error in \<-: object of type 'closure' is not subsettable"
    。 提前感谢您的帮助
r shiny dt
© www.soinside.com 2019 - 2024. All rights reserved.