首页 > 解决方案 > 当我编辑反应式 DT 时,DT 输出将我带到第一页

问题描述

我在下面有我的代码的玩具版本。我闪亮的 DT 输出中的一列(第 7 列)是可编辑的。当我编辑列的单元格时,它会将我带回到列的第一行。我检查了环境中的数据表对象,编辑的单元格确实得到了更新。所以,这很好。但我想在编辑单元格后留在同一页面上。这是因为用户可能已经应用了一些过滤器来到达某个页面,然后当他编辑一个单元格时,他想从那里继续而不是返回开始。

我是 R 新手,所以任何帮助都将不胜感激。

我正在使用 DT 0.7

我的数据框有 7 列:大陆、州、国家、日期、速率(污染)、车辆、备注(可编辑列)

用户可以通过选择输入、范围和滑块输入来过滤表格输出。我想让该输出可编辑。

提前致谢!

library(shiny)
library(DT)

ui <- navbarPage("Hello",
  tabPanel("Tab1",
           sidebarLayout( 
             sidebarPanel( width = 4,
               selectInput("continent", "Select:",
                           choices = ""),

               selectInput("country" , "Select:",
                            choices = ""),

               selectInput("state" , "Select:",
                           choices = ""),

               dateRangeInput("date", "Select:",
                              startview = "month",
                              minview = "months", 
                              maxview = "decades",
                              start = as.Date('1999-01-01'),
                              end = as.Date(today()),
                              separator = "-"), 

               sliderInput("rate", "Select:",
                           min = 1, max = 5, value = c(1,5), 
                           dragRange = TRUE)),

               mainPanel(
                 tabsetPanel(
                   tabPanel("Analysis",
                             dataTableOutput("Table1")

           )))))



#server
server <- function(input, output, session)
{

  observe({
    updateSelectInput(session, "continent",
                             choices = c("All", unique(Df$Continent)))
    })

  observe({
    updateSelectInput(session, "country",
                      choices = c("All", Df %>%
                        filter(`Continent` == input$continent) %>%
                        select(Country)))
  })

  observe({
    updateSelectInput(session, "state",
                      choices = c("All", Df %>%
                        filter(`Continent` == input$continent & 
                                 `Country` == input$country) %>%
                        select(State)))
  })



  #create reactive table
  RecTable <- reactive({

    Df
      if(input$continent != "All") {
        Df <- Df[Df$Continent == input$continent,]
      }

      if(input$country != "All") {
        Df <- Df[Df$Country == input$country,]
      }

      if(input$state != "All") {
        Df <- Df[Df$State == input$state,]
      }

      Df <- Df %>%
        filter(Date >= input$date[1] & Date <= input$date[2]) %>%
        filter(Rate >= input$rate[1] & Rate <= input$rate[2])

      Df})

  output$Table1 <- DT::renderDT({
    DT::datatable(RecTable(),
                  rownames = FALSE ,
                  editable = list(target = 'cell', disable = list(columns = c(0:6)))) 

  })

  proxy1 <- dataTableProxy('Table1')

  observeEvent(input$Table1_cell_edit, {

    Df <<- editData(Df, input$Table1_cell_edit, 'Table1', rownames = FALSE, resetPaging = FALSE)

     })}

#run
shinyApp(ui = ui, server = server)

标签: rshinyshiny-serverdtshiny-reactivity

解决方案


推荐阅读