简体   繁体   English

如何使用数据表函数替换在 R 中呈现的 DT 中的数据

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

I have an R shiny app with a DT datatable that is rendered using the datatable function in order to set various options.我有一个R shiny应用与DT datatable正在使用该渲染datatable功能,以便设置各种选项。 I would like to use dataTableProxy and replaceData to update the data in the table, but all the examples I can find assume the DT is rendered directly from the data object, not using the datatable function.我想使用dataTableProxyreplaceData来更新表中的数据,但我能找到的所有示例都假设DT是直接从数据对象呈现的,而不是使用datatable函数。 The reprex below shows what I would like to do, but replaceData doesn't work in this pattern.下面的 reprex 显示了我想要做什么,但replaceData在这种模式下不起作用。 How do I do this?我该怎么做呢? Thanks.谢谢。


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

Update: Very strangely, removing rownames = FALSE made it all possible.更新:很奇怪,删除rownames = FALSE使这一切成为可能。 I'm not exactly sure why, but probably rownames might be essential for replacing Data.我不确定为什么,但行名可能对替换数据至关重要。

# 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