left_join fails on a sf object in quarto *only* when read from rds

I'm running into a weird issue with sf objects that causes rendering to fail only if I read in the object from an rds file.

In my quarto doc rendering to html, the following code renders fine:

library(tidyverse)

# import map data
us_geo <-
  tigris::states(class = "sf", cb = TRUE) %>%
  tigris::shift_geometry() %>%
  filter(GEOID < 60)

# data that I want to plot
state_data <- read_csv("state_data.csv")

# join to be able to plot the map with state data
map_data <-
  us_geo %>%
  left_join(state_data, by = c("NAME" = "state")) # tigris uses NAME for state name

# plot
map_data %>%
  ...

Importing data from tigris is a little slow. Since I'm rendering a bunch of documents, I thought I'd save time by reading us_geo from an rds file instead, i.e., in a static file:

library(tidyverse)

# save map data to an rds file
tigris::states(class = "sf", cb = TRUE) %>%
  tigris::shift_geometry() %>%
  filter(GEOID < 60) %>%
  write_rds("us_geo.rds")

This works fine when I run interactively a-la running code chunks, but fails to join when I try to render the document with the warning below!

library(tidyverse)

# import map data
us_geo <- read_rds("us_geo.rds")

# data that I want to plot
state_data <- read_csv("state_data.csv")

# join to be able to plot the map with state data
map_data <-
  us_geo %>%
  left_join(state_data, by = c("NAME" = "state"))

# plot
map_data %>%
  ...
Error in `as_tibble()`:
! All columns in a tibble must be vectors.
✖ Column `geometry` is a `sfc_MULTIPOLYGON/sfc` object.
Backtrace:
 5. dplyr:::left_join.data.frame(., state_data, by = c(NAME = "state"))
 6. dplyr:::join_mutate(...)
 8. tibble:::as_tibble.data.frame(x, .name_repair = "minimal")

Again, this works just fine when I run the chunk interactively --- does anyone have an idea about what might be going on? (also, if there's a better way to generate a reprex for quarto docs, I'm all ears)

Do you have {sf} loaded? It kind of looks as if your R session did not know how to interpret the geometry column.

It is possible tigris loads sf in the background, but the call does not happen when loading the rds.

that did the trick --- you're a wizard, thank you!

I wish I was... :slight_smile: But I'm glad I could be of service!