使用 observeEvent() editData() 时出现可编辑单元格错误的 DT "Warning: Error in split.default: first argument must be a vector"

DT with editable cells error with observeEvent() editData() "Warning: Error in split.default: first argument must be a vector"

我正在尝试创建一个闪亮的应用程序,其中包含可编辑的单元格,其中基础数据框会根据用户输入进行更新。我问了一个 and was pointed to this link.

我的应用程序:

library(shiny)
library(tidyverse)
library(DT)

ui <- fluidPage(
    
    # Application title
    titlePanel("blah"),
    
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
            DT::DTOutput('ex_table'),
        )
    )
)

server <- function(input, output,session) {
    
    example_data <- data.frame(x = rnorm(10, 0, 1) %>% round) %>% mutate(y = x + 1)
    output$ex_table <- DT::renderDT(example_data, selection = 'none', editable = TRUE)
    
    # from https://yihui.shinyapps.io/DT-edit/
    observeEvent(input$ex_table_cell_edit, {
        example_data <<- editData(example_data, input$ex_table, 'ex_table', rownames = FALSE)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

当您在 rstudio 中按 运行 时,此应用程序会加载。但是当尝试编辑 x 列中的单元格时,应用程序崩溃并显示错误消息 'Warning: Error in split.default: first argument must be a vector'.

这是问题代码块:

# from https://yihui.shinyapps.io/DT-edit/
observeEvent(input$ex_table_cell_edit, {
    example_data <<- editData(example_data, input$ex_table, 'ex_table', rownames = FALSE)
})

屏幕:

应用程序加载正常。由于数据框定义,Y 始终为 x + 1:

example_data <- data.frame(x = rnorm(10, 0, 1) %>% round) %>% mutate(y = x + 1)

当用户编辑 x 列时,我希望 y 列更新为 x 加一:

当我按下回车键时,期望的行为是让 y = 101。

根据 link 的建议,https://yihui.shinyapps.io/DT-edit/,我更愿意使用 editData() 而不是我之前 post 中提供的内容,因为 editData() 方法看起来更简单,更易读。

但是当我尝试它时,我闪亮的应用程序总是崩溃?

如果将 rownames=FALSE 放入 output$ex_table,您现有的程序可以正常工作。但是,它只允许您编辑 table 个单元格。如果你仍然想保持依赖关系 y=x+1,你需要像@Waldi 之前在他的回答中所做的那样定义。另外,一旦修改,需要通过Proxy的replaceData()反馈给输出或者定义一个reactiveValues对象,如下图

ui <- fluidPage(
  
  # Application title
  titlePanel("blah"),
  
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      DTOutput('ex_table'),
    )
  )
)

server <- function(input, output,session) {
  DF1 <- reactiveValues(data=NULL)
  example_data <- data.frame(x = rnorm(10, 0, 1) %>% round) %>% mutate(y = x + 1)
  DF1$data <- example_data
  output$ex_table <- renderDT(DF1$data, selection = 'none', editable = TRUE, rownames = FALSE)
  
  observeEvent(input$ex_table_cell_edit, {
    
    info = input$ex_table_cell_edit
    str(info)
    i = info$row
    j = info$col + 1  ## offset by 1 

    example_data <<- editData(example_data, input$ex_table_cell_edit, 'ex_table', rownames = FALSE)
    if(j==1){example_data[i,j+1]<<-as.numeric(example_data[i,j])+1}  ###  y = x + 1 dependency
    
    DF1$data <- example_data
  })
}

# Run the application 
shinyApp(ui = ui, server = server)