# NOT RUN { fil <- tempfile("women", fileext = ".rds") ## save a single object to file saveRDS(women, fil) ## restore it under a different name women2 <- readRDS(fil) identical(women, women2) ## or examine the object via a connection, which will be opened as needed. con <- gzfile(fil) readRDS(con) close(con) ## Less convenient ways to restore the object ## which demonstrate compatibility with unserialize() con <- gzfile(fil, "rb") identical(unserialize(con), women) close(con) con <- gzfile(fil, "rb") wm <- readBin(con, "raw", n = 1e4) # size is a guess close(con) identical(unserialize(wm), women) ## Format compatibility with serialize(): fil2 <- tempfile("women") con <- file(fil2, "w") serialize(women, con) # ASCII, uncompressed close(con) identical(women, readRDS(fil2)) fil3 <- tempfile("women") con <- bzfile(fil3, "w") serialize(women, con) # binary, bzip2-compressed close(con) identical(women, readRDS(fil3)) unlink(c(fil, fil2, fil3)) # }
Run the code above in your browser using DataCamp Workspace