Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[general] Use tar_rep() and tar_rep2() inside of tar_map() #165

Closed
1 task done
wlandau opened this issue Jan 3, 2024 Discussed in #164 · 2 comments
Closed
1 task done

[general] Use tar_rep() and tar_rep2() inside of tar_map() #165

wlandau opened this issue Jan 3, 2024 Discussed in #164 · 2 comments
Assignees

Comments

@wlandau
Copy link
Collaborator

wlandau commented Jan 3, 2024

Discussed in #164

Originally posted by psychelzh January 3, 2024

Help

Description

I found I could use tar_rep() inside of tar_map() without any issue, but tar_rep2() will result into wrong results. See the following example, tar_rep2() will be repeated too much. Is this intended?

targets::tar_dir({
  targets::tar_script({
    library(targets)
    tarchetypes::tar_map(
      list(z = letters[1:4]),
      tarchetypes::tar_rep(
        x,
        data.frame(a = rnorm(1)),
        batches = 2,
        reps = 3
      ),
      tarchetypes::tar_rep2(
        y,
        x,
        x
      )
    )
  })
  targets::tar_make(reporter = "silent")
  print(targets::tar_read(x_a))
  print(targets::tar_read(y_a))
})
#>             a tar_batch tar_rep    tar_seed
#> 1  0.92194625         1       1 -1934026178
#> 2 -1.04838174         1       2 -1027989593
#> 3  0.39842572         1       3 -1557401896
#> 4  1.08154612         2       1   609454960
#> 5  0.60904960         2       2     5408412
#> 6 -0.03204864         2       3 -1909983919
#>              a tar_batch tar_rep   tar_seed
#> 1   0.92194625         1       1  -74219856
#> 2  -1.04838174         1       1  -74219856
#> 3   0.39842572         1       1  -74219856
#> 4   0.92194625         1       2 -551422473
#> 5  -1.04838174         1       2 -551422473
#> 6   0.39842572         1       2 -551422473
#> 7   0.92194625         1       3  733186315
#> 8  -1.04838174         1       3  733186315
#> 9   0.39842572         1       3  733186315
#> 10  1.08154612         2       1  -57784111
#> 11  0.60904960         2       1  -57784111
#> 12 -0.03204864         2       1  -57784111
#> 13  1.08154612         2       2  187848175
#> 14  0.60904960         2       2  187848175
#> 15 -0.03204864         2       2  187848175
#> 16  1.08154612         2       3  241018303
#> 17  0.60904960         2       3  241018303
#> 18 -0.03204864         2       3  241018303

Created on 2024-01-03 with reprex v2.0.2

@wlandau wlandau self-assigned this Jan 3, 2024
@wlandau
Copy link
Collaborator Author

wlandau commented Jan 3, 2024

@psychelzh , thanks for letting me know. The metaprogramming in tar_rep2() was creating commands with named lists, and tar_map() cannot modify those names. Should be fixed in c716d40.

@psychelzh
Copy link

psychelzh commented Jan 3, 2024

Thank you very much for the quick fix, as always. Confirmed it is fixed:

targets::tar_dir({
  targets::tar_script({
    library(targets)
    tarchetypes::tar_map(
      list(z = letters[1:4]),
      tarchetypes::tar_rep(
        x,
        data.frame(a = rnorm(1)),
        batches = 2,
        reps = 3
      ),
      tarchetypes::tar_rep2(
        y,
        x,
        x
      )
    )
  })
  targets::tar_make(reporter = "silent")
  print(targets::tar_read(x_a))
  print(targets::tar_read(y_a))
})
#>             a tar_batch tar_rep    tar_seed
#> 1  0.92194625         1       1 -1934026178
#> 2 -1.04838174         1       2 -1027989593
#> 3  0.39842572         1       3 -1557401896
#> 4  1.08154612         2       1   609454960
#> 5  0.60904960         2       2     5408412
#> 6 -0.03204864         2       3 -1909983919
#>             a tar_batch tar_rep   tar_seed
#> 1  0.92194625         1       1  -74219856
#> 2 -1.04838174         1       2 -551422473
#> 3  0.39842572         1       3  733186315
#> 4  1.08154612         2       1  -57784111
#> 5  0.60904960         2       2  187848175
#> 6 -0.03204864         2       3  241018303

Created on 2024-01-03 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants