【发布时间】:2020-02-23 23:09:57
【问题描述】:
我之前问过这个问题 - 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() 函数一次只记录一个编辑。
如何一次编辑和保存多个单元格?
【问题讨论】:
-
看我的回答here
标签: r shiny datatables shinydashboard