Giter Site home page Giter Site logo

Comments (7)

wlandau avatar wlandau commented on June 5, 2024 2

tar_map() uses static branching, so it does its thing before the pipeline runs. In other words, the label1 target may not even exist yet when tar_map() is called. To define new targets based on the values of upstream targets, you can use dynamic branching.

from tarchetypes.

wlandau avatar wlandau commented on June 5, 2024 1

I did, but I didn't realize a response from me would be helpful. Substituting vectors of variable length are out of scope for static branching, but it sounds like you found something that works for you. The underlying mechanism is substitute(), and it looks the deparsed vectors are being substituted correctly. Seems like you can continue with your workaround.

from tarchetypes.

wlandau avatar wlandau commented on June 5, 2024

values should be a named data frame or list, where columns/elements are atomic vectors or symbol lists. The names argument uses tidyselect to choose among names(values) to create the target names. The values list you created was nested enough that targets did not try to parse it into a set of friendly names. That sort of thing gets tricky if values gets complicated.

Here are examples similar to yours.

targets::tar_script({
  list(
    tarchetypes::tar_map(
      values = list(label1 = c("foo", "bar"), label2 = c("test1", "test2")),
      names = label1,
      targets::tar_target(b, label2)
    )
  )
})
  
targets::tar_manifest()
#> # A tibble: 2 x 3
#>   name  command     pattern
#>   <chr> <chr>       <chr>  
#> 1 b_foo "\"test1\"" <NA>   
#> 2 b_bar "\"test2\"" <NA>

targets::tar_script({
  list(
    tarchetypes::tar_map(
      values = list(label1 = c("foo", "bar"), label2 = c("test1", "test2")),
      names = starts_with("label"),
      targets::tar_target(b, label2)
    )
  )
})

targets::tar_manifest()
#> # A tibble: 2 x 3
#>   name        command     pattern
#>   <chr>       <chr>       <chr>  
#> 1 b_bar_test2 "\"test2\"" <NA>   
#> 2 b_foo_test1 "\"test1\"" <NA>

Created on 2021-04-08 by the reprex package (v1.0.0)

from tarchetypes.

adamaltmejd avatar adamaltmejd commented on June 5, 2024

Thanks for the quick reply and all your amazing work :). The thing is that in my real project I want to specifically map vectors of variable length. In my code its a setting specifying which variables the data set that I work with should be collapsed over (can be many). So I first run the code for a = c("foo", "bar") and then separately for a = c("test"). Your code is creating two separate targets, one for each element in label2.

I read in the help file that as you say values should be a list of vectors/data frame, but as you can see the nested list that I supplied did result in the correct mapping, only naming is wrong. b_foo returns "foo-bar" and b_bar returns "test1-test".

The code below works though:

targets::tar_dir({ # tar_dir() runs code from a temporary directory.
targets::tar_script({
  list(
      tarchetypes::tar_map(
        values = list(a = list(c("foo", "bar"), c("test1"), c(1, 2, 3)), names = c("foo", "test", "integers")),
        names = names,
        targets::tar_target(b, paste(a, collapse = "-"))
      )
  )
})
targets::tar_manifest()
})
  name       command                                        pattern
  <chr>      <chr>                                          <chr>  
1 b_integers "paste(c(1, 2, 3), collapse = \"-\")"          NA     
2 b_test     "paste(\"test1\", collapse = \"-\")"           NA     
3 b_foo      "paste(c(\"foo\", \"bar\"), collapse = \"-\")" NA     

Not sure why it works to supply a vector of names as values and then assign it to names, but not to assign a character vector immediately (as the help page for tar_map says is possible).

from tarchetypes.

adamaltmejd avatar adamaltmejd commented on June 5, 2024

Ping @wlandau, not sure if you saw my reply?

from tarchetypes.

adamaltmejd avatar adamaltmejd commented on June 5, 2024

Great, was thinking if perhaps my solution was perhaps the result of a bug but thats great. Thanks!

from tarchetypes.

c1au6i0 avatar c1au6i0 commented on June 5, 2024

targets::tar_script({
list(
tarchetypes::tar_map(
values = list(label1 = c("foo", "bar"), label2 = c("test1", "test2")),
names = label1,
targets::tar_target(b, label2) )
})

In this example that you @wlandau provided, could the element label1 be defined in another single target?
I tried creating the target and referring to it with label1 = rlang::sym("name_target") without success.
Thanks!

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.