当我编辑反应式DT时,DT输出将我带到第一页

时间:2019-08-12 17:28:50

标签: r shiny shiny-server dt shiny-reactivity

我下面的代码有一个玩具版本。我在闪亮的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)

0 个答案:

没有答案