问题
I'm writing a shiny app that loads a list of names and dates and displays them in a datatable.
I want to use the editable
functionality of datatables to allow the user to update one of the dates, click a save button and overwrite the original data with the updated data.
This is what I have so far;
library(shiny)
library(shinydashboard)
library(tidyverse)
library(DT)
users <- reactiveFileReader(
intervalMillis = 100000,
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(users(),'appData/userTest.csv', row.names = FALSE)
})
output$userTable <- renderDataTable({
DT::datatable(users(),
editable = TRUE)
})
}
shinyApp(ui = ui, server = server)
My data looks like this;
userName start end
1 John 06/08/2019 <NA>
2 Mary 01/01/2019 <NA>
3 Mike 23/10/2019 01/10/2019
4 Steve 25/07/2019 <NA>
5 Kate 01/01/2019 29/04/2019
While this does save the users()
data, it only saves the original dataset, not the data from the edited table; I need the user to be able to enter a date, click save, then for the reactiveFileReader
to load the dataset with the changes applied.
Possibly I'm misunderstanding something fundamental with how the editable tables work...
Can this be done?
回答1:
So I figured out that adding the following;
edited <- reactive({editData(users(), input$userTable_cell_edit, proxy = NULL, rownames = FALSE, resetPaging = FALSE)})
observeEvent(
input$do,{
write.csv(edited(),'appData/userTest.csv', row.names = FALSE)
})
allows me to edit a single cell and update the csv. It doesn't allow me to edit more than one cell at a time, however. Will post a new question
edit: posted new question Editing multiple cells in a datatable in shiny
来源:https://stackoverflow.com/questions/58593824/write-editable-shiny-datatable-to-csv-file