Giter Site home page Giter Site logo

Hooks about tarchetypes HOT 7 CLOSED

ropensci avatar ropensci commented on June 11, 2024
Hooks

from tarchetypes.

Comments (7)

wlandau avatar wlandau commented on June 11, 2024

We could also use tidyselect to choose which targets to apply the hooks.

from tarchetypes.

wlandau avatar wlandau commented on June 11, 2024

Also, let's consider supporting arbitrary expressions instead of just single-argument functions. A hook could look something like tar_hook_outer(target_object, hook = run_in_docker(code = .x, hostname = "localhost")), and the wrapped command could go in .x.

from tarchetypes.

wlandau avatar wlandau commented on June 11, 2024

EDIT

Please see #44 (comment). I decided to veer away from in-place modifications.

original post

A few implementation issues that will probably come up repeatedly in subsequent discussions:

Composition of target lists

Should we return a flattened list of target objects, e.g. unlist(list(targets), recursive = TRUE), or should we keep the target list nested in the way the user defines it originally, e.g. tar_map(unlist = FALSE)? I would think the latter. This outcome seems more likely to prevent surprises. Hooks should be minimally invasive when possible.

Modification in place

Should the hooks modify the target objects in place, or should they return deep copies of those objects? If we were to make deep copies, we would probably need to flatten the target list. (Recursing over an arbitrarily nested list and making arbitrary changes requires passing by reference unless we come up with some messy index scheme to keep track of where we are in the list at all times.) Above, we already decided against flattening the target list, so we should go with in-place modifications.

Return value

Given the decision to go with modification in place, we have a choice when it comes to the return value. We could either return the modified target list, or we could invisibly return NULL. I strongly prefer the latter because it emphasizes that target objects are modified in place. The behavior is limiting, and it prevents the use of the magrittr pipe, but it is a strong guardrail that I believe will prevent egregious and counterintuitive user errors.

Drawback: potential to accidentally drop targets

Users might not read the docs and incorrectly assume tar_hook_before() returns target objects. In that case, those targets may not make it into the pipeline. This kind of mistake is not so costly because no extra runtime is spent on misspecified targets, so I think this is the right tradeoff.

Drawback: metaprogramming limitations

Users might want to apply multiple hooks to a common template target object. However, this is already possible with tar_map(). The template can be created with tar_target() and possibly tar_hook_first() etc., and then tar_map() could be invoked afterwards.

Best alternative

The best alternative I see to all this is to return a flattened list of deep-copied target objects. I still prefer not to do this because

  1. It would be more invasive, and
  2. Later on, if the demand is high enough, we can always build wrappers on top of tar_hook_first() etc. that make deep copies beforehand. We could either name those functions tar_hook_first_copy() etc. or think of another term besides "hook". But let's try to pursue slick workarounds before we jump to an implementation.

from tarchetypes.

wlandau avatar wlandau commented on June 11, 2024

TL;DR: I am trying to make hooks safe, even if that makes them a bit limited (at first).

from tarchetypes.

wlandau avatar wlandau commented on June 11, 2024

On second thought, I think it's fine if hooks flatten the target list. I don't think people need the actual structure in a target list all that often.

from tarchetypes.

wlandau avatar wlandau commented on June 11, 2024

So #44 (comment) is a justification for flattening the target list, not modification in place.

from tarchetypes.

wlandau avatar wlandau commented on June 11, 2024

Implemented with the deep copy route.

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.