Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

sql_expr

Generate SQL from R expressions


Description

Low-level building block for generating SQL from R expressions. Strings are escaped; names become bare SQL identifiers. User infix functions have % stripped.

Usage

sql_expr(x, con = sql_current_con())

sql_call2(.fn, ..., con = sql_current_con())

Arguments

x

A quasiquoted expression

con

Connection to use for escaping. Will be set automatically when called from a function translation.

.fn

Function name (as string, call, or symbol)

...

Arguments to function

Details

Using sql_expr() in package will require use of globalVariables() to avoid R CMD check NOTES. This is a small amount of additional pain, which I think is worthwhile because it leads to more readable translation code.

Examples

con <- simulate_dbi() # not necessary when writing translations

sql_expr(f(x + 1), con = con)
sql_expr(f("x", "y"), con = con)
sql_expr(f(x, y), con = con)

x <- ident("x")
sql_expr(f(!!x, y), con = con)

sql_expr(cast("x" %as% DECIMAL), con = con)
sql_expr(round(x) %::% numeric, con = con)

sql_call2("+", quote(x), 1, con = con)
sql_call2("+", "x", 1, con = con)

dbplyr

A 'dplyr' Back End for Databases

v2.1.1
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre], Maximilian Girlich [aut], Edgar Ruiz [aut], RStudio [cph, fnd]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.