-
Hi! Happy New Year, and thank you so much for such a wonderful ecosystem of packages. My question may be related to #35, but I was wondering if there was a good way to use In the example below, I am mapping over data (mtcars and iris), subsetting them, and would like to combine the results into one data frame at the end. Because they don't have the same columns, I can't call Thanks so much 😄 targets::tar_dir({
targets::tar_script({
mapped <- tarchetypes::tar_map(
targets::tar_target(
first,
head(data)
),
targets::tar_target(
last,
tail(data)
),
values = tibble::tibble(
data = rlang::syms(c("iris", "mtcars"))
)
)
combined <- tarchetypes::tar_combine(
combined_data,
mapped,
command = dplyr::bind_rows(!!!.x)
)
list(mapped, combined)
})
targets::tar_manifest()
})
#> # A tibble: 5 × 3
#> name command pattern
#> <chr> <chr> <chr>
#> 1 last_mtcars "tail(mtcars)" <NA>
#> 2 last_iris "tail(iris)" <NA>
#> 3 first_iris "head(iris)" <NA>
#> 4 first_mtcars "head(mtcars)" <NA>
#> 5 combined_data "dplyr::bind_rows(first_iris = first_iris, first_mtcars… <NA> Created on 2022-01-09 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thanks, @jdtrat. In your case, I think it would be convenient to put the library(targets)
tar_script({
library(tarchetypes)
library(tidyselect)
# https://armchairecology.blog/iris-dataset/
# https://allisonhorst.github.io/palmerpenguins/
library(palmerpenguins)
subsets <- list(
tar_target(
first,
head(data)
),
tar_target(
last,
tail(data)
)
)
combined <- tar_combine(
combined,
subsets,
command = dplyr::bind_rows(!!!.x)
)
tar_map(
list(subsets, combined),
values = tibble::tibble(
data = rlang::syms(c("mtcars", "penguins"))
),
unlist = TRUE # Flattened list of targets with informative names.
)
})
tar_visnetwork() Created on 2022-01-09 by the reprex package (v2.0.1) In case that doesn't fit the use case, here is an alternative. library(targets)
tar_script({
library(tarchetypes)
library(tidyselect)
# https://armchairecology.blog/iris-dataset/
# https://allisonhorst.github.io/palmerpenguins/
library(palmerpenguins)
mapped <- tar_map(
tar_target(
first,
head(data)
),
tar_target(
last,
tail(data)
),
values = tibble::tibble(
data = rlang::syms(c("mtcars", "penguins"))
),
unlist = TRUE # Flattened list of targets with informative names.
)
combined_mtcars <- tar_combine(
combined_mtcars,
mapped[eval_select(ends_with("mtcars"), mapped)],
command = dplyr::bind_rows(!!!.x)
)
combined_penguins <- tar_combine(
combined_penguins,
mapped[eval_select(ends_with("penguins"), mapped)],
command = dplyr::bind_rows(!!!.x)
)
list(mapped, combined_mtcars, combined_penguins)
})
tar_visnetwork()
#> Warning message:
#> Targets and globals must have unique names. Ignoring global objects that conflict with target names: combined_mtcars, combined_penguins. Warnings like this one are important, but if you must suppress them, you can do so with Sys.setenv(TAR_WARN = "false"). Created on 2022-01-09 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
Thanks, @jdtrat. In your case, I think it would be convenient to put the
tar_combine()
target insidetar_map()
. Sketch: