dataEditCommit() saves the changes made in a data editor created by
dataEditServer(), for example when the user presses a
Save button in your shiny application. dataEditChanges()
returns a summary of the changes that have not been saved yet, which is
useful to enable a Save button or to ask the user to confirm.
dataEditLog() returns the change log: every change made in the
editor, with when it was made and by whom.
Arguments
- x
the reactive expression returned by
dataEditServer().
Value
dataEditCommit() invisibly returns a list with the number of
rows updated, inserted and deleted, the number of
columns added, removed or renamed and the log entries of
the changes that were saved (NULL if no data is loaded).
dataEditChanges() returns the same counts plus the number of
edited cells and the total number of changes.
dataEditLog() returns the change log. Both must be called in a
reactive context (they update as the data is edited).
Details
For database tables (see db_table()) the staged changes are
written to the database in a single transaction: edited rows are updated,
removed rows are deleted and new rows are inserted. If any row has been
changed or removed by someone else since it was loaded, the whole commit is
rolled back, nothing is written and an error of class
"editr_conflict" is raised; the changes stay in the editor so the
user can try again. After a successful commit the rows are loaded from the
database again, so new rows show the keys generated by the database and
the editor starts afresh.
For data in your R session (data frames, matrices, files, ...) nothing is
written; the current data simply becomes the new starting point, so the
review panel, change highlights and generated code start again from the
committed data. Write the data returned by dataEditServer() wherever
it needs to go in the same observer.
Change log
Every change made in the editor is recorded, one row per changed cell (or per row or column that was added, removed or renamed), in a data frame with the columns:
- session
when the editing session started (e.g.
"2026-11-02 09:27:27.123"), which names the session when logs of many sessions are kept together. Restoring a draft carries on its session.- time
when the change was made.
- user
who made it (see the
userargument ofdataEditServer()).- step
number of the action the change belongs to. One action (typing a value, a paste, a fill, an undo, ...) is one step, the same unit that undo works with.
- how
how the change was made:
"edit","paste","fill","shift"(cells inserted or deleted, moving the cells below),"clear","undo","redo"or"revert"(from the review panel), or the column action:"formula","replace","recode","type"or"sort".- code
for column actions, the
dplyrcode of the action, e.g.mutate(kpl = mpg * 0.425).- action
"edit","add row","remove row","add column","remove column","rename column","change type"or"reorder rows".- row
the row name, the key of a database row or else the row number in the data that was opened (new rows are numbered after the last row).
- column
the name of the column.
- old, new
the values before and after, as text. For a removed row
oldlists the values of the whole row.- note
the note added to the step in the History tab, which users can use to say why a change was made.
Undoing a change is recorded as a new step, so the log is a complete
record of what happened. The log is kept for the whole session, including
across commits; the entries saved by a commit are returned by
dataEditCommit() so that they can be written next to the data (for
example to a log table in the same database).
Author
Dillon Hammill, dillon.hammill21@gmail.com
Examples
if (interactive() && requireNamespace("RSQLite", quietly = TRUE)) {
library(shiny)
con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
DBI::dbExecute(con, "CREATE TABLE cars (id INTEGER PRIMARY KEY,
model TEXT, mpg REAL)")
DBI::dbAppendTable(con, "cars", data.frame(
model = rownames(mtcars), mpg = mtcars$mpg))
ui <- bslib::page_fluid(
shinyjs::useShinyjs(),
dataEditUI(
"cars",
toolbar_right = actionButton("save", "Save", class = "btn-primary")
)
)
server <- function(input, output, session) {
cars <- dataEditServer("cars", data = dplyr::tbl(con, "cars"))
# only enable saving when there is something to save
observe({
ch <- dataEditChanges(cars)
shinyjs::toggleState("save", !is.null(ch) && ch$total > 0)
})
observeEvent(input$save, {
res <- tryCatch(dataEditCommit(cars), editr_conflict = function(e) e)
if (inherits(res, "editr_conflict")) {
showNotification(conditionMessage(res), type = "error")
} else {
# keep a record of who changed what next to the data
if (nrow(res$log) > 0) {
DBI::dbWriteTable(con, "cars_log", res$log, append = TRUE)
}
showNotification(paste(res$updated, "updated,", res$inserted,
"added and", res$deleted, "removed"))
}
})
}
shinyApp(ui, server)
}