Spatial (sf) function fails when called as an RStudio job; works otherwise

I have a function that does some spatial calculations using sf tools. I'm using split and purrr::map_df to pass this function to each row of an sfc tibble with around 7,000 rows. (Each row is a polygon (sfg)).

If I run the map "manually" at the console prompt, it works fine, but it does take about 2 hours to return its eventual output.
So I have tried running it as an RStudio job.

When I do this, I get the error

Input must be a vector, not a sfc_MULTIPOLYGON/sfc object.

Something doesn't like having to work with a tibble that has a geometry column/attributes?
This error looks like it is connected to this issue, related to sf and tibble/vctrs. But I don't understand why I am only getting the error when I run it as a job.

It's exactly the same code, copied and pasted from the R script I'm working in to a separate script for the job. I'm running the job with a copy of the local environment, which I need to do as it contains the large-ish geo data objects I need my function to iterate over.

Any ideas please?

I have sf version 0.9-7, tibble 3.1.0 and vctrs 0.3.6

—-
Here's the script of the job:

library(dplyr)
library(purrr)

msoa_pcon_lookup <- msoa_bounds %>%
  slice_sample(n = 5) %>% # for testing (error occurs without this line too)
  split(.$msoa11cd) %>%
  map_df(~ scan_local_constituencies(., msoa_centroids, pcon_bounds))

and here's the backtrace:

Backtrace:
     x
  1. +-global::sourceWithProgress(...)
  2. | \-base::eval(statements[[idx]], envir = sourceEnv)
  3. |   \-base::eval(statements[[idx]], envir = sourceEnv)
  4. |     \-`%>%`(...)
  5. +-purrr::map_df(...)
  6. | \-purrr::map(.x, .f, ...)
  7. +-base::split(., .$msoa11cd)
  8. +-dplyr::slice_sample(., n = 5)
  9. +-dplyr:::slice_sample.data.frame(., n = 5)
 10. | +-dplyr::slice(...)
 11. | \-dplyr:::slice.data.frame(...)
 12. |   +-dplyr::dplyr_row_slice(.data, loc, preserve = .preserve)
 13. |   \-dplyr:::dplyr_row_slice.data.frame(.data, loc, preserve = .preserve)
 14. |     +-dplyr::dplyr_reconstruct(vec_slice(data, i), data)
 15. |     | \-dplyr:::dplyr_new_data_frame(data)
 16. |     |   +-row.names %||% .row_names_info(x, type = 0L)
 17. |     |   \-base::.row_names_info(x, type = 0L)
 18. |     \-vctrs::vec_slice(data, i)
 19. \-vctrs:::stop_scalar_type(...)
 20.   \-vctrs:::stop_vctrs(msg, "vctrs_error_scalar_type", actual = x)
Execution halted

I can post more details of my scan_local_constituencies function if anyone thinks that will help.

Ugh, problem solved by including

library(sf)

in the job script :tired_face: even though an sf function isn't directly called in the script.

Lesson I should have already learned: try restarting your R session to empty your environment before filing bug reports. Issues with jobs are nearly always going to be something to do with what's implicitly in the environment.
Leaving this thread up anyway in case it helps someone in future.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.