modify,ANY,modifier-method | R Documentation |
Change records in a database table using modification rules specified
in a modifier()
object. This is the main function of package dcmodifydb
.
For more information see the vignettes.
## S4 method for signature 'ANY,modifier' modify( dat, x, copy = NULL, transaction = !isTRUE(copy), ignore_nw = FALSE, ... )
dat |
|
x |
|
copy |
if |
transaction |
if |
ignore_nw |
if |
... |
unused |
The modification rules are translated into SQL update statements and executed on the table.
Note that
by default the updates are executed on a copy of the table.
the default for transaction
is FALSE
when copy=TRUE
and
TRUE
when copy=FALSE
when transaction = TRUE
and a modification fails,
all modifications are rolled back.
tbl_sql()
object, referencing the modified table object.
library(DBI) library(dcmodify) library(dcmodifydb) # silly modification rules m <- modifier( if (cyl == 6) gear <- 10 , gear[cyl == 4] <- 0 # this R syntax works too :-) , if (gear == 3) cyl <- 2 ) # setting up a table in the database con <- dbConnect(RSQLite::SQLite()) dbWriteTable(con, "mtcars", mtcars[,c("cyl", "gear")]) tbl_mtcars <- dplyr::tbl(con, "mtcars") # "Houston, we have a table" head(tbl_mtcars) # lets modify on a temporary copy of the table.. # this copy is only visible to the current connection tbl_m <- modify(tbl_mtcars, m, copy=TRUE) # and gear has changed... head(tbl_m) # If one certain about the changes, then you can overwrite the table with the changes tbl_m <- modify(tbl_mtcars, m, copy=FALSE) dbDisconnect(con)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.