【问题标题】:Edit a datatable in shiny throwing an error : Can't subset with `[` using an object of class NULL编辑闪亮的数据表抛出错误:Can't subset with `[` using an object of class NULL
【发布时间】:2019-12-13 22:50:53
【问题描述】:

我正在尝试在服务器端编辑数据表(DT:datatable),并且我正在使用在前端呈现的反应式数据表。现在我想编辑数据表并从编辑的数据表中检索信息。这是一个可重复的示例,说明我在 reprex 中的 cmets 中提到的要实现的目标:

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

d <- iris

ui <- fluidPage(

    dataTableOutput("table1"),
    dataTableOutput("table2")
)

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

    # This is the main table I would want to display in its full context
    output$table1 <- renderDataTable(
        datatable(d)
    )

    get_row <-reactive({
        d %>% slice(input$table1_rows_selected)})

    # Here the table with row selected from table 1 is displayed
    output$table2 <- renderDataTable({
        datatable(get_row(),
                  editable = TRUE)
    })

    # Now as the cell gets edited in table 2, i want the edited value to show and make the last column values = NA
    proxy <- dataTableProxy("table2")

    observeEvent(eventExpr = input$table2_cell_edit, handlerExpr = {
        x <- isolate(get_row())
        info = input$table2_cell_edit
        i = info$row
        j = info$column
        v = info$value
        x[i, j] <<- DT::coerceValue(v, x[i, j])
        x <- x %>% mutate(Species = NA)
        replaceData(proxy, x, resetPaging = FALSE)  
    })
}

shinyApp(ui = ui, server = server)

我收到一个错误

【问题讨论】:

  • 嗨!欢迎来到堆栈溢出!这可能是一个简单的问题,但您更有可能通过可重现的最小示例获得帮助。我建议使用仅更新可编辑表(可能带有玩具数据)的完整应用程序(UI 和服务器)。说了这么多,如果你需要做你想做的那种分配,你可以考虑使用reactiveValues()作为你的data对象。
  • 嗨@RyanMorton 感谢您的回复。我已经更改了代码并提供了一个可重现的示例。让我知道您是否可以提供帮助

标签: datatable shiny dt


【解决方案1】:

解决方案归结为您的input$table2_cell_edit 名称。它使用col 而不是column。该错误创建了一个空的x,它无法接受您分配的新值。我使用了一个更简单的新值赋值。

以后,可以使用print() 函数打印出应用中的变量,以确定下游传递或未传递的内容。这就是 a 发现此错误的方法。

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

d <- iris

ui <- fluidPage(

  dataTableOutput("table1"),
  dataTableOutput("table2")
)

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

  # This is the main table I would want to display in its full context
  output$table1 <- renderDataTable(
    datatable(d)
  )

  get_row <-reactive({
    req(input$table1_rows_selected)
    d %>% slice(input$table1_rows_selected)})

  # Here the table with row selected from table 1 is displayed
  output$table2 <- renderDataTable({
    req(get_row)
    datatable(get_row(),
              editable = TRUE)
  })

  # Now as the cell gets edited in table 2, i want the edited value to show and make the last column values = NA
  proxy <- dataTableProxy("table2")

  observeEvent(eventExpr = input$table2_cell_edit, handlerExpr = {
    x <- isolate(get_row())
    info = input$table2_cell_edit
    i = info$row
    ### info uses 'col' not 'column'
    j = info$col
    v = info$value
    ### used a base R subset assignment 
    x[i, j] <- v
    x <- x %>% mutate(Species = NA)
    replaceData(proxy, x, resetPaging = FALSE)
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2012-01-02
    • 2022-10-02
    • 2019-08-01
    • 2018-05-09
    • 2020-12-12
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多