I often want to run an expression or call a function on each row of a tibble purely for the side-effect, without any interest in the return value. For example, I might have a tibble with a list column of data that I want to save using file names from a character column. There are of course a myriad ways to do this, but I would prefer to do it as elegantly as the other rowwise operations in dplyr (1.0). Essentially, I'm looking for an amalgam of rowwise mutate() and purrr::walk(). Here's an ugly approximation to what I want to do:
library(tidyverse)
dat <-
tibble(file = c("iris.csv", "mtcars.csv"),
data = list(iris, mtcars))
dat %>% rowwise() %>% mutate(x = list(write_csv(data, file))) %>% invisible()
Is there a way I can do away with the x = list(…) stuff and explicit hiding of the return value while maintaining easy access to the "data-variables" in the function call (without ugly stuff like .x$data[[1]])? Suppose there was such a function (walk_rows()?) I would expect to use it something like this:
dat %>% walk_rows(write_csv(data, file)))
I know I can do this:
dat %>% pwalk(function(dat, file) write_csv(dat, file))
But having to write the names of the data-variables twice is inelegant.