hdkrgr
hdkrgr

Reputation: 1736

Save a tibble with list columns to disk

I would like to save to disk a tibble that has list-columns (for later use inside R only). Ideally I'd like a fast binary format like feather, however, it doesn't seem to support list cols:

test <- tibble(a= list(c(1,2), c(3,4)))
feather::write_feather(test, 'test.csv')

Error in writeFeather(x, path) : Not implemented: a is a list

I was expecting the methods in the readr package to be able to handle this, but none of the ones I've tried seem to be able to.

How do I do this?

Upvotes: 5

Views: 10943

Answers (2)

Paul Hiemstra
Paul Hiemstra

Reputation: 60944

My experience of the tidyverse is that they do not work with columns containing lists. For example, filter from dplyr does not work correctly for lists inside data.frames. So, for the operations that are not supported you are stuck with functions that do support this.

If you are simply looking for a way to store any R object on disk, I would recommend you check out save or saveRDS (and load and readRDS). This serializes R objects to a binary format. Note that this is only useful as storage between R sessions, and is not interoperable with other analysis tools such as Python or SPSS.

Upvotes: 2

Wolfgang
Wolfgang

Reputation: 584

You can use saveRDS and readRDS functions:

library(tibble)
test <- tibble(a= list(c(1,2), c(3,4)))
saveRDS(test, "c:/test.rds")
test_2 <- readRDS("c:/test.rds"))
identical(test, test_2)

In the readr package there are read_rds and write_rds functions, which even allow compression to be set.

Upvotes: 12

Related Questions