1

I am trying to load a csv file to a shiny program, edit it in place and download the edited file. I found that the package DT provides some functionalities for the same. I modified the example here to read csv from the file and example here to write the output. I was also trying to edit the loaded CSV in place using the DT's experimental features here. My code below loads csv, and saves the CSV but while trying to edit it in place it crashes with the error:

Warning: Error in <<-: invalid (NULL) left side of assignment
Stack trace (innermost first):
65: observeEventHandler [/home/user/ShinyApp/EditingValues/server.R#40]

Basically, my problem I believe I am not able to provide values to x from the read csv in the example here.

My ui.R is

fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  'text/tab-separated-values',
                  'text/plain',
                  '.csv',
                  '.tsv'
                )
      ),
      tags$hr(),
      checkboxInput('header', 'Header', TRUE),
      radioButtons('sep', 'Separator',
                   c(Comma=',',
                     Semicolon=';',
                     Tab='\t'),
                   ','),
      radioButtons('quote', 'Quote',
                   c(None='',
                     'Double Quote'='"',
                     'Single Quote'="'"),
                   '"'),
      tags$hr(),
      p('Once you are done download the  data'),
      downloadButton('x3', 'Download Data')
    ),
    mainPanel(
        DT::dataTableOutput('x1')
    )
  )
)

My server.R is

# By default, the file size limit is 5MB. It can be changed by
# setting this option. Here we'll raise limit to 9MB.

# Install DT, experimental version may not work well 
# devtools::install_github('rstudio/DT@feature/editor')

library(shiny) # The main program
library(DT) # Edit the output
library(data.table) # Faster read and write

options(shiny.maxRequestSize = 9*1024^2)

function(input, output, session) {
   dataframe<-reactive({
        if (is.null(input$file1))
            return(NULL)  
       dta_types = fread(input$file1$datapath, header = input$header,
                         sep = input$sep, quote = input$quote, nrows = 0,
                         strip.white = TRUE, data.table = FALSE)
        data<-fread(input$file1$datapath, header = input$header,
                    sep = input$sep, quote = input$quote,
                    stringsAsFactors = FALSE, colClasses= rep("character", ncol(dta_types)))
        data
    })

  output$x3 = downloadHandler('mtcars-filtered.csv', content = function(file) {
      s = input$x1_rows_all
      write.csv(dataframe()[s, , drop = FALSE], file)
  })
  output$x1 = DT::renderDataTable(dataframe(), selection = 'none')

  proxy = dataTableProxy('x1')

  observeEvent(input$x1_cell_edit, {
      info = input$x1_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value
      dataframe()[i, j] <<- DT:::coerceValue(v, dataframe()[i, j])
      replaceData(proxy, dataframe(), resetPaging = FALSE)
  })
}

How can this be achieved? I am sure I missing simple assignment somewhere. I tried

x=dataframe() 

before

output$x1 = DT::renderDataTable(dataframe(), selection = 'none')

and using x instead of dataframe() everywhere but that crashes the app before it is loaded. Thanks for the inputs. A similar question using rhandsontable also does not have an answer yet.

Community
  • 1
  • 1
discipulus
  • 2,665
  • 3
  • 34
  • 51
  • 2
    Using `rhandsontable` is quite easy. You just need to use `hot_to_r` function to convert the user-edited table back to R object and then save it. – Xiongbing Jin Nov 02 '16 at 14:14

0 Answers0