在保持位置的同时更新Shiny DataTable的行 [英] Update row(s) of a Shiny DataTable while maintaining position

查看:73
本文介绍了在保持位置的同时更新Shiny DataTable的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Shiny应用程序,该应用程序在屏幕顶部显示data.frame信息,在底部显示特定的变量统计信息。用户可以通过与 DT :: datatable 对象进行交互来导航data.frame列。

I am creating a Shiny app that displays data.frame information at the top of the screen and specific variable stats at the bottom. The user can navigate the data.frame columns by interacting with a DT::datatable object.

当用户单击一个变量,将显示可编辑的详细信息。我希望此信息能够更新并反映在数据表中。我的问题是,当我更新表时,它从一开始就呈现并显示。 进行编辑后如何保留数据表的页面和行选择?

When a user clicks on a variable, detailed information is presented that can be edited. I would like this information to be updated and reflected in the datatable. My problem is that when I update the table, it is rendered and shown starting at the very beginning. How can I preserve the page and row selection of the datatable after making edits?

以下是一个最小的工作示例,该示例显示了mtcars数据集 DT :: datatable 。我有一些更新字段的控件。请注意,数据表已重新渲染回首页。

Here is a minimal working example that shows the mtcars dataset in a DT::datatable. I have some controls that update fields. Notice that datatable is re-rendered back to the first page.

library(shiny)

runApp(shinyApp(

  ui = fluidPage(
    title = "minimal-working-example",
    fluidRow(
      column(3, inputPanel(
        selectInput("field", "Field", choices = names(mtcars)),
        numericInput("value", "Value", 0),
        actionButton("submit", "Submit")
      )),

      column(9,
        DT::dataTableOutput("table")
      )
    )
  ),

  server = function(input, output) {

    v <- reactiveValues(mtcars=mtcars)

    observeEvent(input$submit, {
      v$mtcars[input$field] <- input$value
    })

    output$table <- DT::renderDataTable({
      DT::datatable(
        v$mtcars,
        selection = "single",
        options = list(pageLength = 5))
    })
  }
))

会话信息:

Session info --------------------------
 setting  value                       
 version  R version 3.3.0 (2016-05-03)
 system   x86_64, mingw32             
 ui       RStudio (0.99.902)          
 language (EN)                        
 collate  English_United States.1252  
 tz       America/Chicago             
 date     2016-07-11                  

Packages -------------------------------
 package     * version     date       source                        
 DT            0.1.45      2016-02-09 Github (rstudio/DT@a63e9ac)   
 shiny       * 0.13.0.9000 2016-02-08 Github (rstudio/shiny@e871934)


推荐答案

这可以在R内部完成,而无需通过JS或类似方法进入数据表的结构。

This can be done from inside R without getting into the structure of the datatable through JS or something like that.

我们利用从 DT 包中获得的各种表状态信息来呈现新的更新后的 datatable 像以前一样。 DT 文档

We utilize the various table state information we get from the DT package to render the new updated datatable like the one before. Everything we use is discribed in this DT documentation.

第一项:选择。您可以通过在数据表的 selection 参数内添加 selected = ... 来预选择行。可以将其与变量 input $ table_rows_selected 结合使用,以保存先前选择的行并在重新渲染时预先选择该行。

Item one: Selection. You can pre-select rows by adding selected = ... inside the selection argument of the datatable. This can be combined with the variable input$table_rows_selected to save the previously selected row and pre-select that exact row on re-rendering.

第二项:页面。 datatable 包具有一个 displayStart 选项,该选项指定呈现表时应首先显示哪一行。 此处的文档。因此,如果每页有5行,则 displayStart = 9 将在第3页上开始显示。(JavaScript数组从0开始,因此总是减去1。)这可以与 input $ table_rows_current 结合使用这是当前可见行号的向量。如果我们存储第一个条目(减去1),就知道从哪里开始显示。

Item two: Page. The datatable package has an option displayStart that specifies which row should be shown first when rendering the table. Documentation here. So, if you have 5 rows per page, displayStart = 9 would start the display on page 3. (JavaScript arrays start at 0, so always subtract 1.) This can be combined with input$table_rows_current which is a vector of currently visible row numbers. If we store the first entry (minus 1), we know where to start the display.

下面的完整代码示例:

library(shiny)

runApp(shinyApp(

  ui = fluidPage(
    title = "minimal-working-example",
    fluidRow(
      column(3, inputPanel(
        selectInput("field", "Field", choices = names(mtcars)),
        numericInput("value", "Value", 0),
        actionButton("submit", "Submit")
      )),

      column(9,
        DT::dataTableOutput("table")
      )
    )
  ),

  server = function(input, output) {

    v <- reactiveValues(mtcars=mtcars)
    previousSelection <- NULL
    previousPage <- NULL

    observeEvent(input$submit, {
      previousSelection <<- input$table_rows_selected
      previousPage <<- input$table_rows_current[1] - 1

      v$mtcars[input$field] <- input$value
    })

    output$table <- DT::renderDataTable({
      DT::datatable(
        v$mtcars,
        selection = list(mode = "single", target = "row", selected = previousSelection),
        options = list(pageLength = 5, displayStart = previousPage))
    })
  }
))

这篇关于在保持位置的同时更新Shiny DataTable的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆