Helping R find the right methods for unserialized complex objects.

Automatically load package namespaces when reading serialized objects with this One Quick Trick.
R
Tutorials
Spatial
geospatial data
R packages
Author
Published

November 27, 2023

So here’s a problem you may have encountered. Say you’ve serialized some complicated R object using a function like saveRDS(), which saves your objects as a binary file. For instance, we can take the boston_canopy sf object from spatialsample, which looks like this:

spatialsample::boston_canopy |>
  head()
Simple feature collection with 6 features and 18 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 749383.6 ymin: 2913059 xmax: 801174.4 ymax: 2965741
Projected CRS: NAD83 / Massachusetts Mainland (ftUS)
# A tibble: 6 × 19
  grid_id land_area canopy_gain canopy_loss canopy_no_change canopy_area_2014
  <chr>       <dbl>       <dbl>       <dbl>            <dbl>            <dbl>
1 AB-4      795045.      15323.       3126.           53676.           56802.
2 I-33      265813.       8849.      11795.           78677.           90472.
3 AO-9      270153        6187.       1184.           26930.           28114.
4 H-10     2691490.      73098.      80362.          345823.          426185.
5 V-7       107890.        219.       3612.             240.            3852.
6 Q-22     2648089.     122211.     154236.         1026632.         1180868.
# ℹ 13 more variables: canopy_area_2019 <dbl>, change_canopy_area <dbl>,
#   change_canopy_percentage <dbl>, canopy_percentage_2014 <dbl>,
#   canopy_percentage_2019 <dbl>, change_canopy_absolute <dbl>,
#   mean_temp_morning <dbl>, mean_temp_evening <dbl>, mean_temp <dbl>,
#   mean_heat_index_morning <dbl>, mean_heat_index_evening <dbl>,
#   mean_heat_index <dbl>, geometry <MULTIPOLYGON [US_survey_foot]>

And save it out as an RDS file:

bos_rds <- tempfile(fileext = ".rds")
saveRDS(spatialsample::boston_canopy, bos_rds)

When you unserialize that file, R won’t automatically be able to find all the methods associated with your complicated object. For instance, in a new session, our boston_canopy data doesn’t print nearly as nicely:1

console_output <- tempfile()
callr::r(
  \(bos_rds) {
    readRDS(bos_rds) |>
      head(1) |>
      print()
    NULL
  },
  args = list(bos_rds = bos_rds),
  stdout = console_output
) |>
  invisible()
readLines(console_output)
 [1] "  grid_id land_area canopy_gain canopy_loss canopy_no_change canopy_area_2014"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
 [2] "1    AB-4  795044.8    15323.45    3126.004         53676.05         56802.05"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
 [3] "  canopy_area_2019 change_canopy_area change_canopy_percentage"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
 [4] "1          68999.5           12197.44                  21.4736"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
 [5] "  canopy_percentage_2014 canopy_percentage_2019 change_canopy_absolute"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 [6] "1                7.14451               8.678693               1.534183"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 [7] "  mean_temp_morning mean_temp_evening mean_temp mean_heat_index_morning"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
 [8] "1          75.72113          86.04341  91.51711                76.97335"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
 [9] "  mean_heat_index_evening mean_heat_index"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
[10] "1                90.91202         96.9585"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
[11] "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                geometry"
[12] "1 781922.7, 781441.9, 780424.1, 780361.1, 780386.0, 780385.0, 780385.7, 780386.0, 780386.7, 780388.0, 780389.3, 780390.9, 780392.8, 780394.5, 780394.8, 780394.8, 780394.1, 780393.5, 780391.9, 780391.5, 780393.2, 780393.8, 780394.1, 780395.1, 780394.5, 780414.6, 780415.6, 780416.9, 780429.8, 780430.5, 780430.5, 780519.8, 780522.4, 780522.7, 780523.0, 780523.4, 780523.7, 780524.0, 780524.3, 780524.3, 780524.7, 780525.0, 780525.6, 780526.3, 780527.3, 780528.6, 780529.9, 780531.2, 780532.8, 780534.1, 780535.7, 780537.6, 780539.6, 780541.5, 780543.8, 780546.1, 780548.4, 780550.6, 780552.9, 780555.2, 780557.5, 780559.7, 780562.0, 780564.3, 780566.5, 780568.2, 780570.1, 780571.7, 780573.4, 780575.0, 780576.6, 780577.6, 780578.9, 780579.9, 780580.5, 780581.5, 780582.1, 780582.8, 780584.1, 780585.4, 780586.0, 780587.0, 780587.0, 780586.4, 780586.0, 780585.7, 780585.4, 780584.7, 780584.4, 780584.4, 780583.1, 780582.5, 780582.1, 780582.1, 780582.1, 780582.8, 780583.4, 780584.1, 780584.4, 780585.4, 780586.0, 780586.7, 780587.6, 780588.6, 780589.6, 780590.9, 780591.9, 780594.5, 780596.7, 780599.3, 780601.3, 780602.3, 780664.3, 780665.3, 780665.3, 780669.1, 780676.3, 780683.1, 780705.5, 780713.0, 780713.0, 780712.0, 780751.0, 780750.6, 780750.3, 780764.0, 780803.6, 780809.1, 780809.1, 780809.1, 780810.1, 780811.0, 780812.7, 780814.3, 780816.6, 780817.9, 780819.5, 780821.4, 780822.7, 780824.0, 780825.3, 780827.0, 780828.6, 780829.9, 780831.5, 780842.2, 780846.1, 780858.1, 780856.2, 780855.9, 780856.2, 780856.2, 780856.2, 780856.5, 780856.5, 780856.8, 780857.2, 780857.5, 780857.8, 780858.5, 780858.8, 780859.4, 780859.7, 780860.7, 780861.4, 780862.0, 780863.0, 780864.0, 780864.6, 780865.6, 780866.6, 780867.2, 780868.2, 780869.2, 780870.1, 780871.1, 780871.8, 780872.7, 780873.7, 780874.7, 780875.7, 780876.6, 780877.6, 780878.6, 780879.6, 780880.2, 780881.2, 780881.8, 780882.8, 780883.5, 780884.4, 780885.1, 780885.4, 780886.1, 780886.4, 780887.3, 780888.0, 780890.3, 780892.9, 780894.5, 780895.5, 780944.5, 780945.1, 780980.5, 781028.3, 781076.6, 781123.7, 781155.6, 781169.2, 781167.6, 781163.7, 781166.3, 781165.6, 781186.7, 781186.7, 781210.8, 781211.1, 781306.2, 781340.6, 781341.9, 781373.4, 781399.7, 781431.2, 781449.7, 781463.4, 781483.8, 781483.8, 781485.8, 781488.7, 781490.6, 781491.9, 781492.9, 781493.9, 781494.2, 781495.8, 781496.2, 781495.8, 781496.2, 781496.2, 781497.1, 781497.8, 781498.8, 781499.7, 781500.4, 781500.7, 781501.4, 781501.7, 781502.0, 781502.7, 781503.0, 781504.0, 781505.3, 781506.2, 781507.2, 781509.8, 781510.8, 781512.1, 781512.7, 781513.4, 781514.0, 781514.3, 781514.7, 781514.7, 781515.3, 781515.6, 781516.3, 781516.6, 781517.3, 781517.9, 781519.2, 781520.5, 781521.5, 781522.1, 781523.1, 781523.8, 781524.7, 781525.7, 781526.0, 781526.4, 781527.0, 781527.3, 781529.3, 781534.2, 781539.0, 781543.2, 781544.2, 781545.2, 781545.8, 781546.5, 781547.1, 781547.8, 781548.8, 781549.7, 781551.0, 781552.3, 781553.6, 781554.9, 781555.9, 781557.2, 781557.5, 781558.2, 781558.8, 781559.2, 781559.5, 781560.1, 781560.1, 781560.8, 781564.0, 781566.0, 781567.9, 781570.5, 781571.8, 781572.5, 781572.8, 781573.4, 781574.1, 781574.7, 781575.4, 781576.0, 781576.4, 781577.0, 781577.7, 781579.9, 781582.9, 781585.5, 781588.1, 781591.0, 781593.2, 781595.5, 781597.8, 781598.8, 781599.7, 781602.3, 781608.2, 781614.0, 781617.3, 781620.5, 781626.4, 781629.3, 781632.2, 781636.1, 781639.4, 781642.3, 781644.6, 781646.5, 781648.4, 781650.4, 781651.4, 781652.3, 781653.3, 781654.3, 781656.6, 781658.8, 781664.7, 781670.5, 781677.7, 781682.5, 781684.8, 781687.1, 781689.4, 781690.7, 781691.6, 781692.6, 781693.6, 781695.2, 781696.8, 781698.5, 781700.4, 781702.3, 781703.3, 781704.0, 781704.9, 781705.6, 781706.6, 781708.2, 781710.5, 781712.7, 781714.7, 781715.3, 781716.3, 781717.6, 781718.3, 781718.9, 781719.2, 781719.2, 781717.9, 781717.3, 781717.0, 781716.6, 781716.6, 781716.3, 781716.6, 781717.0, 781717.3, 781718.9, 781719.2, 781719.2, 781719.6, 781720.2, 781720.2, 781719.9, 781719.6, 781719.2, 781718.6, 781717.6, 781717.3, 781717.0, 781717.0, 781717.3, 781717.6, 781717.9, 781718.3, 781718.9, 781720.2, 781720.9, 781720.9, 781720.9, 781721.2, 781721.2, 781721.8, 781722.5, 781723.5, 781724.1, 781724.8, 781725.1, 781725.7, 781726.7, 781728.3, 781729.0, 781729.9, 781731.2, 781732.5, 781736.8, 781743.6, 781743.6, 781743.3, 781780.0, 781831.3, 781904.3, 781922.7, 2965534.6, 2964701.8, 2964701.8, 2964810.9, 2964811.0, 2964821.4, 2964824.0, 2964826.3, 2964828.6, 2964835.7, 2964847.4, 2964859.4, 2964869.8, 2964880.2, 2964885.4, 2964890.6, 2964894.5, 2964898.4, 2964906.5, 2964966.6, 2965059.1, 2965076.0, 2965081.2, 2965132.8, 2965190.0, 2965190.0, 2965144.8, 2965108.1, 2965107.2, 2965094.5, 2965079.6, 2965081.2, 2965079.2, 2965078.6, 2965077.3, 2965076.3, 2965075.0, 2965074.4, 2965073.4, 2965072.1, 2965070.5, 2965068.8, 2965066.9, 2965065.3, 2965063.6, 2965061.7, 2965060.4, 2965058.8, 2965056.8, 2965055.9, 2965054.6, 2965053.6, 2965052.3, 2965051.3, 2965050.3, 2965049.4, 2965049.0, 2965048.4, 2965048.1, 2965047.7, 2965047.7, 2965048.1, 2965048.1, 2965048.4, 2965049.0, 2965049.7, 2965050.0, 2965050.7, 2965051.3, 2965052.0, 2965053.3, 2965054.2, 2965055.5, 2965056.8, 2965058.1, 2965059.4, 2965061.0, 2965062.0, 2965066.9, 2965071.1, 2965075.7, 2965092.2, 2965108.8, 2965133.8, 2965146.1, 2965159.1, 2965164.6, 2965208.8, 2965222.7, 2965236.7, 2965246.8, 2965252.0, 2965256.8, 2965261.1, 2965265.0, 2965269.5, 2965273.1, 2965274.7, 2965276.0, 2965277.0, 2965278.3, 2965279.6, 2965280.5, 2965281.5, 2965282.5, 2965283.5, 2965284.4, 2965285.1, 2965285.7, 2965286.1, 2965286.1, 2965286.1, 2965287.4, 2965287.4, 2965279.6, 2965279.9, 2965279.6, 2965279.9, 2965281.2, 2965327.0, 2965385.4, 2965445.5, 2965445.5, 2965391.3, 2965325.4, 2965324.7, 2965321.8, 2965359.8, 2965363.7, 2965367.9, 2965373.4, 2965378.9, 2965384.5, 2965390.3, 2965395.2, 2965399.1, 2965402.3, 2965405.9, 2965406.5, 2965407.2, 2965408.2, 2965408.5, 2965409.1, 2965409.5, 2965409.8, 2965410.1, 2965409.8, 2965408.8, 2965448.1, 2965449.7, 2965451.0, 2965451.3, 2965452.0, 2965453.0, 2965453.9, 2965454.3, 2965454.9, 2965455.6, 2965456.2, 2965456.9, 2965457.5, 2965458.2, 2965458.5, 2965459.1, 2965459.8, 2965460.1, 2965460.4, 2965460.8, 2965461.1, 2965461.4, 2965461.4, 2965461.4, 2965461.7, 2965461.7, 2965461.4, 2965461.4, 2965461.1, 2965461.1, 2965460.8, 2965460.4, 2965460.1, 2965459.5, 2965459.1, 2965458.5, 2965457.8, 2965456.9, 2965456.2, 2965455.9, 2965454.3, 2965452.3, 2965451.3, 2965450.0, 2965448.4, 2965447.1, 2965445.5, 2965436.4, 2965419.8, 2965392.9, 2965363.7, 2965340.0, 2965318.9, 2965318.5, 2965300.4, 2965301.7, 2965302.3, 2965302.6, 2965303.6, 2965304.6, 2965303.6, 2965376.0, 2965416.6, 2965441.6, 2965741.3, 2965738.1, 2965442.2, 2965442.9, 2965359.1, 2965359.1, 2965488.7, 2965493.2, 2965490.6, 2965487.4, 2965485.1, 2965484.5, 2965483.2, 2965481.9, 2965465.0, 2965463.4, 2965460.8, 2965456.5, 2965452.6, 2965449.1, 2965445.8, 2965443.5, 2965430.9, 2965426.3, 2965424.7, 2965423.1, 2965421.8, 2965419.5, 2965417.2, 2965415.3, 2965413.0, 2965412.7, 2965412.1, 2965411.4, 2965411.1, 2965410.8, 2965410.4, 2965410.4, 2965409.5, 2965409.1, 2965408.8, 2965408.8, 2965408.5, 2965408.5, 2965408.2, 2965407.8, 2965407.8, 2965407.5, 2965407.2, 2965407.2, 2965406.9, 2965406.9, 2965406.2, 2965405.9, 2965405.6, 2965405.2, 2965404.9, 2965404.6, 2965403.9, 2965403.6, 2965403.0, 2965402.0, 2965401.0, 2965399.7, 2965398.4, 2965397.4, 2965396.8, 2965396.1, 2965395.5, 2965393.9, 2965390.6, 2965388.3, 2965386.4, 2965386.1, 2965385.7, 2965385.7, 2965385.7, 2965385.7, 2965385.7, 2965385.4, 2965385.1, 2965384.5, 2965383.8, 2965383.2, 2965382.2, 2965381.2, 2965380.2, 2965379.9, 2965379.3, 2965378.6, 2965377.6, 2965376.3, 2965375.0, 2965373.1, 2965369.8, 2965358.5, 2965352.3, 2965346.1, 2965340.3, 2965338.3, 2965337.7, 2965337.0, 2965336.7, 2965336.1, 2965335.7, 2965335.1, 2965334.8, 2965334.8, 2965334.4, 2965334.1, 2965333.8, 2965334.1, 2965334.1, 2965334.4, 2965334.4, 2965334.1, 2965333.5, 2965332.8, 2965332.5, 2965332.2, 2965331.8, 2965331.8, 2965331.8, 2965331.8, 2965331.5, 2965331.5, 2965331.5, 2965331.5, 2965330.9, 2965330.2, 2965329.3, 2965328.6, 2965327.6, 2965326.0, 2965325.0, 2965324.1, 2965323.4, 2965322.8, 2965322.4, 2965321.8, 2965321.1, 2965320.8, 2965320.5, 2965320.5, 2965321.1, 2965321.5, 2965322.1, 2965322.8, 2965323.1, 2965323.7, 2965324.1, 2965325.0, 2965326.7, 2965328.3, 2965330.2, 2965333.1, 2965335.4, 2965337.4, 2965339.3, 2965341.9, 2965344.8, 2965348.4, 2965353.6, 2965358.8, 2965364.0, 2965369.8, 2965374.4, 2965379.3, 2965384.8, 2965387.4, 2965390.3, 2965394.8, 2965401.3, 2965430.6, 2965435.1, 2965436.1, 2965437.1, 2965438.4, 2965439.7, 2965440.3, 2965441.3, 2965443.5, 2965459.1, 2965462.7, 2965466.3, 2965470.2, 2965473.7, 2965477.3, 2965481.2, 2965484.1, 2965487.7, 2965491.0, 2965494.2, 2965498.1, 2965501.7, 2965505.9, 2965509.8, 2965511.7, 2965513.0, 2965514.0, 2965515.0, 2965518.6, 2965520.8, 2965521.2, 2965521.8, 2965522.8, 2965523.1, 2965523.7, 2965524.7, 2965525.7, 2965526.0, 2965526.7, 2965527.0, 2965527.6, 2965529.3, 2965532.2, 2965533.8, 2965535.4, 2965536.7, 2965539.0, 2965546.8, 2965554.6, 2965566.9, 2965589.3, 2965578.3, 2965563.0, 2965540.3, 2965534.6"

What on Earth is up with that geometry column? That’s unworkable.

Before we dig into this further, let’s write a quick wrapper function that will handle the console_output and readLines() dance we just did for the rest of this post:

run_isolated <- function(..., libpath = .libPaths()) {
  console_output <- tempfile()
  callr::r(
    ...,
    libpath = libpath,
    stdout = console_output
  )
  readLines(console_output)
}

I’ve also added a libpath argument, which will let me control what packages these new R sessions are able to access. By default, R sessions run via run_isolated() will have access to all the libraries installed on my machine:

run_isolated(
  \() print(requireNamespace("sf"))
)
[1] "[1] TRUE"

But because I’ve got all my packages installed into a user library (not the system library), I can use that new libpath argument to make it so these R sessions are entirely isolated, without access to any non-base packages at all:2

run_isolated(
  \() print(requireNamespace("sf")),
  libpath = "/dev/null"
)
[1] "[1] FALSE"

Anyway, back to the point. Complicated objects print ugly, and don’t have any of their other methods available, when you unserialize them in a new session.

The reason for this is pretty straightforward: the print (and other) methods are inside the namespace of the package that created the objects,3 if that namespace isn’t loaded then R can’t find the methods when they’re needed, and unserializing the object doesn’t automatically load the relevant namespace. So the reason that boston_canopy printed nicely in our current session is that spatialsample sneakily loaded sf in the background when we called the boston_canopy object:4

is_sf_loaded <- function() {
  sessionInfo()[c("otherPkgs", "loadedOnly")] |>
    lapply(names) |>
    grepl(pattern = "sf") |>
    any()
}
is_sf_loaded()
[1] TRUE

But when we unserialize the object this doesn’t happen:

callr::r(
  \(bos_rds, is_sf_loaded) {
    readRDS(bos_rds)
    is_sf_loaded()
  },
  args = list(bos_rds = bos_rds, is_sf_loaded = is_sf_loaded)
)
[1] FALSE

Because print.sf() is defined in sf, and sf isn’t loaded, we fall back to the default, ugly print method.

This has broader-reaching impacts. For instance, a number of dplyr functions fail for seemingly nonsensical reasons if they’re used on a unserialized object:

callr::r(
  \(bos_rds) {
    readRDS(bos_rds) |>
      dplyr::arrange() |>
      try()
  },
  args = list(bos_rds = bos_rds)
)
[1] "Error in vec_size() : \n  `x` must be a vector, not a <sfc_MULTIPOLYGON/sfc> object.\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<error/vctrs_error_scalar_type>
Error in `vec_size()`:
! `x` must be a vector, not a <sfc_MULTIPOLYGON/sfc> object.
---
Backtrace:
     ▆
  1. ├─base::tryCatch(...)
  2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
  3. │   ├─base (local) tryCatchOne(...)
  4. │   │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
  5. │   └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
  6. │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
  7. │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
  8. ├─base::withCallingHandlers(...)
  9. ├─base::saveRDS(...)
 10. ├─base::do.call(...)
 11. ├─base (local) `<fn>`(...)
 12. ├─global `<fn>`(bos_rds = base::quote("/tmp/Rtmpb70291/file7571358e23a4.rds"))
 13. │ ├─base::try(dplyr::arrange(readRDS(bos_rds)))
 14. │ │ └─base::tryCatch(...)
 15. │ │   └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 16. │ │     └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 17. │ │       └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 18. │ ├─dplyr::arrange(readRDS(bos_rds))
 19. │ └─dplyr:::arrange.data.frame(readRDS(bos_rds))
 20. │   ├─dplyr::dplyr_row_slice(.data, loc)
 21. │   └─dplyr:::dplyr_row_slice.data.frame(.data, loc)
 22. │     ├─dplyr::dplyr_reconstruct(vec_slice(data, i), data)
 23. │     │ └─dplyr:::dplyr_new_data_frame(data)
 24. │     │   ├─row.names %||% .row_names_info(x, type = 0L)
 25. │     │   └─base::.row_names_info(x, type = 0L)
 26. │     └─vctrs::vec_slice(data, i)
 27. └─vctrs:::stop_scalar_type(`<fn>`(`<s_MULTIP>`), "x", `<fn>`(vec_size()))
 28.   └─vctrs:::stop_vctrs(...)

Just like with printing, the root cause here is that R can’t find the arrange.sf() method when sf isn’t loaded, and winds up using the basic data frame method instead (which then errors out).5

sf:::arrange.sf
function (.data, ..., .dots) 
{
    sf_column_name = attr(.data, "sf_column")
    class(.data) = setdiff(class(.data), "sf")
    st_as_sf(NextMethod(), sf_column_name = sf_column_name)
}
<bytecode: 0x558e252883b0>
<environment: namespace:sf>

The fix is pretty straightforward: load sf (or whatever package has the methods you’re looking for). That’ll let R find your methods, arrange your data, print all pretty, and do everything else you want:6

run_isolated(
  \(bos_rds) {
    library(sf)
    readRDS(bos_rds) |>
      dplyr::arrange() |>
      head(1) |>
      print()
  },
  args = list(bos_rds = bos_rds)
)
 [1] "Simple feature collection with 1 feature and 18 fields"                       
 [2] "Geometry type: MULTIPOLYGON"                                                  
 [3] "Dimension:     XY"                                                            
 [4] "Bounding box:  xmin: 780361.1 ymin: 2964702 xmax: 781922.7 ymax: 2965741"     
 [5] "Projected CRS: NAD83 / Massachusetts Mainland (ftUS)"                         
 [6] "# A tibble: 1 × 19"                                                           
 [7] "  grid_id land_area canopy_gain canopy_loss canopy_no_change canopy_area_2014"
 [8] "  <chr>       <dbl>       <dbl>       <dbl>            <dbl>            <dbl>"
 [9] "1 AB-4      795045.      15323.       3126.           53676.           56802."
[10] "# ℹ 13 more variables: canopy_area_2019 <dbl>, change_canopy_area <dbl>,"     
[11] "#   change_canopy_percentage <dbl>, canopy_percentage_2014 <dbl>,"            
[12] "#   canopy_percentage_2019 <dbl>, change_canopy_absolute <dbl>,"              
[13] "#   mean_temp_morning <dbl>, mean_temp_evening <dbl>, mean_temp <dbl>,"       
[14] "#   mean_heat_index_morning <dbl>, mean_heat_index_evening <dbl>,"            
[15] "#   mean_heat_index <dbl>, geometry <MULTIPOLYGON [US_survey_foot]>"          

This all might sound pretty familiar; I wrote a post about more or less about this a year ago. So why bring it up again?

Well, because I saw a cool trick in patchwork that is currently in the development version of sf that I like a lot better than the one I wrote about last year. It’s possible for these unserialized objects to load their relevant packages themselves, making all their methods available as soon as they exist in your R session. For this example to work on your machine, you’re going to need the development version (at time of writing) of sf:

packageVersion("sf") > "1.0-14"
[1] TRUE

Let’s create a new RDS file, this time using an object that was created by the development version of sf:

nc_rds <- tempfile(fileext = ".rds")
system.file("shape/nc.shp", package = "sf") |>
  sf::st_read() |>
  saveRDS(nc_rds)
Reading layer `nc' from data source 
  `/home/mikemahoney218/R/x86_64-pc-linux-gnu-library/4.3/sf/shape/nc.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
Geodetic CRS:  NAD27

Just like before, let’s unserialize this RDS file and print it to our console:

run_isolated(
  \(nc_rds) {
    readRDS(nc_rds) |>
      head(1) |>
      print()
    invisible(NULL)
  },
  args = list(nc_rds = nc_rds)
)
[1] "Simple feature collection with 1 feature and 14 fields"                        
[2] "Geometry type: MULTIPOLYGON"                                                   
[3] "Dimension:     XY"                                                             
[4] "Bounding box:  xmin: -81.74107 ymin: 36.23436 xmax: -81.23989 ymax: 36.58965"  
[5] "Geodetic CRS:  NAD27"                                                          
[6] "   AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74"
[7] "1 0.114     1.442  1825    1825 Ashe 37009  37009        5  1091     1      10"
[8] "  BIR79 SID79 NWBIR79                       geometry"                          
[9] "1  1364     0      19 MULTIPOLYGON (((-81.47276 3..."                          

Somehow, magically, R found the right print method!

R can now also find the right methods for other functions, like arrange():

callr::r(
  \(nc_rds) {
    readRDS(nc_rds) |>
      dplyr::arrange() |>
      try()
  },
  args = list(nc_rds = nc_rds)
) |>
  head(1)
Simple feature collection with 1 feature and 14 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -81.74107 ymin: 36.23436 xmax: -81.23989 ymax: 36.58965
Geodetic CRS:  NAD27
   AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74
1 0.114     1.442  1825    1825 Ashe 37009  37009        5  1091     1      10
  BIR79 SID79 NWBIR79                       geometry
1  1364     0      19 MULTIPOLYGON (((-81.47276 3...

So what’s the trick? Well, under the hood these new sf objects are quietly loading sf when they get unserialized:

callr::r(
  \(nc_rds, is_sf_loaded) {
    readRDS(nc_rds)
    is_sf_loaded()
  },
  args = list(nc_rds = nc_rds, is_sf_loaded = is_sf_loaded)
)
[1] TRUE

The magic here is that sf objects now have an attribute, .sf_namespace, that’s a simple stub function defined in the sf namespace:

nc_from_rds <- readRDS(nc_rds)
attr(nc_from_rds, ".sf_namespace")
function () 
NULL
<bytecode: 0x558e2d2322d8>
<environment: namespace:sf>

That attribute – which takes up nearly no RAM or disk space – is enough to cause R to automatically load the sf namespace when these objects are unserialized. You now magically have access to all the methods for your complex objects right out of the box.

What if sf isn’t installed? Then it doesn’t get loaded. But that means objects fall back to their default methods, which isn’t great but seems fine:

run_isolated(
  \(nc_rds) {
    readRDS(nc_rds) |>
      head(1) |>
      print()
    invisible(NULL)
  },
  args = list(nc_rds = nc_rds),
  libpath = "/dev/null"
)
[1] "   AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
[2] "1 0.114     1.442  1825    1825 Ashe 37009  37009        5  1091     1      10"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
[3] "  BIR79 SID79 NWBIR79"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
[4] "1  1364     0      19"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
[5] "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               geometry"
[6] "1 -81.47276, -81.54084, -81.56198, -81.63306, -81.74107, -81.69828, -81.70280, -81.67000, -81.34530, -81.34754, -81.32478, -81.31332, -81.26624, -81.26284, -81.24069, -81.23989, -81.26424, -81.32899, -81.36137, -81.36569, -81.35413, -81.36745, -81.40639, -81.41233, -81.43104, -81.45289, -81.47276, 36.23436, 36.27251, 36.27359, 36.34069, 36.39178, 36.47178, 36.51934, 36.58965, 36.57286, 36.53791, 36.51368, 36.48070, 36.43721, 36.40504, 36.37942, 36.36536, 36.35241, 36.36350, 36.35316, 36.33905, 36.29972, 36.27870, 36.28505, 36.26729, 36.26072, 36.23959, 36.23436"

I think this is really cool! It feels like a user-friendly way to make unserialized complex objects work like you’d expect them to, and prevents confusing error chains like the ones above.

Footnotes

  1. Note that we need to use the stdout argument to capture how this object actually prints to R’s console, and to then use readLines() to print the contents of that file into our current session.↩︎

  2. Thanks to Gabor on Mastodon for teaching me that R will always have access to the system library.↩︎

  3. I mean, mostly. Some packages like broom provide a bunch of methods for objects that don’t come from that package. But those packages aren’t automatically loaded on unserialization either (and can you imagine how chaotic a world that would be!), so the basic point stands.↩︎

  4. More on the trick in this old blog post.↩︎

  5. Well, the root root cause is that vctrs can’t calculate the length of sf geometry columns, because they don’t subclass the list class.↩︎

  6. I’m trying to be very precise with my wording here, so let me highlight that this call to library() is actually attaching sf, when all that’s necessary is loading it. For this post this is a distinction without a difference, so I’m not spending a lot of time on it. But only loading the namespace is necessary, you don’t need to attach it to the search path.↩︎