Combining `dplyr::across()`, `furrr::future_map_chr()`, and `dplyr::cur_column()`

53 Views Asked by At

I am trying to apply a function that leverages dplyr::cur_column() across every value in multiple columns using purrr:map() and dplyr::across(). In a simple reprex, I am able to combine across(), map(), and cur_column() as in the following:

# Load required packages
library(dplyr)
library(furrr)
library(palmerpenguins)
library(purrr)

mutate(
  penguins,
  across(
    .cols = everything(),
    .fns = \(column) map_chr(column, \(value) cur_column())
  )
)

# # A tibble: 344 × 8
#    species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#    <chr>   <chr>  <chr>          <chr>         <chr>             <chr>       <chr> <chr>
#  1 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  2 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  3 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  4 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  5 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  6 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  7 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  8 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
#  9 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
# 10 species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex   year 
# # ℹ 334 more rows
# # ℹ Use `print(n = ...)` to see more rows

However, if I attempt to execute this code asynchronously with furrr::future_map_chr(), I get the following:

# Using furrr with any number of workers > 1 produces an error
plan(multisession, n_workers = 2)

mutate(
  penguins,
  across(
    .cols = everything(),
    .fns = \(column) future_map_chr(column, \(value) cur_column())
  )
)

# Error in `mutate()`:
# ℹ In argument: `across(...)`.
# Caused by error in `across()`:
# ! Can't compute column `species`.
# Caused by error:
# ℹ In index: 1.
# Caused by error in `cur_column()`:
# ! Must only be used inside `across()`.
# Run `rlang::last_trace()` to see where the error occurred.

I imagine this has something to do with how furrr is splitting the data among sessions. Does anyone know how I might be able to work around this error?

0

There are 0 best solutions below