Skip to content

Instantly share code, notes, and snippets.

@dkulp2
Last active October 16, 2017 18:23
Show Gist options
  • Save dkulp2/7ebb1c936d08f3434127e58d7798af28 to your computer and use it in GitHub Desktop.
Save dkulp2/7ebb1c936d08f3434127e58d7798af28 to your computer and use it in GitHub Desktop.
Sticky rows in DT table
library(shiny)
ui <- fluidPage(
checkboxInput('yellow.only', 'Yellow Only'),
uiOutput('fruit.selection'),
DT::dataTableOutput("dt.fruit.selection")
)
server <- function(input, output) {
fruit.options <- reactive({
all.fruits <- c(grape='Grape', banana='Banana', papaya='Papaya', raspberry='Raspberry')
yellow.fruits <- c(FALSE, TRUE, TRUE, FALSE)
all.fruits[yellow.fruits | !input$yellow.only]
})
fruit.options.df <- reactive({
data.frame(fruits=fruit.options(), some.other.col=nchar(fruit.options()))
})
output$fruit.selection <- renderUI({ selectInput('fruit', 'Fruit', choices=fruit.options(), selected=input$fruit, multiple=TRUE, selectize=FALSE, size=length(fruit.options())) })
output$dt.fruit.selection <- DT::renderDataTable({
if (!exists('fruit.options.cache') || identical(fruit.options.cache, fruit.options.df())) {
rows.selected <- isolate(input$dt.fruit.selection_rows_selected)
} else {
rows.selected <- which(fruit.options.df()$fruit %in% fruit.options.cache$fruits[isolate(input$dt.fruit.selection_rows_selected)])
}
fruit.options.cache <<- fruit.options.df()
DT::datatable(fruit.options.cache, rownames=FALSE, selection=list(mode="multiple", selected=rows.selected))
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment