Corned Beef Hash Map
Corned Beef Hash Map

Reputation: 221

Shiny DataTable row selection responding to user input

I'm having problems selecting a row in a DataTable via user input. I'm using dev versions of Shiny and DT because row selection isn't working in non-dev versions. Specifically, I'm using Shiny ‘0.13.2.9004’ and DT ‘0.1.56’. Consider this app:

library(DT)
library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textOutput('row'),
      numericInput('selectRow', 'selectRow', 3)
    ),
    mainPanel(
      DT::dataTableOutput('testTable')
    )
  )
)

server <- function(input, output, session) {
  output$testTable <- DT::renderDataTable(iris,
                                          selection = list(mode = 'single',
                                                           target = 'row',
                                                           selected = as.character(input$selectRow)),
                                          server = TRUE)
}

shinyApp(ui = ui, server = server)

When it runs, the third row of testTable is selected because that's the default value of selectRow. But changing the value of selectRow does nothing to the row selection in testTable. Bug? Or am I doing something wrong?

Upvotes: 1

Views: 623

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12097

The selected option only works for pre-selection. To update selection after table is created, you need to use dataTableProxy and selectRows. Add the following to your server code.

proxy = dataTableProxy("testTable")

observeEvent(input$selectRow, {
    selectRows(proxy, as.numeric(input$selectRow))
})

Upvotes: 2

Related Questions