【发布时间】:2019-11-14 16:58:55
【问题描述】:
我有一个带有DT datatable 的R shiny 应用程序,它使用datatable 函数呈现以设置各种选项。我想使用dataTableProxy 和replaceData 来更新表中的数据,但我能找到的所有示例都假设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)。