【问题标题】:R Shiny Datatable - Editing values, impacting other columns, and retaining pagingR Shiny Datatable - 编辑值、影响其他列并保留分页
【发布时间】:2020-09-23 16:49:22
【问题描述】:

我正在尝试允许用户编辑 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
        })
    }
)

基于this post,我尝试删除反应性,但它​​不接受多次编辑。尝试在第一页对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 datatable


    【解决方案1】:

    已解决...

    感谢这篇文章... R Shiny - multi-page editable DataTable jumps to row #1 after an edit

    解决方案是像这样隔离 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
        })
      }
    )
    

    【讨论】:

    • 由于某种原因,当我使用replaceData 时,表会变空,但如果我使用reloadData,它就可以工作。我查看了replaceData,文档说它调用了reloadData函数。
    猜你喜欢
    • 2016-12-29
    • 2022-10-13
    • 2018-03-24
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2013-09-01
    • 2018-06-18
    相关资源
    最近更新 更多