| tar_sql | R Documentation | 
Shorthand to include a SQL query in a 'targets' pipeline.
tar_sql(
  name,
  path,
  params = list(),
  format = targets::tar_option_get("format"),
  tidy_eval = targets::tar_option_get("tidy_eval"),
  repository = targets::tar_option_get("repository"),
  iteration = targets::tar_option_get("iteration"),
  error = targets::tar_option_get("error"),
  memory = targets::tar_option_get("memory"),
  garbage_collection = targets::tar_option_get("garbage_collection"),
  deployment = targets::tar_option_get("deployment"),
  priority = targets::tar_option_get("priority"),
  resources = targets::tar_option_get("resources"),
  storage = targets::tar_option_get("storage"),
  retrieval = targets::tar_option_get("retrieval"),
  cue = targets::tar_option_get("cue")
)
name | 
 Symbol, name of the target. A target
name must be a valid name for a symbol in R, and it
must not start with a dot. Subsequent targets
can refer to this name symbolically to induce a dependency relationship:
e.g.   | 
path | 
 Character of length 1 to the single '*.sql' source file to be executed. Defaults to the working directory of the 'targets' pipeline.  | 
params | 
 Code, can be 'NULL'. 'params' evaluates to a named list of parameters that are passed to 'jinjar::render()'. The list is quoted (not evaluated until the target runs) so that upstream targets can serve as parameter values.  | 
format | 
 Optional storage format for the target's return value.
With the exception of   | 
tidy_eval | 
 Logical, whether to enable tidy evaluation
when interpreting   | 
repository | 
 Character of length 1, remote repository for target storage. Choices: 
 Note: if   | 
iteration | 
 Character of length 1, name of the iteration mode of the target. Choices: 
  | 
error | 
 Character of length 1, what to do if the target stops and throws an error. Options: 
  | 
memory | 
 Character of length 1, memory strategy.
If   | 
garbage_collection | 
 Logical, whether to run   | 
deployment | 
 Character of length 1, only relevant to
  | 
priority | 
 Numeric of length 1 between 0 and 1. Controls which
targets get deployed first when multiple competing targets are ready
simultaneously. Targets with priorities closer to 1 get built earlier
(and polled earlier in   | 
resources | 
 Object returned by   | 
storage | 
 Character of length 1, only relevant to
 
  | 
retrieval | 
 Character of length 1, only relevant to
 
  | 
cue | 
 An optional object from   | 
'tar_sql()' is an alternative to 'tar_target()' for SQL queries that depend on upstream targets. The SQL source files ('*.sql' files) should mention dependency targets with 'tar_load()' within SQL comments ('–'). (Do not use 'tar_load_raw()' or 'tar_read_raw()' for this.) Then, 'tar_sql()' defines a special kind of target. It 1. Finds all the 'tar_load()'/'tar_read()' dependencies in the query and inserts them into the target's command. This enforces the proper dependency relationships. (Do not use 'tar_load_raw()' or 'tar_read_raw()' for this.) 2. Sets 'format = "file"' (see 'tar_target()') so 'targets' watches the files at the returned paths and reruns the query if those files change. 3. Creates another upstream target to watch the query file for changes '<target name> ‘sqltargets_option_get("sqltargets.target_file_suffix")'’.
A data frame
targets::tar_dir({  # tar_dir() runs code from a temporary directory.
  # Unparameterized SQL query:
  lines <- c(
    "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())",
    "-- targets::tar_load(data1)",
    "-- targets::tar_load(data2)",
    "select 1 AS my_col",
    ""
  )
  # In tar_dir(), not part of the user's file space:
  writeLines(lines, "query.sql")
  # Include the query in a pipeline as follows.
  targets::tar_script({
    library(tarchetypes)
    library(sqltargets)
    list(
      tar_sql(query, path = "query.sql")
    )
  }, ask = FALSE)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.