add_case_when: Use a case_when function against a database

Description Usage Arguments Details Value See Also Examples

View source: R/add_case_when.R

Description

add_case_when is useful when you have previously created a case_when function with create_case_when() and want to use it against a database with dbplyr.

Usage

1

Arguments

con

A DBIConnection object.

...

Not used.

Details

In order to be safely used with a pipe, add_case_when does not throw any error. In case of internal error, the original connection object is returned with warning.

Value

A new DBIConnection object with a customised translation.

See Also

create_case_when, create_sql_case_when

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
25
26
27
28
29
30
if (requireNamespace("RSQLite", quietly = TRUE)) {
  library(dplyr)

  cw_fb <- create_case_when(
    number %% 35 == 0 ~ "fizz buzz",
    number %% 5 == 0 ~ "fizz",
    number %% 7 == 0 ~ "buzz",
    TRUE ~ as.character(number),
    vars = "number"
  )

  con <-
    DBI::dbConnect(RSQLite::SQLite(), ":memory:") %>%
    add_case_when(cw_fb)

  # You can print con to retrieve informations about custom translation
  con

  numbers <- copy_to(con, data.frame(x = 1:50, y = 51:100), "numbers")

  fizzbuzz <-
    numbers %>%
    mutate(fb_x = cw_fb(x), fb_y = cw_fb(y))

  fizzbuzz %>% show_query()

  fizzbuzz %>% collect()

  DBI::dbDisconnect(con)
}

RLesur/casewhen documentation built on May 5, 2019, 12:29 a.m.