【问题标题】:DT with Shiny: Multipage editable DataTable jumps to first page after an editDT with Shiny:多页可编辑数据表在编辑后跳转到第一页
【发布时间】:2019-08-04 23:32:57
【问题描述】:

我有以下程序。正如标题所示,每次我在第一页之后的页面上编辑项目时,表格都会返回到第一页。我希望表格保留在我正在编辑的页面上,而不跳回第一页。

我在这里的其他线程上看到过这个问题,但他们的解决方案似乎不适用于当前版本的 DTshiny 包。

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DTOutput('x1'),
    verbatimTextOutput("print")
  ),
  server = function(input, output, session) {
    x = reactiveValues(df = NULL)

    observe({
      df <- iris
      df$Date = Sys.time() + seq_len(nrow(df))
      x$df <- df
    })

    output$x1 = renderDT(x$df, selection = 'none', editable = TRUE)

    proxy = dataTableProxy('x1')

    observeEvent(input$x1_cell_edit, {
      info = input$x1_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]))
    })

    output$print <- renderPrint({
      x$df
    })
  }
)

任何帮助将不胜感激

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    你可以这样做:

    library(shiny)
    library(DT)
    shinyApp(
      ui = fluidPage(
        DTOutput('x1'),
        verbatimTextOutput("print")
      ),
      server = function(input, output, session) {
    
        dat <- reactiveVal(cbind(iris, Date = Sys.time() + seq_len(nrow(iris))))
    
        output$x1 = renderDT(isolate(dat()), selection = 'none', editable = TRUE)
    
        proxy = dataTableProxy('x1')
    
        observeEvent(input$x1_cell_edit, {
          info = input$x1_cell_edit
          dat(editData(dat(), info, proxy, resetPaging = FALSE))
        })
    
        output$print <- renderPrint({
          dat()
        })
      }
    )
    

    【讨论】:

    • 如果有人在使用reactiveValues,您只需替换两行代码:1) 将dat &lt;- reactiveVal(cbind(iris, Date = Sys.time() + seq_len(nrow(iris)))) 替换为dat &lt;- reactiveValues(test=cbind(iris, Date = Sys.time() + seq_len(nrow(iris))))。 2) 将dat(editData(dat(), info, proxy, resetPaging = FALSE)) 替换为editData(dat$table, info, proxy, resetPaging = FALSE)
    【解决方案2】:

    请参阅DT-edit。我复制了以下 2 个相关示例:

    library(shiny)
    library(DT)
    
    dt_output = function(title, id) {
        fluidRow(column(
            12, h1(paste0('Table ', sub('.*?([0-9]+)$', '\\1', id), ': ', title)),
            hr(), DTOutput(id)
        ))
    }
    render_dt = function(data, editable = 'cell', server = TRUE, ...) {
        renderDT(data, selection = 'none', server = server, editable = editable, ...)
    }
    
    shinyApp(
        ui = fluidPage(
            title = 'Double-click to edit table cells',
    
            dt_output('client-side processing (editable = "cell")', 'x1'),
            dt_output('server-side processing (editable = "cell")', 'x5')
        ),
    
        server = function(input, output, session) {
            d1 = iris
            d1$Date = Sys.time() + seq_len(nrow(d1))
            d5 = d1
    
            options(DT.options = list(pageLength = 5))
    
            # client-side processing
            output$x1 = render_dt(d1, 'cell', FALSE)
    
            observe(str(input$x1_cell_edit))
    
            # server-side processing
            output$x5 = render_dt(d5, 'cell')
    
            # edit a single cell
            proxy5 = dataTableProxy('x5')
            observeEvent(input$x5_cell_edit, {
                info = input$x5_cell_edit
                str(info)  # check what info looks like (a data frame of 3 columns)
                d5 <<- editData(d5, info)
                replaceData(proxy5, d5, resetPaging = FALSE)  # important
                # the above steps can be merged into a single editData() call; see examples below
            })
    
        }
    )
    

    我不确定为什么您似乎使用 reactiveValues 使过程不必要地复杂化,但这可能是您的表格需要刷新回第一页的原因。

    【讨论】:

    • 我一定是误会了,抱歉。也许你应该在你的问题中明确这一点,因为我回答了我对它的理解,并且没有提到 reactiveValues 是一个要求。
    猜你喜欢
    • 2019-08-08
    • 2020-10-21
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多