编辑数据表,但当我们移动到另一个页面时,更改就会消失

问题描述 投票:0回答:1

我从另一个论坛窃取代码如何使用 DT 包编辑和保存对闪亮数据表所做的更改。该论坛的解决方案(如下)效果很好。我注意到数据表的“editable = TRUE”功能似乎有一个错误。运行下面的代码,在界面上,更改一个单元格,点击第2页,然后再次返回第1页,您的更改就会消失。

我可以知道这里出了什么问题吗?还是数据表有bug?

library(shiny)
library(DT)

Admit<-c("Admitted","Rejected","Admitted", "Rejected", "Admitted", "Rejected", "Admitted",
         "Rejected","Admitted", "Rejected", "Admitted","Rejected","Admitted", "Rejected","Admitted","Rejected", "Admitted", "Rejected",
         "Admitted","Rejected", "Admitted" ,"Rejected","Admitted", "Rejected")
Gender<-c("Male","Male","Female","Female", "Male",   "Male",   "Female", "Female", "Male","Male","Female","Female",
          "Male","Male","Female","Female","Male",   "Male",   "Female", "Female","Male","Male","Female","Female")
Dept<-c( "A","A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F")
Freq<-c("512", "313",  "89",  "19", "353", "207",  "17",   "8", "120", "205", "202", "391", "138", "279", "131", "244",  "53", "138",
        "94", "299",  "22", "351",  "24", "317")

banking.df<-data.frame(Admit,Gender,Dept, Freq,stringsAsFactors = FALSE)
d1 = banking.df
d1$Date = Sys.time() + seq_len(nrow(d1))


# Define UI ----
ui <- fluidPage(
  titlePanel("UC Berkley Admissions"),
  
  mainPanel(
    tabsetPanel(
      id = 'dataset',
      tabPanel("Sample Bank", 
               
               DT::dataTableOutput("banking.df_data"),
               br(),
               actionButton("viewBtn","View"),
               br(),
               actionButton("saveBtn","Save"),
               br(),
               DT::dataTableOutput("updated.df")
      ))))

# Define server logic ----
server <- function(input, output) {
  
  output$banking.df_data<-renderDataTable(
    d1,selection = 'none', editable = TRUE, 
    rownames = TRUE,
    extensions = 'Buttons',
    
    options = list(
      paging = TRUE,
      searching = TRUE,
      fixedColumns = TRUE,
      autoWidth = TRUE,
      ordering = TRUE,
      dom = 'Bfrtip',
      buttons = c('csv', 'excel')
    ),
    
    class = "display"
  )
  
  
  observeEvent(input$banking.df_data_cell_edit, {
    d1[input$banking.df_data_cell_edit$row,input$banking.df_data_cell_edit$col] <<- input$banking.df_data_cell_edit$value
  })
  
  view_fun<-eventReactive(input$viewBtn,{
    if(is.null(input$saveBtn)||input$saveBtn==0)
    {
      returnValue()
    }
    else
    {
      DT::datatable(d1,selection = 'none')
    }
    
  })
  
  
  observeEvent(input$saveBtn,{
    write.csv(d1,'test.csv')
  })
  
  output$updated.df<-renderDataTable({
    view_fun()
  }
  )
}



# Run the app ----
shinyApp(ui = ui, server = server)
r shiny dt
1个回答
2
投票

您的原始程序旨在显示下面第二个表中的更改,当您单击查看按钮时会显示该表。要保留顶部表格中的更改而不显示第二个表格,请尝试以下代码。如有必要,重置按钮可返回到初始数据。

Admit<-c("Admitted","Rejected","Admitted", "Rejected", "Admitted", "Rejected", "Admitted",
         "Rejected","Admitted", "Rejected", "Admitted","Rejected","Admitted", "Rejected","Admitted","Rejected", "Admitted", "Rejected",
         "Admitted","Rejected", "Admitted" ,"Rejected","Admitted", "Rejected")
Gender<-c("Male","Male","Female","Female", "Male",   "Male",   "Female", "Female", "Male","Male","Female","Female",
          "Male","Male","Female","Female","Male",   "Male",   "Female", "Female","Male","Male","Female","Female")
Dept<-c( "A","A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F")
Freq<-c("512", "313",  "89",  "19", "353", "207",  "17",   "8", "120", "205", "202", "391", "138", "279", "131", "244",  "53", "138",
        "94", "299",  "22", "351",  "24", "317")

banking.df<-data.frame(Admit,Gender,Dept, Freq,stringsAsFactors = FALSE)
d1 = banking.df
d1$Date = Sys.time() + seq_len(nrow(d1))

library(shiny)
library(DT)

ui <- fluidPage(
  titlePanel("UC Berkley Admissions"),

  mainPanel(
    tabsetPanel(
      id = 'dataset',
      tabPanel("Sample Bank",

               DT::DTOutput("banking.df_data"),
               br(),
               actionButton("saveBtn","Save"),
               br(),
               actionButton(inputId = "reset", label = "Reset")
               
      ))))

server <- function(input, output) {
  DF1 <- reactiveValues(data=d1)
  orig_d1 <- d1

  output$banking.df_data<-renderDT(
    DF1$data,selection = 'none', editable = TRUE,
    rownames = TRUE,
    extensions = 'Buttons',

    options = list(
      paging = TRUE,
      searching = TRUE,
      fixedColumns = TRUE,
      autoWidth = TRUE,
      ordering = TRUE,
      dom = 'Bfrtip',
      buttons = c('csv', 'excel')
    )#, class = "display"
  )

  observeEvent(input$banking.df_data_cell_edit, {
    req(input$banking.df_data_cell_edit)
    d1[input$banking.df_data_cell_edit$row,input$banking.df_data_cell_edit$col] <<- input$banking.df_data_cell_edit$value
    DF1$data <- d1
  })

  observeEvent(input$saveBtn,{
      write.csv(d1,file='C:/My Disk Space/_My Work/RStuff/GWS/test.csv')
  })
  
  observeEvent(input$reset, {
    DF1$data <- orig_d1
  })

}

shinyApp(ui = ui, server = server)
© www.soinside.com 2019 - 2024. All rights reserved.