r - 如何使用数据表函数替换 R 中呈现的 DT 中的数据
问题描述
我有一个R
shiny
应用程序,DT
datatable
它使用该datatable
函数呈现以设置各种选项。我想使用dataTableProxy
和replaceData
更新表中的数据,但我能找到的所有示例都假设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)
推荐阅读
- r - R:setwd 中的错误(“C:/Users/diegoesparza/GitHub/covid_equity”):无法更改工作目录。为什么我不能更改我的工作目录?
- python - 打印和输出相同的变量会得到不同的结果 - Leetcode, "Next Permutation"
- reactjs - Symfony 和 React - 语法错误:意外的令牌,预期的“;”
- c++ - boost::hash 用于包含 boost 单元的元组
- twilio - 如果用户不接听外拨电话,则发送带有一些预录消息的语音邮件
- kubernetes - Kubernetes 在哪里挂载服务帐户机密?
- android - Android Activity/Fragment 在方向更改时重复(Kotlin)
- sql-server - 为什么我在 SSMS 中不断收到不正确的 const UDF 用法?
- c++ - 我如何使用 QDialogbox 上的确定和取消按钮
- webhooks - Podio Webhook API:什么是 Ref 类型?