keep selected rows when changing dataset in shiny DT datatable

前提是你 提交于 2019-12-14 03:53:05

问题


I am using the DT package to display a data table in my shiny app. Since I provide different data sets, I have radio buttons to select them and the data table updates automatically.

What I would like to do is to preselect the available rows from df1 in df2 when switching the datasets. At the moment, my selection always get erased. When I try to save the selected rows (uncomment the two rows), my table get reset directly.

library(shiny)
library(DT)

df1 <- data.frame(names=letters,
                  values=1:26)
df2 <- data.frame(names=letters,
                  values=(1:26)*2)[seq(1,26,2),]

ui <- shinyUI(
  fluidPage(
    sidebarLayout(
      sidebarPanel(
        radioButtons("dataset", label=h5("Select dataset"),
                     choices=list("df1"='df1',
                                  "df2"='df2'),
                     selected='df1', inline=TRUE)
      ),
      mainPanel(
        DT::dataTableOutput("name_table")
      )
    )
  )
)

Server side...

server <- function(input, output, session) {
  getDataset <- reactive({
    result <- list()
    result[['dataset']] <- switch(input$dataset,
                     'df1'=df1,
                     'df2'=df2)
    # result[['selection']] <- 
    #   as.numeric(input$name_table_rows_selected)
    return(result)
  })
  output$name_table <- DT::renderDataTable({
    DT::datatable(getDataset()[['dataset']],
                  options=list(pageLength=5))

  })
  name_proxy = DT::dataTableProxy('name_table')
}

shinyApp(ui, server)

I used the DT table, since I need the proxy and some interaction with the data table.


回答1:


You can save selected rows only when going to change df like

server <- function(input, output, session) {
  dd=reactiveValues(select=NULL)

  observeEvent(input$dataset,{
    dd$select=as.numeric(isolate(input$name_table_rows_selected))
   })

  getDataset <- reactive({
    result <- list()
    result[['dataset']] <- switch(input$dataset,
                                  'df1'=df1,
                                  'df2'=df2)

    return(result)
  })
  output$name_table <- DT::renderDataTable({
    DT::datatable(getDataset()[['dataset']],
                  options=list(pageLength=5),
                  selection = list(mode = 'multiple', selected =dd$select  )
    )

  })
  name_proxy = DT::dataTableProxy('name_table')
}

shinyApp(ui, server)

Or a bit modification of @drmariod variant: use eventReactive instead of reactive

server <- function(input, output, session) {
  getDataset <- eventReactive(input$dataset,{
    result <- list()
    result[['dataset']] <- switch(input$dataset,
                                  'df1'=df1,
                                  'df2'=df2)
    result[['selection']] <- testing()
    return(result)
  })
  testing <- function() {
    list(selected=as.numeric(input$name_table_rows_selected))
  } 
  output$name_table <- DT::renderDataTable({
    DT::datatable(getDataset()[['dataset']],
                  options=list(pageLength=5),
                  selection=getDataset()[['selection']])

  })
  name_proxy = DT::dataTableProxy('name_table')
}



回答2:


Hm, it looks like I found a solution, but I wonder if there is a better solution.

server <- function(input, output, session) {
  getDataset <- reactive({
    result <- list()
    result[['dataset']] <- switch(input$dataset,
                     'df1'=df1,
                     'df2'=df2)
    result[['selection']] <- testing()
    return(result)
  })
  testing <- function() {
    list(selected=as.numeric(input$name_table_rows_selected))
  } 
  output$name_table <- DT::renderDataTable({
    DT::datatable(getDataset()[['dataset']],
                  options=list(pageLength=5),
                  selection=getDataset()[['selection']])

  })
  name_proxy = DT::dataTableProxy('name_table')
}

I wonder, sometimes comes a processing message. and on each click the table shortly "blinks"... Would be great to get a better answer.



来源:https://stackoverflow.com/questions/40736201/keep-selected-rows-when-changing-dataset-in-shiny-dt-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!