使用rshiny中的datatableproxy删除选定的行?

时间:2019-08-06 10:53:53

标签: r shiny

我有一个问题,我想从datatable中删除选定的行。 我将代码用作共享的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')
    )
  )
)

我能够添加行并了解其工作方式。但是我也想添加删除按钮来删除选定的行。任何帮助都会很棒

0 个答案:

没有答案