如何使用datatable函数替换R呈现的DT中的数据 [英] How to replaceData in DT rendered in R shiny using the datatable function

查看:147
本文介绍了如何使用datatable函数替换R呈现的DT中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an R shiny app with a DT datatable that is rendered using the datatable function in order to set various options. 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. The reprex below shows what I would like to do, but replaceData doesn't work in this pattern. 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)


推荐答案

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

Update: Very strangely, removing rownames = FALSE made it all possible. 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)

这篇关于如何使用datatable函数替换R呈现的DT中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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