我试图允许用户编辑 DT 值,并根据这些编辑对其他列运行计算。这在第一页上效果很好,但在后续页面上编辑值会将分页重置回第一页 - 从用户体验角度来看这是一场噩梦。
演示问题的示例应用程序。
尝试编辑第 2 页上的
vs
值,看看会发生什么...
library(shiny)
library(DT)
library(dplyr)
shinyApp(
ui = fluidPage(
DTOutput('table')
),
server = function(input, output, session) {
x = reactiveValues(df = mtcars %>% select(vs,am))
output$table = renderDT(x$df, editable = TRUE)
proxy = dataTableProxy('table')
observeEvent(input$table_cell_edit, {
info = input$table_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
x$df[i,which(colnames(x$df)=='am')] <- x$df[[i, j]]*2
replaceData(proxy, x$df, resetPaging = FALSE) # important I have tried with and without this line no impact on page resetting
})
}
)
基于这篇文章,我尝试删除反应性,但它不接受多次编辑。尝试对第一页上的
vs
进行两次编辑,看看会发生什么...
library(shiny)
library(DT)
library(dplyr)
shinyApp(
ui = fluidPage(
DTOutput('table')
),
server = function(input, output, session) {
df <- mtcars %>% select(vs,am)
output$table = renderDT(df, editable = TRUE)
proxy = dataTableProxy('table')
observeEvent(input$table_cell_edit, {
info = input$table_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
df[i, j] <- isolate(DT::coerceValue(v, df[i, j]))
df[i,which(colnames(df)=='am')] <- df[[i, j]]*2
replaceData(proxy, df, resetPaging = FALSE)
})
}
)
已解决...
感谢这篇文章... R Shiny - 多页可编辑数据表在编辑后跳转到第 1 行
解决方案是像这样隔离 renderDT() 函数中的反应式 df...
library(shiny)
library(DT)
library(dplyr)
shinyApp(
ui = fluidPage(
DTOutput('table')
),
server = function(input, output, session) {
x = reactiveValues(df = mtcars %>% select(vs,am))
output$table = renderDT(isolate(x$df), editable = TRUE)
proxy = dataTableProxy('table')
observeEvent(input$table_cell_edit, {
info = input$table_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
x$df[i,which(colnames(x$df)=='am')] <- x$df[[i, j]]*2
replaceData(proxy, x$df, resetPaging = FALSE) # important I have tried with and without this line no impact on page resetting
})
}
)