modify-ANY-modifier-method: Modify records in a tbl

Description Usage Arguments Details Value Examples

Description

Modify records in a database table using modification rules specified in a modifier object.

Usage

1
2
## S4 method for signature 'ANY,modifier'
modify(dat, x, copy = NULL, transaction = !isTRUE(copy), ...)

Arguments

dat

tbl_sql() object, table in a SQL database

x

dcmodify::modifier() object.

copy

if TRUE (default), modify copy of table

transaction

if TRUE use one transaction for all modifications.

...

unused

Details

The modification rules are translated into SQL update statements and executed on the table. Note that

Value

tbl_sql() object, referencing the modified table object.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 copy of the table...
tbl_m <- modify(tbl_mtcars, m, copy=TRUE)
# and gear has changed...
head(tbl_m)

dbDisconnect(con)

dcmodifydb documentation built on Jan. 21, 2022, 5:06 p.m.