dbplyr translates commonly used base functions including logical (!, &, |), arithmetic (^), and comparison (!=) operators, as well as common summary (mean(), var()), and transformation (log()) functions. All other functions will be preserved as is. R's infix functions (e.g. %like%) will be converted to their SQL equivalents (e.g. LIKE).
..., dots: Expressions to translate. translate_sql()
automatically quotes them for you. translate_sql_() expects a list of already quoted objects.
con: An optional database connection to control the details of the translation. The default, NULL, generates ANSI SQL.
vars_group, vars_order, vars_frame: Parameters used in the OVER
expression of windowed functions.
window: Use FALSE to suppress generation of the OVER
statement used for window functions. This is necessary when generating SQL for a grouped summary.
context: Use to carry information for special translation cases. For example, MS SQL needs a different conversion for is.na() in WHERE vs. SELECT clauses. Expects a list.
Examples
con <- simulate_dbi()# Regular maths is translated in a very straightforward waytranslate_sql(x +1, con = con)translate_sql(sin(x)+ tan(y), con = con)# Note that all variable names are escapedtranslate_sql(like =="x", con = con)# In ANSI SQL: "" quotes variable _names_, '' quotes strings# Logical operators are converted to their sql equivalentstranslate_sql(x <5&!(y >=5), con = con)# xor() doesn't have a direct SQL equivalenttranslate_sql(xor(x, y), con = con)# If is translated into case whentranslate_sql(if(x >5)"big"else"small", con = con)# Infix functions are passed onto SQL with % removedtranslate_sql(first %like%"Had%", con = con)translate_sql(first %is%NA, con = con)translate_sql(first %in% c("John","Roger","Robert"), con = con)# And be careful if you really want integerstranslate_sql(x ==1, con = con)translate_sql(x ==1L, con = con)# If you have an already quoted object, use translate_sql_:x <- quote(y +1/ sin(t))translate_sql_(list(x), con = simulate_dbi())# Windowed translation --------------------------------------------# Known window functions automatically get OVER()translate_sql(mpg > mean(mpg), con = con)# Suppress this with window = FALSEtranslate_sql(mpg > mean(mpg), window =FALSE, con = con)# vars_group controls partition:translate_sql(mpg > mean(mpg), vars_group ="cyl", con = con)# and vars_order controls ordering for those functions that need ittranslate_sql(cumsum(mpg), con = con)translate_sql(cumsum(mpg), vars_order ="mpg", con = con)