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

tar_select_targets() returns incorrect targets when they are not in alphabetical order #92

Closed
7 tasks done
arcruz0 opened this issue Apr 7, 2022 · 1 comment
Closed
7 tasks done
Assignees

Comments

@arcruz0
Copy link

arcruz0 commented Apr 7, 2022

Prework

  • Read and agree to the code of conduct and contributing guidelines.
  • Confirm that your issue is a genuine bug in tarchetypes and not a known limitation, usage error, or issue from another package that tarchetypes depends on. If you are unsure, please submit a discussion thread instead.
  • If there is already a relevant issue, whether open or closed, comment on the existing thread instead of posting a new issue.
  • Post a minimal reproducible example like this one so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.

Description

Thanks for the great package ecosystem! Sorry if I am misunderstanding anything here.

tar_select_targets() returns incorrect targets when they are not in alphabetical order. In the reprex below, I slightly modified the example in the tar_select_targets() help file, to change the order of targets within the targets list, and also make the returned values a bit more obvious. Notice how in the output the returned "y1" target is actually the "x" target (see names and command).

I think that the issue comes from the sort() function in this line:

names(targets) <- sort(map_chr(targets, ~.x$settings$name))

Reproducible example

library(tarchetypes)

targets::tar_dir({ # tar_dir() runs code from a temporary directory.
  targets <- list(
    list(
      targets::tar_target(y1, "y1_val"),
      targets::tar_target(x, "x_val")
    ),
    targets::tar_target(y2, "y2_val"),
    targets::tar_target(z, "z_val")
  )
  tar_select_targets(targets, starts_with("y"), contains("z"))
})
#> $y1
#> <tar_stem> 
#>   name: x 
#>   command:
#>     "x_val" 
#>   format: rds 
#>   iteration method: vector 
#>   error mode: stop 
#>   memory mode: persistent 
#>   storage mode: main 
#>   retrieval mode: main 
#>   deployment mode: worker 
#>   priority: 0 
#>   resources:
#>     list() 
#>   cue:
#>     mode: thorough
#>     command: TRUE
#>     depend: TRUE
#>     format: TRUE
#>     iteration: TRUE
#>     file: TRUE 
#>   packages:
#>     tarchetypes
#>     stats
#>     graphics
#>     grDevices
#>     utils
#>     datasets
#>     methods
#>     base 
#>   library:
#>     NULL
#> $y2
#> <tar_stem> 
#>   name: y2 
#>   command:
#>     "y2_val" 
#>   format: rds 
#>   iteration method: vector 
#>   error mode: stop 
#>   memory mode: persistent 
#>   storage mode: main 
#>   retrieval mode: main 
#>   deployment mode: worker 
#>   priority: 0 
#>   resources:
#>     list() 
#>   cue:
#>     mode: thorough
#>     command: TRUE
#>     depend: TRUE
#>     format: TRUE
#>     iteration: TRUE
#>     file: TRUE 
#>   packages:
#>     tarchetypes
#>     stats
#>     graphics
#>     grDevices
#>     utils
#>     datasets
#>     methods
#>     base 
#>   library:
#>     NULL
#> $z
#> <tar_stem> 
#>   name: z 
#>   command:
#>     "z_val" 
#>   format: rds 
#>   iteration method: vector 
#>   error mode: stop 
#>   memory mode: persistent 
#>   storage mode: main 
#>   retrieval mode: main 
#>   deployment mode: worker 
#>   priority: 0 
#>   resources:
#>     list() 
#>   cue:
#>     mode: thorough
#>     command: TRUE
#>     depend: TRUE
#>     format: TRUE
#>     iteration: TRUE
#>     file: TRUE 
#>   packages:
#>     tarchetypes
#>     stats
#>     graphics
#>     grDevices
#>     utils
#>     datasets
#>     methods
#>     base 
#>   library:
#>     NULL

Created on 2022-04-07 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.1.2 (2021-11-01)
#>  os       Pop!_OS 20.04 LTS           
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_US:en                    
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Chicago             
#>  date     2022-04-07                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                               
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.0)                       
#>  backports     1.4.0      2021-11-23 [1] RSPM (R 4.1.0)                       
#>  base64url     1.4        2018-05-14 [1] RSPM (R 4.1.0)                       
#>  callr         3.7.0      2021-04-20 [1] CRAN (R 4.1.0)                       
#>  cli           3.1.0      2021-10-27 [1] CRAN (R 4.1.1)                       
#>  codetools     0.2-18     2020-11-04 [4] CRAN (R 4.0.3)                       
#>  crayon        1.4.2      2021-10-29 [1] CRAN (R 4.1.1)                       
#>  data.table    1.14.2     2021-09-27 [1] RSPM (R 4.1.0)                       
#>  DBI           1.1.1      2021-01-15 [1] CRAN (R 4.1.0)                       
#>  digest        0.6.29     2021-12-01 [1] RSPM (R 4.1.0)                       
#>  dplyr         1.0.8      2022-02-08 [1] RSPM (R 4.1.0)                       
#>  ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)                       
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 4.1.0)                       
#>  fansi         0.5.0      2021-05-25 [1] CRAN (R 4.1.0)                       
#>  fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)                       
#>  fs            1.5.2      2021-12-08 [1] RSPM (R 4.1.0)                       
#>  generics      0.1.1      2021-10-25 [1] RSPM (R 4.1.0)                       
#>  glue          1.6.2      2022-02-24 [1] RSPM (R 4.1.0)                       
#>  highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)                       
#>  htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.1)                       
#>  igraph        1.2.6      2020-10-06 [1] CRAN (R 4.1.0)                       
#>  knitr         1.36       2021-09-29 [1] RSPM (R 4.1.0)                       
#>  lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.1)                       
#>  magrittr      2.0.2      2022-01-26 [1] RSPM (R 4.1.0)                       
#>  pillar        1.6.4      2021-10-18 [1] RSPM (R 4.1.0)                       
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)                       
#>  processx      3.5.2      2021-04-30 [1] CRAN (R 4.1.0)                       
#>  ps            1.6.0      2021-02-28 [1] CRAN (R 4.1.0)                       
#>  purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)                       
#>  R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.1)                       
#>  reprex        2.0.0      2021-04-02 [1] CRAN (R 4.1.0)                       
#>  rlang         1.0.1      2022-02-03 [1] RSPM (R 4.1.0)                       
#>  rmarkdown     2.10       2021-08-06 [1] CRAN (R 4.1.1)                       
#>  rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)                       
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 4.1.1)                       
#>  stringi       1.7.6      2021-11-29 [1] RSPM (R 4.1.0)                       
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)                       
#>  styler        1.4.1      2021-03-30 [1] CRAN (R 4.1.0)                       
#>  tarchetypes * 0.5.0.9000 2022-04-07 [1] Github (ropensci/tarchetypes@d8bded8)
#>  targets       0.10.0     2022-01-07 [1] RSPM (R 4.1.0)                       
#>  tibble        3.1.6      2021-11-07 [1] RSPM (R 4.1.0)                       
#>  tidyselect    1.1.1      2021-04-30 [1] CRAN (R 4.1.0)                       
#>  utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.1)                       
#>  vctrs         0.3.8      2021-04-29 [1] CRAN (R 4.1.0)                       
#>  withr         2.4.3      2021-11-30 [1] RSPM (R 4.1.0)                       
#>  xfun          0.29       2021-12-14 [1] RSPM (R 4.1.0)                       
#>  yaml          2.2.1      2020-02-01 [1] CRAN (R 4.1.0)                       
#> 
#> [1] /home/andres/R/x86_64-pc-linux-gnu-library/4.1
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

Expected result

The returned targets should not depend on how the targets are ordered in the input list. I think that simply removing sort() from the function fixes the issue (see reprex below), but I might be misunderstanding its role?

Thank you!

library(tarchetypes)

tar_select_targets2 <- function (targets, ...) 
{
  targets <- unlist(list(targets), recursive = TRUE)
  names(targets) <- purrr::map_chr(targets, ~.x$settings$name) # no sort()
  out <- tidyselect::eval_select(rlang::expr(c(...)), data = targets, 
                                 strict = FALSE)
  targets[names(out)]
}


targets::tar_dir({ # tar_dir() runs code from a temporary directory.
  targets <- list(
    list(
      targets::tar_target(y1, "y1_val"),
      targets::tar_target(x, "x_val")
    ),
    targets::tar_target(y2, "y2_val"),
    targets::tar_target(z, "z_val")
  )
  tar_select_targets2(targets, starts_with("y"), contains("z"))
})
#> $y1
#> <tar_stem> 
#>   name: y1 
#>   command:
#>     "y1_val" 
#>   format: rds 
#>   iteration method: vector 
#>   error mode: stop 
#>   memory mode: persistent 
#>   storage mode: main 
#>   retrieval mode: main 
#>   deployment mode: worker 
#>   priority: 0 
#>   resources:
#>     list() 
#>   cue:
#>     mode: thorough
#>     command: TRUE
#>     depend: TRUE
#>     format: TRUE
#>     iteration: TRUE
#>     file: TRUE 
#>   packages:
#>     tarchetypes
#>     stats
#>     graphics
#>     grDevices
#>     utils
#>     datasets
#>     methods
#>     base 
#>   library:
#>     NULL
#> $y2
#> <tar_stem> 
#>   name: y2 
#>   command:
#>     "y2_val" 
#>   format: rds 
#>   iteration method: vector 
#>   error mode: stop 
#>   memory mode: persistent 
#>   storage mode: main 
#>   retrieval mode: main 
#>   deployment mode: worker 
#>   priority: 0 
#>   resources:
#>     list() 
#>   cue:
#>     mode: thorough
#>     command: TRUE
#>     depend: TRUE
#>     format: TRUE
#>     iteration: TRUE
#>     file: TRUE 
#>   packages:
#>     tarchetypes
#>     stats
#>     graphics
#>     grDevices
#>     utils
#>     datasets
#>     methods
#>     base 
#>   library:
#>     NULL
#> $z
#> <tar_stem> 
#>   name: z 
#>   command:
#>     "z_val" 
#>   format: rds 
#>   iteration method: vector 
#>   error mode: stop 
#>   memory mode: persistent 
#>   storage mode: main 
#>   retrieval mode: main 
#>   deployment mode: worker 
#>   priority: 0 
#>   resources:
#>     list() 
#>   cue:
#>     mode: thorough
#>     command: TRUE
#>     depend: TRUE
#>     format: TRUE
#>     iteration: TRUE
#>     file: TRUE 
#>   packages:
#>     tarchetypes
#>     stats
#>     graphics
#>     grDevices
#>     utils
#>     datasets
#>     methods
#>     base 
#>   library:
#>     NULL

Created on 2022-04-07 by the reprex package (v2.0.0)

Diagnostic information

  • Session info in the reprex above
  • SHA-1 hash of the GitHub commit of tarchetypes currently installed: d8bded8
@wlandau
Copy link
Member

wlandau commented Apr 8, 2022

Thanks @arcruz0, should be fixed in 5de84df.

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