编辑数据表中的多个单元格,效果良好

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

我之前问过这个问题-Write editable shiny Datatable to .csv file尝试编辑闪亮的数据表中的单元格,然后将原始数据替换为编辑后的数据。

我能够弄清楚如何编辑数据表中的单个单元格,但是,如果编辑了多个单元格,则仅保存最后的编辑。

代码:

library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)


users <- reactiveFileReader(
  intervalMillis = 1000,  
  NULL,
  filePath = 'appData/userTest.csv',
  readFunc = read.csv,
  stringsAsFactors = FALSE
)

header <- dashboardHeader(title = "demo")
sidebar <- dashboardSidebar(uiOutput('sidebar'))
body <- dashboardBody(uiOutput("body"))

f1 <- fluidRow(
  box(
    dataTableOutput('userTable'),
    width = 6
  )
)

ui <- dashboardPage(title = 'admin function test', header, sidebar, body, skin='blue')

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

  output$body <- renderUI({
    tabItems(
      tabItem(
        tabName = 'admin', class = 'active', h2(f1)
      )
    )
  })

  output$sidebar <- renderUI({
    sidebarMenu(id = 'sidebarmenu',
                menuItem("admin", tabName = "admin", icon = icon("adjust")),
                actionButton("do", 'save', icon = icon('redo'))
    )
  })

  observeEvent(
    input$do,{
      write.csv(edited(),'appData/userTest.csv', row.names = FALSE)
    })

  output$userTable <- renderDataTable({
    DT::datatable(users(),
                  editable = TRUE,
                  rownames = FALSE)
  })

  edited <- reactive({editData(users(), input$userTable_cell_edit, proxy = NULL, rownames = FALSE, resetPaging = FALSE)})
}

shinyApp(ui = ui, server = server)

数据:

   userName      start        end
1      John 06/08/2019 01/10/2019
2      Mary 01/01/2019 01/10/2019
3      Mike 23/10/2019 01/10/2019
4     Steve 25/07/2019 07/02/2015
5      Kate 01/01/2019 29/04/2019

我想这是因为editData()函数一次只记录一次编辑。如何一次性编辑和保存多个单元格?

r shiny datatables shinydashboard
1个回答
0
投票

请参见下文,这将允许进行多个更改,并且每个更改都会被跟踪。

library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)

header <- dashboardHeader(title = "demo")
sidebar <- dashboardSidebar(
  sidebarMenu(id = 'sidebarmenu',
              menuItem("admin", tabName = "admin", icon = icon("adjust")),
              downloadButton("downloadResults","Download Results")
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(
      tabName = 'admin', class = 'active', 
        fluidRow(
          box(
            dataTableOutput('userTable'), width = 6
          )
        )
    )
  )
)


ui <- dashboardPage(title = 'admin function test', header, sidebar, body, skin='blue')

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

  dat <- data.frame(userName = c("John","Mary","Mike"), start = c("06/08/2019","01/01/2019","23/10/2019"), stringsAsFactors = FALSE)

  output$userTable <- renderDataTable({
    DT::datatable(isolate(dat),
                  editable = TRUE,
                  rownames = FALSE)
  })

  ###Tracking Changes###
  rvs <- reactiveValues(
    data = NA #dynamic data object
  )

  observe({
    rvs$data <- dat
  })

  proxy = dataTableProxy('userTable')
  observe({
    DT::replaceData(proxy, rvs$data, rownames = FALSE, resetPaging = FALSE)
  })

  observeEvent(input$userTable_cell_edit, {
    rvs$data <<- editData(rvs$data, input$userTable_cell_edit, rownames = FALSE)
  })

  # observeEvent(
  #   input$do,{
  #     write.csv(rvs$data,'userTest.csv', row.names = FALSE)
  #   })

  output$downloadResults <- downloadHandler(
    filename = function(){paste("userTest.csv.csv", sep = "")},
    content = function(file){write.csv(rvs$data, file, row.names = FALSE)}
  )

}

shinyApp(ui = ui, server = server)

如果要同时编辑多个单元格,请将此行添加到数据表中:

editable = list(target = "all") #can be all, row, or column

将来发布时的一些技巧:

  1. 创建一个可复制的示例,使其无法从您计算机上的文件中提取数据/保存数据
  2. 使代码尽可能简单,以解决您的问题。将renderUI丢到正文和侧边栏只会使您的代码混乱。
© www.soinside.com 2019 - 2024. All rights reserved.