sqltargets

targets extension for SQL queries

https://github.com/daranzolin/sqltargets

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.3%) to scientific vocabulary

Keywords

pipeline r rstats sql targets workflow
Last synced: 6 months ago · JSON representation

Repository

targets extension for SQL queries

Basic Info
Statistics
  • Stars: 40
  • Watchers: 2
  • Forks: 2
  • Open Issues: 7
  • Releases: 1
Topics
pipeline r rstats sql targets workflow
Created almost 2 years ago · Last pushed over 1 year ago
Metadata Files
Readme License Code of conduct

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
```

# sqltargets 


[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
[![R-CMD-check](https://github.com/daranzolin/sqltargets/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/daranzolin/sqltargets/actions/workflows/R-CMD-check.yaml)
[![CRAN status](https://www.r-pkg.org/badges/version/sqltargets)](https://CRAN.R-project.org/package=sqltargets)
[![](https://cranlogs.r-pkg.org/badges/sqltargets)](https://cran.r-project.org/package=sqltargets)
[![R Targetopia](https://img.shields.io/badge/R_Targetopia-member-blue?style=flat&labelColor=gray)](https://wlandau.github.io/targetopia/)



sqltargets makes it easy to integrate SQL files within your [targets workflows.](https://github.com/ropensci/targets) The shorthand `tar_sql()` creates two targets: (1) the ‘upstream’ SQL file; and (2) the ‘downstream’ result of the query. Dependencies can be specified by calling `tar_load()` within SQL comments. The template engine can be specified using the `sqltargets.template_engine` option (either 'glue' or 'jinjar').

## Installation

You can install sqltargets from CRAN with:

```r
install.packages("sqltargets")
```

You can install the development version of sqltargets with:

``` r
remotes::install_github("daranzolin/sqltargets)
```

## Demo

See the [sqltargets-demo repository](https://github.com/daranzolin/sqltargets-demo) for a reproducible demonstration.


## Dependencies

Use `tar_load` or `targets::tar_load` within a SQL comment to indicate query 
dependencies. Check the dependencies of any query with `tar_sql_deps`.

```{r}
library(sqltargets)
lines <- c(
   "-- !preview conn=DBI::dbConnect(RSQLite::SQLite())",
   "-- targets::tar_load(data1)",
   "-- targets::tar_load(data2)",
   "select 1 AS my_col",
   ""
 )
 query <- tempfile()
 writeLines(lines, query)
 tar_sql_deps(query)
```

## Parameters

You can pass parameters (presumably from another object in your targets project) to `tar_sql()` using one of two 'template engines': [glue](https://github.com/tidyverse/glue) or 'Jinja' (courtesy of [the 'jinjar' package.)](https://github.com/davidchall/jinjar)  

Set the 'template engine' with `sqltargets_option_set("sqltargets.template_engine", "jinjar")`. ('glue' is the default.)

With glue:

`query.sql`

```sql
-- !preview conn=DBI::dbConnect(RSQLite::SQLite())
-- tar_load(params)
select id
from table
where age > {age_threshold}
```

`_targets.R`

```{r eval = FALSE}
library(targets)
library(sqltargets)
list(
  tar_target(params, list(age_threshold = 30)),
  tar_sql(report, path = "query.sql", params = params)
  )
```

With 'Jinja':

`query.sql`

```sql
-- !preview conn=DBI::dbConnect(RSQLite::SQLite())
-- tar_load(payment_methods)
select
order_id,
{% for payment_method in params.payment_methods %}
sum(case when payment_method = '{{payment_method}}' then amount end) as {{payment_method}}_amount
{% if not loop.is_last %},{% endif %}
{% endfor %}
from payments
group by 1
```

`_targets.R`

```{r eval = FALSE}
library(targets)
library(sqltargets)

sqltargets_option_set("sqltargets.template_engine", "jinjar")

list(
  tar_target(payment_methods, list(payment_methods = c("bank_transfer", "credit_card", "gift_card"))),
  tar_sql(report, path = "query.sql", params = payment_methods)
  )
```

Note that `loop.is_last` differs from typical Jinja (`loop.last`). Refer to [this 'jinjar' vignette](https://davidchall.github.io/jinjar/articles/template-syntax.html) for other syntactical differences.

![](inst/tar_glimpse.png)

## Code of Conduct

Please note that the sqltargets project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.

## Acknowledgement

Much of the code has been adapted from [the excellent tarchetypes package.](https://github.com/ropensci/tarchetypes) Special 
thanks to the authors and Will Landau in particular for revolutionizing data pipelines in R.

Owner

  • Name: David Ranzolin
  • Login: daranzolin
  • Kind: user
  • Location: Santa Clara
  • Company: @SFOEWD

Data Analyst and Analytics Developer

GitHub Events

Total
  • Issues event: 1
  • Watch event: 3
Last Year
  • Issues event: 1
  • Watch event: 3

Issues and Pull Requests

Last synced: 7 months ago

All Time
  • Total issues: 16
  • Total pull requests: 10
  • Average time to close issues: 14 days
  • Average time to close pull requests: 3 days
  • Total issue authors: 2
  • Total pull request authors: 2
  • Average comments per issue: 0.5
  • Average comments per pull request: 0.5
  • Merged pull requests: 8
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 4
  • Pull requests: 6
  • Average time to close issues: 10 days
  • Average time to close pull requests: 4 days
  • Issue authors: 1
  • Pull request authors: 2
  • Average comments per issue: 0.25
  • Average comments per pull request: 0.33
  • Merged pull requests: 4
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • daranzolin (10)
  • psychelzh (1)
Pull Request Authors
  • daranzolin (15)
  • jennybc (2)
Top Labels
Issue Labels
question (3)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 108 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 4
  • Total maintainers: 1
cran.r-project.org: sqltargets

'Targets' Extension for 'SQL' Queries

  • Versions: 4
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 108 Last month
Rankings
Dependent packages count: 27.8%
Dependent repos count: 35.7%
Average: 49.4%
Downloads: 84.8%
Maintainers (1)
Last synced: 6 months ago

Dependencies

DESCRIPTION cran
  • DBI * imports
  • cli * imports
  • glue * imports
  • purrr * imports
  • readr * imports
  • stringr * imports
  • tarchetypes * imports
  • targets * imports
.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite