0

I have a problem where i want to delete selected rows from datatable . I am using code as shared here

server.R

library(shiny)
library(DT)

shinyServer(function(input, output, session) {

  # using server = FALSE mainly for addRow(); server = TRUE works for
  # selectRows() and selectColumns()
  output$foo = DT::renderDataTable(
    iris, server = FALSE, selection = list(target = 'row+column'),
    caption = 'Using a proxy object to manipulate the table'
  )

  proxy = dataTableProxy('foo')

  observeEvent(input$select1, {
    proxy %>% selectRows(as.numeric(input$rows))
  })

  observeEvent(input$select2, {
    proxy %>% selectColumns(input$col)
  })

  observeEvent(input$clear1, {
    proxy %>% selectRows(NULL)
  })

  observeEvent(input$clear2, {
    proxy %>% selectColumns(NULL)
  })

  observeEvent(input$add, {
    proxy %>% addRow(iris[sample(nrow(iris), 1), , drop = FALSE])
  })

  observe({
    if (input$cap != '') proxy %>% updateCaption(input$cap)
  })

  output$info = renderPrint({
    list(rows = input$foo_rows_selected, columns = input$foo_columns_selected)
  })

})

ui.R

library(shiny)

fluidPage(

  title = 'Manipulate an Existing Table',

  sidebarLayout(
    sidebarPanel(
      selectizeInput('rows', 'Row IDs', choices = seq_len(nrow(iris)), multiple = TRUE),
      actionButton('select1', 'Select Rows'),
      actionButton('clear1', 'Clear Rows'),
      numericInput('col', 'Column ID', 1, min = 1, max = ncol(iris), step = 1),
      actionButton('select2', 'Select Column'),
      actionButton('clear2', 'Clear Columns'),
      hr(),
      actionButton('add', 'Add Row'),
      hr(),
      textInput('cap', 'Table Caption')
    ),
    mainPanel(
      DT::dataTableOutput('foo'),
      verbatimTextOutput('info')
    )
  )
)

I am able to add rows and understand how it is working. But i want to add delete button also to delete selected rows. Any help will be great

Regolith
  • 2,944
  • 9
  • 33
  • 50
Paras Ghai
  • 363
  • 1
  • 6
  • 14
  • One answer here: https://stackoverflow.com/questions/39136385/delete-row-of-dt-data-table-in-shiny-app – CClaire Aug 06 '19 at 12:43
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. – camille Aug 06 '19 at 18:28

0 Answers0