【问题标题】:How to replaceData in DT rendered in R shiny using the datatable function如何使用数据表函数替换 R 中呈现的 DT 中的数据
【发布时间】:2019-11-14 16:58:55
【问题描述】:

我有一个带有DT datatableR shiny 应用程序,它使用datatable 函数呈现以设置各种选项。我想使用dataTableProxyreplaceData 来更新表中的数据,但我能找到的所有示例都假设DT 是直接从数据对象呈现的,而不是使用datatable 函数。下面的代表显示了我想做的事情,但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)

【问题讨论】:

  • 您是否尝试选择多个?它适用于这种情况
  • 谢谢,问题出在数据刷新上。我已经更新了reprex。
  • 我会尝试使用replaceData 选项rownames = FALSE,即DT::replaceData(proxy2, my$data, rownames = FALSE)

标签: r shiny dt


【解决方案1】:

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

【讨论】:

  • 谢谢,我已经更新了 reprex 以更清楚地显示我的问题。
  • @SimonWoodward 所以你想让不工作的部分工作吗?
  • 是的。谢谢。问题是我的表格有额外的格式,但这似乎阻止了 replaceData 工作。
  • 太棒了,干得好!虽然我真的不想要行名......除非我可以自定义它们......
  • @SimonWoodward 请注意,您也可以将rownames=FALSE 传递给replaceData
猜你喜欢
  • 2018-07-02
  • 2019-10-23
  • 2018-05-28
  • 2017-02-19
  • 1970-01-01
  • 2020-02-04
  • 2019-05-13
  • 2021-11-25
  • 2018-03-24
相关资源
最近更新 更多