更改Shiny DT数据表中的数据集时保留选定行

3

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

我想做的是,在切换数据集时,在df2中预选来自df1的可用行。目前,我的选择总是被删除。当我尝试保存选定的行(取消两行的注释)时,表格会直接重置。

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)

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

2个回答

4

当您要更改df时,只能保存所选行,如下所示:

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而不是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')
}

看起来很不错!感谢您提供的两个建议! - drmariod

1

嗯,看起来我找到了一个解决方案,但我想知道是否有更好的解决方案。

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

有时会出现一个processing消息,并且在每次点击表格时都会短暂地“闪烁”…希望能得到更好的答案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接