簡體   English   中英

如何使用數據表函數替換在 R 中呈現的 DT 中的數據

[英]How to replaceData in DT rendered in R shiny using the datatable function

我有一個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)

更新:很奇怪,刪除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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM