ChriiSchee
ChriiSchee

Reputation: 709

Dynamically pre-select row using DT in shiny

I try to dynamically pre-select rows in shiny using DT and saw this question/answer: Pre-select rows of a dynamic DT in shiny I do however need the 'native' appearance of DT with the intelligent column filters.

Here is my code:

library(shiny)
library(DT)

shinyApp(
    ui=shinyUI(
        fixedPage(
            radioButtons('selectedRows',
                         'select a row',
                         c(
                         "row one"="1",
                         "row two"="2")),
            DT::dataTableOutput('myTable')
        )
    )
,

server=shinyServer(function(input, output) {

    selRows <- reactiveValues(row=c())

    observe({
        validate(need(input$selectedRows, message=FALSE)) 
        selRows$row <- as.numeric(input$selectedRows)
    })

    output$myTable <- DT::renderDataTable ({
                                             mtcars[,1:5]
                                          },server=T,
                                            rownames = T,
                                            filter = "top",
                                            selection = list(mode='multiple',
                                                             selected = selRows$row))

    })
) 

Thanks!

Upvotes: 2

Views: 2254

Answers (1)

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

It is written in the other post to wrap it within datatable() then it works :)

output$myTable <- DT::renderDataTable ({
  datatable(
    mtcars[,1:5],
    rownames = T,
    filter = "top",
    selection = list(mode='multiple',
                     selected = selRows$row)  
  )
})

Upvotes: 2

Related Questions