在闪亮的DT数据表中更改数据集时保留选定的行 [英] keep selected rows when changing dataset in shiny DT datatable

查看:55
本文介绍了在闪亮的DT数据表中更改数据集时保留选定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DT 包在我的 shiny 应用中显示数据表。由于我提供了不同的数据集,因此我具有单选按钮来选择它们,并且数据表会自动更新。

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.

我想做的是从 df2中的 df1 中预先选择可用的行。 切换数据集时。此刻,我的选择总是被删除。当我尝试保存选定的行(取消注释两行)时,我的表将直接重置。

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 <- 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)

我使用了 DT 表,因为我需要代理以及与数据表的一些交互。

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

推荐答案

只有在要更改df之类的情况下,才能保存选定的行

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)

或对@drmariod变量进行一些修改:使用 eventReactive 而不是 active

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')
}

这篇关于在闪亮的DT数据表中更改数据集时保留选定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆