如何使用 DT 包编辑和保存对 Shiny dataTable 所做的更改

Posted

技术标签:

【中文标题】如何使用 DT 包编辑和保存对 Shiny dataTable 所做的更改【英文标题】:How to edit and save changes made on Shiny dataTable using DT package 【发布时间】:2020-03-07 19:10:19 【问题描述】:

我想编辑表格并将更改保存到下面的示例中。这是我目前所拥有的。

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)


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

    mainPanel(
      tabsetPanel(
        id = 'dataset',
        tabPanel("Sample Bank", DT::dataTableOutput("banking.df"),
                 br(),
                 actionButton("saveBtn","Save")))))

# Define server logic ----
server <- function(input, output) 
  d1 = banking.df
  d1$Date = Sys.time() + seq_len(nrow(d1))

  output$banking.df=render_dt(d1,'cell')

  #edit a single cell
  proxy5=dataTableProxy('banking.df')
  observeEvent(input$banking.df_cell_edit,
    info=input$banking.df_cell_edit
    str(info)
    d1<<-editData(d1,info)
    replaceData(proxy5,d1,resetPaging = FALSE)
  )

  #observeEvent(input$saveBtn,write.csv(input$banking.df),
  #             file="NewData.csv",row.names=FALSE)




# Run the app ----
shinyApp(ui = ui, server = server)

我可以编辑表格,它会存储更改。但是,每当我尝试对特定列进行新的编辑时,它不会保存更改并将其保留为空白。例如,如果我在部门输入“H”。不允许更改的列。我怎样才能解决这个问题? 另外,如何使用“保存按钮”来覆盖banking.df? 谢谢。

【问题讨论】:

【参考方案1】:

我尝试了您的代码并修改了一些元素。有几点需要清除:

您可以使用 DT 属性 editable=TRUE。 要下载为csv或excel,可以使用DT默认CSV,Excel按钮。它可以存储编辑的数据。 如果您想使用单独的按钮下载,您可以简单地使用 write.csv()。

代码: UI.R

library(shiny)
library(DT)

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")
               ))))

服务器.R

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))

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()
  
  )

希望这会有所帮助。

【讨论】:

这很棒。非常感谢!

以上是关于如何使用 DT 包编辑和保存对 Shiny dataTable 所做的更改的主要内容,如果未能解决你的问题,请参考以下文章

使用因子变量的下拉选择在 Shiny 中编辑数据表

如何使用 DT、R 和 Shiny 在表格的单元格中嵌入图像

R语言-在shiny中使用DT包的常用设置

R Shiny DT - 使用反应式编辑表中的值

如何在正确位置使用 SelectInput 时保存在 DT 中所做的编辑

在 Shiny 中通过串扰将 Plotly 与 DT 一起使用