首页 > 解决方案 > 在闪亮的应用程序中编辑后数据表消失

问题描述

我正在尝试在闪亮的应用程序中制作可编辑和可下载的数据表。在我编辑表格后,数据表格由于某种原因自动消失了。这只发生在数据dat是反应性的(这在我的应用程序中是必需的)时。

有谁知道发生了什么?非常感谢。

下面的示例代码:

library(shiny)
library(DT)

ui <- fluidPage(
    selectInput("nrow",
                "num of rows",
                choices = 1:5,
                selected = 3,
                multiple = FALSE),
    DTOutput("table")
)

server <- function(input, output){

    dat = reactive({
        iris[1:as.integer(input$nrow),]
    })

    output[["table"]] <- renderDT({
        datatable(dat(), editable = "cell", extensions = "Buttons", 
                  options = list(
                      dom = "Bfrtip",
                      buttons = list(
                          "csv"
                      )
                  ))
    })

    observeEvent(input[["table_cell_edit"]], {
        cellinfo <- input[["table_cell_edit"]]
        dat() <<- editData(dat(), input[["table_cell_edit"]], "table")
    })


}

shinyApp(ui, server)

标签: rshiny

解决方案


尝试这个:

library(shiny)
library(DT)

ui <- fluidPage(
  selectInput("nrow","num of rows",choices = 1:5,selected = 3,multiple = FALSE),
  DTOutput("table")
)

server <- function(input, output){

  v <- reactiveValues()
  observeEvent(input$nrow,{
    v$dat <- iris[1:as.integer(input$nrow),]
  })

  output[["table"]] <- renderDT({
    datatable(v$dat, editable = "cell", extensions = "Buttons", options = list(dom = "Bfrtip",buttons = list("excel")))
  })

  observeEvent(input[["table_cell_edit"]], {
    cellinfo <- input[["table_cell_edit"]]
    v$dat <<- editData(v$dat, input[["table_cell_edit"]], "table")
  })
}

shinyApp(ui, server)

推荐阅读