R Shiny 中的 dataTableProxy 到 data.frame 错误

dataTableProxy to data.frame error in R Shiny

我创建了一个演示应用程序,允许用户编辑现有的 df,然后将更新的 table 下载为 .csv。该应用程序运行几乎正常,因为当我单击 download 按钮时,出现以下错误:

Warning: Error in as.data.frame.default: cannot coerce class ‘"dataTableProxy"’ to a data.frame


[No stack trace available]

如何解决这个问题?

代码

# Double click in a table cell to edit its value and then download the updated table

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

# Define UI for application that edits the table
        ui = fluidPage(
            DTOutput('x1'),
            
        # App title 
        titlePanel("Downloading Data"),
        
        # Sidebar layout with input and output definitions 
        sidebarLayout(
            
            # Sidebar panel for inputs 
            sidebarPanel(
                
                # Input: Choose dataset 
                selectInput("dataset", "Choose a dataset:",
                            choices = c("Demo Table")),
                
                # Button
                downloadButton("downloadData", "Download")
                
            ),
            
            # Main panel for displaying outputs 
            mainPanel(
                
                tableOutput("table")
                
            )
        ))
            
        # Define server logic required
        server = function(input, output) {
            x = iris
            x$Date = Sys.time() + seq_len(nrow(x))
            output$x1 = renderDT(x, selection = 'none', editable = TRUE)
            
            proxy = dataTableProxy('x1')
            
            observeEvent(input$x1_cell_edit, {
                info = input$x1_cell_edit
                str(info)
                i = info$row
                j = info$col
                v = info$value
                x[i, j] <<- DT::coerceValue(v, x[i, j])
                replaceData(proxy, x, resetPaging = FALSE)  # important
            })
            
            # Downloadable table (df) as csv
            output$downloadData = downloadHandler(
                filename = function() {
                    paste(input$dataset, ".csv", sep = "")
                },
                content = function(file) {
                    write.csv(proxy, file, row.names = FALSE)
                }
            )
        }
   


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

这不是一个完整的答案! 我知道错误发生在哪里,我可以修复它。但是我无法让下载处理程序下载数据表(而是出现一个空的 csv 文件)。

这可能是由于:

  1. input$dataset 未在您的代码中定义。这是处理此问题的类似 post:

Shiny App Upload and Download Data Dynamically Not Working

  1. 我认为更重要的是 Stephane Laurent 在他的回答中所说:

replaceData 在第二个参数中需要一个 数据框 不是数据表 .

这就是您收到错误的原因。

当您有一个数据表 proxy 时,数据帧位于 proxy$x$data.

使用此代码可消除错误,但会下载一个空白的 .csv 文件

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

替换

write.csv(proxy, file, row.names = FALSE)

write.csv(x, file, row.names = FALSE)