R Shiny - 多页可编辑数据表在编辑后跳转到第 1 行

标签 r shiny dt

我正在使用 R 3.3.1、Shiny v. 1.2.0 和 v. DT 0.5 开发 Shiny 应用程序。其中一个元素是跨越多个页面的可编辑数据表。在我进行编辑后,焦点行跳转到第 1 行,这会破坏用户体验。

以下是使用以下代码段重现此问题的具体步骤:

  • 加载应用
  • 切换到数据表第2页
  • 编辑第 3 行第 2 列:将 Duh 更改为 Blue 并按 Tab
  • 观察当前行跳转到第 1 页第 1 行。如果每页有更多行,这将更容易查看。

  • 我在第 4 步中得到的不是理想的行为。我希望数据表将焦点保持在我刚刚编辑的同一行上。

    我愿意使用自定义 JS 逻辑来完成这项工作。

    看似相关的问题- DataTable doesn't remember paginated page after edit但我不知道如何在这个特定示例中从 R 桥接到 JS。
     R.version.string
    # "R version 3.3.1 (2016-06-21)"
    
    library(shiny)  # v. 1.2.0
    library(DT)  # v. 0.5
    
    page_length <- 2 # 5 elements should span 3 pages
    
    hardcoded_df <- read.table(text = "Fruit Color
                                       Apple Red
                                       Plum Purple
                                       Blueberry Duh
                                       Orange Carrot
                                       Crocodile Green",
                               header = TRUE,
                               stringsAsFactors = FALSE)
    
    ui <- fluidPage(
       DT::dataTableOutput('x1')
    )
    
    server <- function(input, output) {
      x = reactiveValues(df = hardcoded_df)
    
       output$x1 = renderDT(DT::datatable(x$df, options = list(pageLength = page_length), selection = 'none', editable = TRUE))
    
       proxy = dataTableProxy('x1')
    
       observeEvent(input$x1_cell_edit, {
         info = input$x1_cell_edit
         str(info)
    
         # str(input$x1_state)
         i = info$row
         j = info$col
         v = info$value
    
         # Without this line the table does not change but with it it jumps to row 1 after an edit.
         x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
    
         # Now we need to scroll to row i somehow ... clearly this does not work. Help!
         selectPage(proxy, ceiling(i / page_length))
         # selectRow(proxy, i)
       })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    最佳答案

    在这种情况下 DT::replaceDataresetPaging = FALSE应该可以正常工作,如图所示 here .但是,定义 x作为 reactiveValues()导致我使用 isolate 解决的一些问题

     server <- function(input, output, session) {
        x = reactiveValues(df = hardcoded_df)
        output$x1 = renderDT(DT::datatable(isolate(x$df), 
                    options = list(pageLength = page_length), selection = 'none', editable = TRUE))
    
        proxy = dataTableProxy('x1')
    
        data = reactiveValues()
        observeEvent(input$x1_cell_edit, {
          info = input$x1_cell_edit
          str(info)
          # str(input$x1_state)
          i = info$row
          j = info$col
          v = info$value
    
          # Without this line the table does not change but with it it jumps to row 1 after an edit.
          x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
          DT::replaceData(proxy, x$df, resetPaging = FALSE)  # important
          # Now we need to scroll to row i somehow ... clearly this does not work. Help!
          #selectPage(proxy, ceiling(i / page_length))
          # selectRow(proxy, i)
        })
      }
    

    关于R Shiny - 多页可编辑数据表在编辑后跳转到第 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55215433/

    相关文章:

    r - Shiny :将 styleColorBar 与来自两个数据框的数据一起使用

    r - 在 R 中绘制双变量高斯分布

    r - 将访客计数和分析添加到 R blogdown > netlify 托管网站

    r - 如何更新 R shiny 中 for 循环内的无功输出

    r - 无法将 ggproto 对象添加在一起

    javascript - 数据表中列的摘要(来自包 DT)

    r - 如何在数据表的 styleEqual() 中指定缺失或 NA?

    r - mutate 和 rowSums 排除列

    r - 以相反的方向创建水平条形图

    R Shiny : Side by Side Checkbox