【问题标题】:How to download editable data table in shiny如何下载闪亮的可编辑数据表
【发布时间】:2019-10-25 09:23:17
【问题描述】:

在数据表中,我们可以使用参数editable 使表可编辑。我正在制作一个闪亮的应用程序,其中的表格既可编辑又可下载。

我的问题是如何在编辑后下载数据表?

这是我的应用代码:

library(shiny)
library(DT)

server <- function(input, output) {

    df = iris

    output$data = DT::renderDataTable ({
        DT::datatable(df, editable = list(
            target = 'row', 
            disable = list(columns = c(1, 3, 4))
        ))

    })


    output$downloadData <- downloadHandler(
        filename = function() {
            #paste(input$dataset, ".csv", sep = "")
        },
        content = function(file) {
            write.csv(df, file, row.names = FALSE)
        }
    )
}

ui <- fluidPage(

    DT::dataTableOutput('data'),
    downloadButton("downloadData", "Download")
)

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    当您编辑名为“XXX”的数据表的单元格时,有关单元格编辑的信息在input$XXX_cell_edit 中。此信息包含已编辑单元格的索引及其新值。所以你可以这样做:

    library(shiny)
    library(DT)
    
    dat <- iris[1:3, ]
    
    ui <- fluidPage(
      downloadButton("downloadData", "Download"),
      DTOutput("table")
    )
    
    server <- function(input, output){
    
      output[["table"]] <- renderDT({
        datatable(dat, editable = "cell")
      })
    
      df <- reactiveVal(dat)
    
      observeEvent(input[["table_cell_edit"]], {
        cell <- input[["table_cell_edit"]]
        newdf <- df()
        newdf[cell$row, cell$col] <- cell$value
        df(newdf)
      })
    
      output[["downloadData"]] <- downloadHandler(
        filename = function() {
          "mydata.csv"
        },
        content = function(file) {
          write.csv(df(), file, row.names = FALSE)
        }
      )
    
    }
    
    shinyApp(ui, server)
    

    或者,正如@MrGumble 所建议的那样,您可以使用 Datatables 的嵌入式按钮而不是 downloadHandler。这更时尚。

    library(shiny)
    library(DT)
    
    dat <- iris[1:3, ]
    
    ui <- fluidPage(
      DTOutput("table")
    )
    
    server <- function(input, output){
    
      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)
    

    【讨论】:

    • 非常感谢!这行得通。但是,当我尝试制作我的应用程序中必需的数据reactive 时,表格在编辑后消失了。不知道为什么。感谢您是否可以提供帮助。我打开了另一个问题。 stackoverflow.com/questions/56547882/…
    • @zesla 因为这是另一个问题,你可以接受这个问题的答案:)
    【解决方案2】:

    您可以直接在 DT 数据表中添加下载按钮,供用户下载表中的当前数据,请参阅R Shiny: How to add download buttons in DT::renderDataTable

    但是,如果您想将编辑后的数据用于服务器端计算,那么您是在正确的轨道上,但需要使用 replaceData 将编辑后的表格保存到 data.frame 中。参见例如https://yihui.shinyapps.io/DT-edit/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 2019-09-25
      • 2020-12-24
      • 1970-01-01
      • 2015-05-22
      • 2023-03-19
      • 2020-02-01
      • 2020-07-04
      相关资源
      最近更新 更多