antecessor
antecessor

Reputation: 2800

Importing all sheet of a ODS file in R keeping the name of the sheets

I have this LibreOffice Calc (.ods) file (download).

I would like to import all the sheets of this document. In the import I will create a list, where each element of the list would be one sheet. And I would like the name of the list elements to keep the original name of sheets in the ODS file.

I created this code:

  library(readODS)
  library(dplyr)
  library(purrr)
  library(tidyr)

  path <- "example.ods"

  mylist <- path %>%
    ods_sheets() %>%
    set_names() %>%
    map(read_ods, skip = 1, path = path)

By running this code I get an error with the set_names() function:

Error in set_names(.) : 1 argument passed to 'names<-' which requires 2

How can I solve it to create a list with elements named as the original sheets?

Upvotes: 0

Views: 1018

Answers (1)

duckmayr
duckmayr

Reputation: 16940

Since I am loath to download a random file from Google Drive, I set up my own example ODS file for this, with two sheets, "foo" and "bar", that look like this:

foo

a b
1 2
3 4

bar

a b
7 6
5 4

So, you had a few issues with your code. You tripped on your first one, that you need two arguments to set_names(). Your other issue here is that you weren't specifying both .x and .f for map(). We can solve these issues by using an intermediate assignment step like so:

library(readODS)
library(dplyr)
library(purrr)
library(tidyr)

path <- "test.ods"

sheets <- ods_sheets(path)
mylist <- map(seq_along(sheets), read_ods, path = path) %>%
    set_names(sheets)
mylist

$foo
  a b
1 1 2
2 3 4

$bar
  a b
1 7 6
2 5 4

Upvotes: 1

Related Questions