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

时间:2019-11-12 05:23:02

标签: r shiny dt

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

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 个答案:

答案 0 :(得分:0)

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

  • 您可以使用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")
               ))))

Server.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()
  }
  )
}

希望这会有所帮助。 enter image description here