首页 > 解决方案 > 如何使用数据表函数替换 R 中呈现的 DT 中的数据

问题描述

我有一个R shiny应用程序,DT datatable它使用该datatable函数呈现以设置各种选项。我想使用dataTableProxyreplaceData更新表中的数据,但我能找到的所有示例都假设DT是直接从数据对象呈现的,而不是使用datatable函数。下面的 reprex 显示了我想做的事情,但replaceData在这种模式下不起作用。我该怎么做呢?谢谢。


# based on 
# https://community.rstudio.com/t/reorder-data-table-with-seleceted-rows-first/4254

library(shiny)
library(DT)

ui = fluidPage(
    actionButton("button1", "Randomize"),
    fluidRow(
        column(6,
               h4("Works"),
               DT::dataTableOutput('table1', width="90%")),
        column(6,
               h4("Doesn't Work"),
               DT::dataTableOutput('table2', width="90%"))
    )
)

server = function(input, output, session) {

        my <- reactiveValues(data = iris)

        output$table1 <- DT::renderDataTable(isolate(my$data))

        output$table2 <- DT::renderDataTable({
            DT::datatable(isolate(my$data),
                          options = list(lengthChange=FALSE, ordering=FALSE, searching=FALSE,
                                       columnDefs=list(list(className='dt-center', targets="_all")),
                                       stateSave=TRUE, info=FALSE),
                          class = "nowrap cell-border hover stripe",
                          rownames = FALSE,
                          editable = FALSE
            ) %>%
                DT::formatStyle('Sepal.Width', `text-align`="center")
        })

        observeEvent(input$button1, {

            # calculate new row order
            row_order <- sample(1:nrow(my$data))
            my$data <- my$data[row_order, ]

            proxy1 <- DT::dataTableProxy('table1')
            DT::replaceData(proxy1, my$data)
            proxy2 <- DT::dataTableProxy('table2')
            DT::replaceData(proxy2, my$data)

        })

}

shinyApp(ui, server)

标签: rshinydt

解决方案


更新:很奇怪,删除rownames = FALSE使这一切成为可能。我不完全确定为什么,但可能行名对于替换数据可能是必不可少的。

# based on 
# https://community.rstudio.com/t/reorder-data-table-with-seleceted-rows-first/4254

library(shiny)
library(DT)

ui = fluidPage(
  actionButton("button1", "Randomize"),
  fluidRow(
    column(6,
           h4("Works"),
           DT::dataTableOutput('table1', width="90%")),
    column(6,
           h4("Doesn't Work"),
           DT::dataTableOutput('table2', width="90%"))
  )
)

server = function(input, output, session) {

  my <- reactiveValues(data = iris)

  output$table1 <- DT::renderDataTable(isolate(my$data))

  output$table2 <- DT::renderDataTable({
    DT::datatable(isolate(my$data),
                  options = list(lengthChange=FALSE, ordering=FALSE, searching=FALSE,
                                 columnDefs=list(list(className='dt-center', targets="_all")),
                                 stateSave=TRUE, info=FALSE),
                  class = "nowrap cell-border hover stripe",
                 # rownames = FALSE,
                  editable = FALSE
    ) %>%
      DT::formatStyle('Sepal.Width', `text-align`="center")
  })

  observeEvent(input$button1, {

    # calculate new row order
    row_order <- sample(1:nrow(my$data))
    my$data <- my$data[row_order, ]

    proxy1 <- DT::dataTableProxy('table1')
    DT::replaceData(proxy1, my$data)
    proxy2 <- DT::dataTableProxy('table2')
    DT::replaceData(proxy2, my$data)

  })

}

shinyApp(ui, server)

推荐阅读