condusco

query-driven query generation in R

https://github.com/ras44/condusco

Science Score: 26.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
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (10.5%) to scientific vocabulary
Last synced: 11 months ago · JSON representation

Repository

query-driven query generation in R

Basic Info
Statistics
  • Stars: 11
  • Watchers: 0
  • Forks: 1
  • Open Issues: 2
  • Releases: 1
Created over 8 years ago · Last pushed over 2 years ago
Metadata Files
Readme Changelog

README.md

condusco

Overview

condusco lets you run a function iteratively, passing it the rows of a dataframe or the results of a query.

We call the functions condusco runs pipelines, and define a pipeline as a function that accepts a list of parameters and run a series of customized commands based on the values of the parameters.

The most common use case for condusco are data pipelines. For data pipelines that primarily run SQL queries, we can template queries with a library (ie. whisker), so that parametrized values are separated from the query logic. We can then render the query with the appropriate values:

``` parameters <- source("params.R")

define a pipeline

pipeline <- function(parameters){ query <- "SELECT * FROM {{dataset}}.{{tableprefix}}results LIMIT {{limitsize}}" querywithparams <- whisker.render(query, parameters) runquery(querywithparams) }

run the pipeline with the parameters in 'params.R'

pipeline(parameters) ```

condusco provides the following extensions in functionality to the above design pattern: - the user can provide a data-frame that contains multiple rows of parameters to be iteratively passed to the pipeline - the user can provide a query and each row of results is iteratively passed to the pipeline - any JSON-string parameter will be converted to an object before being passed to the pipeline

Functions

|function|description| |:--------------|:--------------| |runpipeline(pipeline, parameters)| iteratively pass each row of parameters to a pipeline, converting any JSON parameters to objects| |runpipelinegbq(pipeline, query, project)|calls runpipeline with the results of query executed via bigrquery| |runpipelinedbi(pipline, query, con)|calls run_pipeline with the results of query executed via DBI|

Installation

{r, eval = FALSE} install.packages("condusco")

Features

  • Name-based substitution of local parameters into pipelines, iterating through rows of parameters:

    {r} run_pipeline( #the pipeline function(parameters){ query <- "SELECT * FROM {{table_prefix}}_results;" print(whisker.render(query,parameters)) }, #the parameters data.frame( table_prefix = c('batman', 'robin') ) )

  • Name-based substitution of query-results into pipelines, iterating through rows of parameters dataframe:

    ```{r} con <- dbConnect(RSQLite::SQLite(), ":memory:")

    pipeline <- function(parameters){

    query <-" SELECT count(*) as nhits FROM userhits WHERE date(datetime) BETWEEN date('{{{datelow}}}') AND date('{{{date_high}}}') ;"

    whisker.render(query,parameters)

    }

    runpipelinedbi(pipeline, "SELECT date('now', '-5 days') as datelow, date('now') as datehigh", con )

    dbDisconnect(con) ```

  • Dynamic query generation based on JSON strings:

    ```{r} con <- dbConnect(RSQLite::SQLite(), ":memory:") mtcars dbWriteTable(con, "mtcars", mtcars)

    for each cylinder count, count the number of top 5 hps it has

    pipeline <- function(swap){

    query <- "SELECT {{#list}} SUM(CASE WHEN hp='{{val}}' THEN 1 ELSE 0 END )as nhp{{val}}, {{/list}} cyl FROM mtcars GROUP BY cyl ;"

    print(whisker.render(query,swap))

    print( dbGetQuery( con, whisker.render(query,swap) ) ) }

    pass the top 5 most common hps as val parameters

    runpipelinedbi( pipeline, ' SELECT "[" || GROUP_CONCAT("{ ""val"": """ || hp || """ }") || "]" AS list FROM ( SELECT CAST(hp as INTEGER) as HP, count(hp) as cnt FROM mtcars GROUP BY hp ORDER BY cnt DESC LIMIT 5 ) ', con )

    dbDisconnect(con) ```

Google BigQuery Examples

This is not available as a vignette because it requires user authentication

```{r } library(whisker) library(bigrquery) library(condusco)

Set GBQ project

project <- ''

Set the following options for GBQ authentication on a cloud instance

options("httroauthcache" = "~/.httr-oauth") options(httroobdefault=TRUE)

Run the below query to authenticate and write credentials to .httr-oauth file

query_exec("SELECT 'foo' as bar",project=project);

```

Dynamically generated queries via JSON

If list is defined, convert the JSON string to an object and iterate through name1,name2 pairs. This illustrates how to dynamically generate a query based on the JSON constructed by another query. In this example, we create a trivial JSON object manually. We'll use a dynamically generated JSON object in the next example. ```{r} pipeline <- function(params){

query <- "SELECT {{{value}}} as dollarswon, {{#list}} '{{name1}}' as {{name2}}, {{/list}} {{{field}}} as field FROM {{tablename}} LIMIT {{limit_size}} ;"

res <- queryexec(whisker.render(query,params), project=project, uselegacy_sql = FALSE );

print(res) }

project

runpipelinegbq(pipeline, " SELECT 1000 as value, 'word' as field, '[{\"name1\":\"foo\", \"name2\":\"bar\"},{\"name1\":\"foo2\", \"name2\":\"bar2\"}]' as list, 'publicdata:samples.shakespeare' AS tablename, 5 AS limitsize ", project)

```

Feature Generation Query

Create features for each of the repos describing how many commits the top 10 commiters made to that repo. ```{r} pipeline <- function(params){

query <- " SELECT {{#list}} SUM(CASE WHEN author.name ='{{name}}' THEN 1 ELSE 0 END) as n{{nameclean}}, {{/list}} reponame FROM `bigquery-public-data.githubrepos.samplecommits` GROUP BY reponame ;"

res <- queryexec( whisker.render(query,params), project=project, uselegacy_sql = FALSE );

print(res) }

runpipelinegbq(pipeline, " SELECT CONCAT('[', STRINGAGG( CONCAT('{\"name\":\"',name,'\",' ,'\"nameclean\":\"', REGEXPREPLACE(name, r'[^[:alpha:]]', ''),'\"}' ) ), ']') as list FROM ( SELECT author.name, COUNT(commit) ncommits FROM bigquery-public-data.github_repos.sample_commits GROUP BY 1 ORDER BY 2 DESC LIMIT 10 ) ", project, uselegacysql = FALSE )

```

Owner

  • Login: ras44
  • Kind: user

GitHub Events

Total
Last Year

Committers

Last synced: over 3 years ago

All Time
  • Total Commits: 4
  • Total Committers: 2
  • Avg Commits per committer: 2.0
  • Development Distribution Score (DDS): 0.25
Top Committers
Name Email Commits
rstevenson r****n@s****m 3
ras44 9****4@u****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: over 1 year ago

All Time
  • Total issues: 8
  • Total pull requests: 1
  • Average time to close issues: 7 minutes
  • Average time to close pull requests: N/A
  • Total issue authors: 2
  • Total pull request authors: 1
  • Average comments per issue: 0.0
  • Average comments per pull request: 1.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • repo-reviews (4)
  • ras44 (1)
Pull Request Authors
  • hadley (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 360 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
  • Total maintainers: 1
cran.r-project.org: condusco

Query-Driven Pipeline Execution and Query Templates

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 360 Last month
Rankings
Stargazers count: 17.0%
Forks count: 28.8%
Dependent packages count: 29.8%
Average: 31.2%
Dependent repos count: 35.5%
Downloads: 44.7%
Maintainers (1)
Last synced: over 1 year ago

Dependencies

DESCRIPTION cran
  • DBI * depends
  • R >= 3.3.2 depends
  • assertthat * depends
  • bigrquery * depends
  • jsonlite * depends
  • RSQLite * suggests
  • knitr * suggests
  • rmarkdown * suggests
  • testthat * suggests
  • whisker * suggests