-
I am encountering an issue when applying library(targets)
library(tidyverse)
library(tarchetypes)
mean_plus_x <- function(group_vars, x) {
mtcars %>%
group_by(!!!group_vars) %>%
summarize(mean(mpg) + 1, .groups = "drop")
}
list(
tar_map(
values = tidyr::expand_grid(
x = c(0, 1, 2),
group_vars = list(
dplyr::vars(cyl),
dplyr::vars(cyl, gear)
)
),
tar_target(summary, mean_plus_x(group_vars, x)),
names = c(group_vars, x)
)
)
#> Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 9, 6 Created on 2021-04-26 by the reprex package (v1.0.0) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hello, maybe "hello" and "please" would be appreciated here don't you think? |
Beta Was this translation helpful? Give feedback.
-
Hi @djbirke, In deparse(list(vars(cyl), vars(cyl, gear)))
#> [1] "list(structure(list(~cyl), .Names = \"\", class = c(\"quosures\", "
#> [2] "\"list\")), structure(list(~cyl, ~gear), .Names = c(\"\", \"\"), class = c(\"quosures\", "
#> [3] "\"list\")))" So I recommend you create another column for the names. library(targets)
tar_script({
options(tidyverse.quiet = TRUE)
library(tarchetypes)
library(tidyverse)
values <- expand_grid(
x = c(0, 1, 2),
tibble(
group_vars = list(vars(cyl), vars(cyl, gear)),
group_names = c("cyl", "cyl_gear")
)
)
list(
tar_map(
values = values,
names = all_of(c("x", "group_names")), # Omits group_vars.
tar_target(summary, mean_plus_x(group_vars, x))
)
)
})
tar_manifest()
#> # A tibble: 6 x 3
#> name command pattern
#> <chr> <chr> <chr>
#> 1 summary_2_cyl mean_plus_x(list(~cyl), 2) <NA>
#> 2 summary_1_cyl mean_plus_x(list(~cyl), 1) <NA>
#> 3 summary_2_cyl_gear mean_plus_x(list(~cyl, ~gear), 2) <NA>
#> 4 summary_0_cyl mean_plus_x(list(~cyl), 0) <NA>
#> 5 summary_1_cyl_gear mean_plus_x(list(~cyl, ~gear), 1) <NA>
#> 6 summary_0_cyl_gear mean_plus_x(list(~cyl, ~gear), 0) <NA> Created on 2021-04-27 by the reprex package (v2.0.0) |
Beta Was this translation helpful? Give feedback.
Hi @djbirke,
In
tar_map()
, thenames
argument refers to columns ofvalues
that can be deparsed to suffixes of target names.group_vars
is difficult to deparse, and the result is not usable for target names.So I recommend you create another column for the names.