Home > database >  dataTableProxy to data.frame error in R Shiny
dataTableProxy to data.frame error in R Shiny

Time:01-16

I have created a demo app that allows the user to edit an existing df and then download the updated table as a .csv. The app runs almost fine, as when I click the download button, I get the following error:

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


[No stack trace available]

How can this be fixed?

Code

# 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)

CodePudding user response:

Replace

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

with

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

CodePudding user response:

This is not a full answer! I know where the error occurs and I can fix it. But I can not make the downloadhandler to download the datatable (instead an empty csv file is occuring).

This might be due to:

  1. input$dataset is not defined in your code. Here is a similar post handling this issue:

Shiny App Upload and Download Data Dynamically Not Working

  1. And I think more important as stated by Stephane Laurent in his answer:

Replace data in R formattable datatable using replaceData function

replaceData requires a dataframe in the second argument, not a datatable.

This is the reason you get the error.

When you have a datatable proxy, the dataframe is in proxy$x$data.

Using this code removes the error, but a blank .csv file is downloaded

  # 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)
    }
  )
}

  •  Tags:  
  • Related