Giter Site home page Giter Site logo

Comments (3)

wlandau avatar wlandau commented on June 11, 2024

tar_render() tries to unify two opposite styles of reporting:

  1. With automation: render as part of a reproducible pipeline.
  2. With interactivity: render with the "Knit" button in the RStudio IDE.

If you write parameterized reports and iterate over lots of parameter values, you have abandoned (2) in favor of (1), which is why tar_render() does not try to accommodate dynamic branching. So I actually recommend:

  1. Use custom functions instead of tar_render(), and
  2. Reference upstream dependency targets using more conventional means instead of through tar_load() or tar_read().

Sketch:

lines <- c(
  "---",
  "title: '`r params$paramcd`'",
  "output: html_document",
  "params:",
  "  paramcd: 'TOTAL'",
  "---",
  "",
  "```{r}",
  "print(params$paramcd)",
  "print(object)",
  "```"
)

writeLines(lines, "source.Rmd")

library(targets)
tar_script({
  library(targets)
  library(tarchetypes)
  options(crayon.enabled = FALSE)
  render_output <- function(source, pars, ...) {
    output_file <- paste0(pars$paramcd, ".html")
    rmarkdown::render(
      source,
      output_file = output_file,
      params = pars,
      envir = parent.frame(), # Find targets in the environment like `object`.
      quiet = TRUE
    )
    output_file
  }
  tar_pipeline(
    tar_target(pars, data.frame(paramcd = c("param1", "param2"))),
    tar_target(object, 123),
    tar_target(source, "source.Rmd", format = "file"),
    tar_target(
      output,
      render_output(
        source = source, # Mention the Rmd source file by target name so the graph is connected.
        pars = pars,
        object = object # Mention any dependency targets by name.
      ),
      pattern = map(pars), # Dynamic branching over R Markdown parameters.
      format = "file"
    )
  )
})

tar_make()
#> ● run target object
#> ● run target pars
#> ● run target source
#> ● run branch output_36be00ef
#> ● run branch output_788b11ba

webshot::webshot("param2.html")

Created on 2020-12-12 by the reprex package (v0.3.0)

from tarchetypes.

yonicd avatar yonicd commented on June 11, 2024

As always thank you for the great help!

Using targets has been a great experience and has made my workflows much more reliable reducing turnaround time for audibles from stakeholders.

from tarchetypes.

yonicd avatar yonicd commented on June 11, 2024

This also solves my other question about how to render xaringan slides in targets. Ty

from tarchetypes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.