ropensci / readODS

Read ODS (OpenDocument Spreadsheet) into R as data frame. Also support writing data frame into ODS file.
https://docs.ropensci.org/readODS/
Other
55 stars 22 forks source link

[FeatureRequest] Writing multiple sheets into the same file #56

Closed mmahmoudian closed 1 year ago

mmahmoudian commented 4 years ago

It would be nice to also write into multiple sheets of the same .ods file. The function can:

  1. Accept a list of dataframes
  2. In case the list has names, create sheets with names otherwise follow the convention of LibreOffice/OpenOffice naming
  3. write the dataframes into the corresponding sheets
ivan-paleo commented 2 years ago

It is now possible to append sheets in v1.7, but it requires one call to write_ods() per sheet. Packages like writexl and openxlsx have the possibility to write a list of data.frames to a single XLSX file, where each data.frame will be written to a sheet.

For example: https://www.rdocumentation.org/packages/writexl/versions/1.4.0/topics/write_xlsx "To create an xlsx with (multiple) named sheets, simply set x to a named list of data frames."

That would be great for write_ods() too!

mmahmoudian commented 2 years ago

@ivan-paleo I think that is nothing that a simple lapply can't solve.

I didn't know about the development about this issue. If it is now possible, I suggest closing this issue.

ivan-paleo commented 2 years ago

Yes, of course, there are workarounds. But a straightforward (and common) way of doing it would be great too. It's not necessary, but that's why it's a feature request :)

chainsawriot commented 1 year ago

@mmahmoudian Just to clarify, you want it to work like writexl::write_xlsx(), right?

excel_files <- writexl::write_xlsx(list("some_car_data" = mtcars, "some_flower_data" = iris))
readxl::excel_sheets(excel_files)
#> [1] "some_car_data"    "some_flower_data"
readxl::read_excel(excel_files, sheet = "some_car_data")
#> # A tibble: 32 × 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows
readxl::read_excel(excel_files, sheet = "some_flower_data")
#> # A tibble: 150 × 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <chr>  
#>  1          5.1         3.5          1.4         0.2 setosa 
#>  2          4.9         3            1.4         0.2 setosa 
#>  3          4.7         3.2          1.3         0.2 setosa 
#>  4          4.6         3.1          1.5         0.2 setosa 
#>  5          5           3.6          1.4         0.2 setosa 
#>  6          5.4         3.9          1.7         0.4 setosa 
#>  7          4.6         3.4          1.4         0.3 setosa 
#>  8          5           3.4          1.5         0.2 setosa 
#>  9          4.4         2.9          1.4         0.2 setosa 
#> 10          4.9         3.1          1.5         0.1 setosa 
#> # ℹ 140 more rows

Created on 2023-08-22 with reprex v2.0.2

mmahmoudian commented 1 year ago

@chainsawriot well, I don't write into Microsoft formats, so... 🤷 But based on the code snippet you provided, yes something like that. I don't care much about the syntax but rather the functionality.

chainsawriot commented 1 year ago

@mmahmoudian @ivan-paleo I just wanted to let you know that this feature is now implemented.

require(readODS)
#> Loading required package: readODS
temp_ods <- tempfile(fileext = ".ods")
write_ods(list("flower_data" = iris, "car_data" = mtcars), temp_ods)
list_ods_sheets(temp_ods)
#> [1] "flower_data" "car_data"
read_ods(temp_ods, sheet = "car_data")
#> # A tibble: 32 × 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

Created on 2023-08-24 with reprex v2.0.2

ivan-paleo commented 1 year ago

@chainsawriot that's great news, thank you!

In which version is it available: the CRAN version or only in the current development version on GitHub? From what I've understood, the CRAN version is v2.0.0 so I guess it's only the latest GitHub release (v2.1), right?

chainsawriot commented 1 year ago

@ivan-paleo Yes, GitHub (or r-universe).

install.packages("readODS", repos = "https://ropensci.r-universe.dev")
remotes::install_github("ropensci/readODS")

Another CRAN release will be in a few weeks.