title: Edit datatable in shiny with dropdown selection (for DT v0.19)
body: <p>I based the code below on Stephane Laurent's solution to the following question on Stack Overflow:</p>
  <p><a href="https://stackoverflow.com/questions/52593539/edit-datatable-in-shiny-with-dropdown-selection-for-factor-variables">Edit datatable in Shiny with dropdown selection for factor variables</a></p>
  <p>I added in code to use editData to update the table and to be able to save/export the updates.</p>
  <p>The following works with DT v0.18 but with DT v0.19 I found the id_cell_edit seems to not be triggering.  I am unsure if it has to do with the callback or possibly jquery.contextMenu given DT v0.19 upgraded to jquery 3.0. Would appreciate any insight people may have on how to work through this.</p>
  <p>Here is a description of the behavior I observe when using v0.18. When I select the usage column and update the value for the first row from the default “sel” to “id” the value changes in the DT table.  I also see it updates the view of the tibble and thus the data in the download csv file is also updated.  If I progress to the Next page to see the 11th item, and then return back to the first page, I can see the record I updated still says “id”.</p>
  <p>Here is a description of the behavior I observe when using v0.19. When I select the usage column and update the value for the first row from the default “sel” to “id” the value changes in the DT table.  It does not update the view of the tibble and thus the data in the download csv file does not get updated.  If I progress to the Next page to see the 11th item, and then return back to the first page, the update I had made gets cleared.</p>
  <p>Note that I also ran reactive graphs using reactlog. I followed the same steps to update the usage column of the first row to &quot;id&quot;. The first difference I note is that reactiveValues###$dt at Step 5 gives me a list of 7 when I use version v0.18 and a list of 8 when I use version v0.19. At Step 16, for v0.18 input$dt_cell_edit invalidates, then Data invalidates and output$table invalidates. At Step 16 when using v0.19, however, output$dt invalidates then output$table invalidates. In other words, when using v0.19 input$dt_cell_edit and Data are not invalidating.</p>
  <pre><code>library(shiny)
  library(DT)
  library(dplyr)
  
  cars_df &lt;- mtcars
  cars_meta &lt;- dplyr::tibble(variables = names(cars_df), data_class = sapply(cars_df, class), usage = &quot;sel&quot;)
  cars_meta$data_class &lt;- factor(cars_meta$data_class,  c(&quot;numeric&quot;, &quot;character&quot;, &quot;factor&quot;, &quot;logical&quot;))
  cars_meta$usage &lt;- factor(cars_meta$usage,  c(&quot;id&quot;, &quot;meta&quot;, &quot;demo&quot;, &quot;sel&quot;, &quot;text&quot;))
  
  
  callback &lt;- c(
      &quot;var id = $(table.table().node()).closest('.datatables').attr('id');&quot;,
      &quot;$.contextMenu({&quot;,
      &quot;  selector: '#' + id + ' td.factor input[type=text]',&quot;,
      &quot;  trigger: 'hover',&quot;,
      &quot;  build: function($trigger, e){&quot;,
      &quot;    var levels = $trigger.parent().data('levels');&quot;,
      &quot;    if(levels === undefined){&quot;,
      &quot;      var colindex = table.cell($trigger.parent()[0]).index().column;&quot;,
      &quot;      levels = table.column(colindex).data().unique();&quot;,
      &quot;    }&quot;,
      &quot;    var options = levels.reduce(function(result, item, index, array){&quot;,
      &quot;      result[index] = item;&quot;,
      &quot;      return result;&quot;,
      &quot;    }, {});&quot;,
      &quot;    return {&quot;,
      &quot;      autoHide: true,&quot;,
      &quot;      items: {&quot;,
      &quot;        dropdown: {&quot;,
      &quot;          name: 'Edit',&quot;,
      &quot;          type: 'select',&quot;,
      &quot;          options: options,&quot;,
      &quot;          selected: 0&quot;,
      &quot;        }&quot;,
      &quot;      },&quot;,
      &quot;      events: {&quot;,
      &quot;        show: function(opts){&quot;,
      &quot;          opts.$trigger.off('blur');&quot;,
      &quot;        },&quot;,
      &quot;        hide: function(opts){&quot;,
      &quot;          var $this = this;&quot;,
      &quot;          var data = $.contextMenu.getInputValues(opts, $this.data());&quot;,
      &quot;          var $input = opts.$trigger;&quot;,
      &quot;          $input.val(options[data.dropdown]);&quot;,
      &quot;          $input.trigger('change');&quot;,
      &quot;        }&quot;,
      &quot;      }&quot;,
      &quot;    };&quot;,
      &quot;  }&quot;,
      &quot;});&quot;
  )
  
  createdCell &lt;- function(levels){
      if(missing(levels)){
          return(&quot;function(td, cellData, rowData, rowIndex, colIndex){}&quot;)
      }
      quotedLevels &lt;- toString(sprintf(&quot;\&quot;%s\&quot;&quot;, levels))
      c(
          &quot;function(td, cellData, rowData, rowIndex, colIndex){&quot;,
          sprintf(&quot;  $(td).attr('data-levels', '[%s]');&quot;, quotedLevels),
          &quot;}&quot;
      )
  }
  
  ui &lt;- fluidPage(
      tags$head(
          tags$link(
              rel = &quot;stylesheet&quot;,
              href = &quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.8.0/jquery.contextMenu.min.css&quot;
          ),
          tags$script(
              src = &quot;https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.8.0/jquery.contextMenu.min.js&quot;
          )
      ),
      DTOutput(&quot;dt&quot;),
      br(),
      verbatimTextOutput(&quot;table&quot;),
      br(),
      downloadButton('download',&quot;Download the data&quot;)
      
  )
  
  server &lt;- function(input, output){
      
      dat &lt;- cars_meta
      
      value &lt;- reactiveValues()
      value$dt&lt;-
          datatable(
              dat, editable = &quot;cell&quot;, callback = JS(callback),
              options = list(
                  columnDefs = list(
                      list(
                          targets = 2,
                          className = &quot;factor&quot;,
                          createdCell = JS(createdCell(c(levels(cars_meta$data_class), &quot;another level&quot;)))
                      ),
                      list(
                          targets = 3,
                          className = &quot;factor&quot;,
                          createdCell = JS(createdCell(c(levels(cars_meta$usage), &quot;another level&quot;)))
                      )
                  )
              )
          )
      
      output[[&quot;dt&quot;]] &lt;- renderDT({
          value$dt
          
      }, 
      server = TRUE)
      
      Data &lt;- reactive({
          info &lt;- input[[&quot;dt_cell_edit&quot;]]
          if(!is.null(info)){
              info &lt;- unique(info)
              info$value[info$value==&quot;&quot;] &lt;- NA
              dat &lt;-  editData(dat, info, proxy = &quot;dt&quot;)
          }
          dat
      })
      
      
      #output table to be able to confirm the table updates
      output[[&quot;table&quot;]] &lt;- renderPrint({Data()})  
      
      output$download &lt;- downloadHandler(
          filename = function(){&quot;Data.csv&quot;}, 
          content = function(fname){
              write.csv(Data(), fname)
          }
      )
  }
  
  shinyApp(ui, server)
  </code></pre>
  <p>Below I've leveraged <a href="https://stackoverflow.com/questions/69344974/dt-dynamically-change-column-values-based-on-selectinput-from-another-column-in/69389649#69389649">ismirsehregal's solution</a> into my use case. I also added in the renderPrint/verbatimTextOutput to illustrate what I am trying to do with the underlying data. I'd like to be able to capture the values not the input containers. Essentially with the code I am trying to give the User a dataset, allow them to change some values but restrict the choices with dropdowns, and then use the updated dataset for further processing. At this point in the solution I don't know how to get to the updated dataset so that I can use it, for example, to export to a csv file.</p>
  <pre><code>library(DT)
  library(shiny)
  library(dplyr)
  
  
  cars_df &lt;- mtcars
  selectInputIDa &lt;- paste0(&quot;sela&quot;, 1:length(cars_df))
  selectInputIDb &lt;- paste0(&quot;selb&quot;, 1:length(cars_df))
  
  initMeta &lt;- dplyr::tibble(
      variables = names(cars_df), 
      data_class = sapply(selectInputIDa, function(x){as.character(selectInput(inputId = x, label = &quot;&quot;, choices = c(&quot;character&quot;,&quot;numeric&quot;, &quot;factor&quot;, &quot;logical&quot;), selected = sapply(cars_df, class)))}),
      usage = sapply(selectInputIDb, function(x){as.character(selectInput(inputId = x, label = &quot;&quot;, choices = c(&quot;id&quot;, &quot;meta&quot;, &quot;demo&quot;, &quot;sel&quot;, &quot;text&quot;), selected = &quot;sel&quot;))})
  )
  
  
  
  ui &lt;- fluidPage(
      DT::dataTableOutput(outputId = 'my_table'),
      br(),
      verbatimTextOutput(&quot;table&quot;)
  )
  
  
  server &lt;- function(input, output, session) {
      
      
      displayTbl &lt;- reactive({
          dplyr::tibble(
              variables = names(cars_df), 
              data_class = sapply(selectInputIDa, function(x){as.character(selectInput(inputId = x, label = &quot;&quot;, choices = c(&quot;numeric&quot;, &quot;character&quot;, &quot;factor&quot;, &quot;logical&quot;), selected = input[[x]]))}),
              usage = sapply(selectInputIDb, function(x){as.character(selectInput(inputId = x, label = &quot;&quot;, choices = c(&quot;id&quot;, &quot;meta&quot;, &quot;demo&quot;, &quot;sel&quot;, &quot;text&quot;), selected = input[[x]]))})
          )
      })
      
      
  
      
      output$my_table = DT::renderDataTable({
          DT::datatable(
              initMeta, escape = FALSE, selection = 'none', rownames = FALSE,
              options = list(paging = FALSE, ordering = FALSE, scrollx = TRUE, dom = &quot;t&quot;,
                             preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                             drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
              )
          )
      }, server = TRUE)
      
      my_table_proxy &lt;- dataTableProxy(outputId = &quot;my_table&quot;, session = session)
      
      observeEvent({sapply(selectInputIDa, function(x){input[[x]]})}, {
          replaceData(proxy = my_table_proxy, data = displayTbl(), rownames = FALSE) # must repeat rownames = FALSE see ?replaceData and ?dataTableAjax
      }, ignoreInit = TRUE)
      
      observeEvent({sapply(selectInputIDb, function(x){input[[x]]})}, {
          replaceData(proxy = my_table_proxy, data = displayTbl(), rownames = FALSE) # must repeat rownames = FALSE see ?replaceData and ?dataTableAjax
      }, ignoreInit = TRUE)
      
      
      
      output$table &lt;- renderPrint({displayTbl()})  
      
      
  }
  
  shinyApp(ui = ui, server = server)
  
  </code></pre>