How to use renderDataTable in Shinny R's file upload example

2k Views Asked by At

I am trying to create my first Shiny R application, which does two things:

  1. Display user uploaded CSV file
  2. Count user selected rows

To achieve these features, I need to combine two examples file upload and row selection, which involves switching renderTable to renderDataTable. However, I am only able to display the data using renderDataTable, but not be able to pass arguments like callback and options into it.

Here is the server.R code

shinyServer(function(input, output) {
  output$contents <- renderDataTable({
    inFile <- input$file1
    if (is.null(inFile))
        return(NULL)
    subset(read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote), select=input$show_vars)
  })
})

If I change the above function this case (below), I got the following errors server.R:9:5: unexpected 'if'. I think this is related to building a reactive file upload application, which I could not find good examples... Can anyone give me some suggestions? Thanks!

shinyServer(function(input, output) {
  output$contents <- renderDataTable(
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
   subset(read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote), select=input$show_vars),
   options = list(pageLength = 10),
   callback = "function(table) {
         table.on('click.dt', 'tr', function() {
           $(this).toggleClass('selected');
           Shiny.onInputChange('rows',
                               table.rows('.selected').indexes().toArray());
         });
       }"
  )
})
1

There are 1 best solutions below

3
jdharrison On BEST ANSWER

The first argument to renderDataTable should be an expression. You need to wrap your code in curly braces to pass this first section of code as an expression:

shinyServer(function(input, output) {
  output$contents <- renderDataTable({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
    subset(read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote), select=input$show_vars)
  }
  , options = list(pageLength = 10),
  , callback = "function(table) {
         table.on('click.dt', 'tr', function() {
           $(this).toggleClass('selected');
           Shiny.onInputChange('rows',
                               table.rows('.selected').indexes().toArray());
         });
       }"
  )
})