tarchetypes

repository·main·Indexed 41 days ago

https://github.com/ropensci/tarchetypes

An R package providing target and pipeline archetypes for the 'targets' package. It includes tools for dynamic branching over grouped data frames via functions like tar_group_by(), literate programming report rendering with tar_render(), and an alternative pipeline syntax using tar_plan().

Tokens
870
Snippets
4
Records
4
Agent score
50%

What's inside tarchetypes

  1. Dynamic branching over grouped data frames

    main

    The tarchetypes package provides functions to define row groups in a data frame, allowing downstream dynamic targets to automatically branch over these groups. This is useful for running the same analysis on different subsets of data.

    Available grouping functions:

    • tar_group_by(): Uses dplyr::group_by() semantics.
    • tar_group_select(): Uses tidyselect semantics.
    • tar_group_count(): Defines a specific number of row groups.
    • tar_group_size(): Defines row groups of a specific size.
    # _targets.R file:
    library(targets)
    library(tarchetypes)
    
    produce_data <- function() {
      expand.grid(var1 = c("a", "b"), var2 = c("c", "d"), rep = c(1, 2, 3))
    }
    
    list(
      tar_group_by(data, produce_data(), var1, var2),
      tar_target(group, data, pattern = map(data))
    )
  2. Install tarchetypes

    main

    You can install tarchetypes from CRAN, GitHub, or the rOpenSci development repository.

    # From CRAN
    install.packages("tarchetypes")
    
    # From GitHub (requires remotes package)
    remotes::install_github("ropensci/tarchetypes")
    
    # From rOpenSci development repository
    install.packages("tarchetypes", repos = "https://dev.ropensci.org")
  3. Use tar_render() for literate programming reports

    main

    The tar_render() archetype simplifies the process of rendering R Markdown reports within a targets pipeline. It automatically scans the report file (e.g., .Rmd) for mentions of targets via tar_load() or tar_read() and enforces the necessary dependency relationships. This ensures that the report is automatically re-rendered whenever its input data targets change.

    # _targets.R
    library(targets)
    library(tarchetypes)
    
    list(
      tar_target(dataset, data.frame(x = letters)),
      tar_render(report, "report.Rmd")
    )
  4. Use tar_plan() for alternative pipeline syntax

    main

    tar_plan() is a drop-in replacement for drake_plan() in the targets ecosystem. It allows you to write targets as simple name/command pairs, avoiding the need to wrap every target in an explicit tar_target() call. Note that when using target archetypes like tar_render() inside tar_plan(), they are still supported.

    tar_plan(
      tar_file(raw_data_file, "data/raw_data.csv", format = "file"),
      
      # Simple name/command syntax:
      raw_data = read_csv(raw_data_file, col_types = cols()),
      data = raw_data %>% mutate(Ozone = replace_na(Ozone, mean(Ozone, na.rm = TRUE))),
      hist = create_plot(data),
      fit = biglm(Ozone ~ Wind + Temp, data),
      
      # Using an archetype within tar_plan:
      tar_render(report, "report.Rmd")
    )