mikemahoney218 / mm218.dev

Code and assets for my personal website at https://mm218.dev
https://mm218.dev
Other
25 stars 14 forks source link

posts/2023-11-27-objects-loading-namespaces/ #24

Open utterances-bot opened 7 months ago

utterances-bot commented 7 months ago

Mike Mahoney - Helping R find the right methods for unserialized complex objects.

Automatically load package namespaces when reading serialized objects with this One Quick Trick.

https://www.mm218.dev/posts/2023-11-27-objects-loading-namespaces/

jromanowska commented 7 months ago

Interesting! But, actually, I was more intrigued by your \ code. E.g., the first chunk:

bos_rds <- tempfile(fileext = ".rds")
callr::r(
  \(bos_rds) saveRDS(spatialsample::boston_canopy, bos_rds),
  args = list(bos_rds = bos_rds)
) |> 
  invisible()

I've never seen such a difficult way to save an RDS file! :-O Teach me - what is this \(bos_rds) doing? And why calling the whole thing through callr::r?!

mikemahoney218 commented 7 months ago

Ha, sorry! Legacy of the writing process. My first draft here was very intent that I was going to keep the primary R session with as few packages loaded as possible -- so I couldn't call spatialsample::boston_canopy in the primary session, because that would load sf. Then I realized I needed to introduce the problem somehow, and so wind up calling spatialsample::boston_canopy in the primary session ~immediately. ~I might change this chunk to use the straightforward approach.~ I immediately changed this (and saving nc_rds) to use the more straightforward approach -- thanks!

(As for "what is \(bos_rds) doing" -- \ is a shorthand for function as of R 4.1, if memory serves, so this defines an in-line function to be run inside the callr R session).