Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mmahmoudian opened this issue Apr 16, 2020 · 8 comments
Closed

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

mmahmoudian opened this issue Apr 16, 2020 · 8 comments
Labels

Comments

@mmahmoudian
Copy link
Contributor

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
Copy link

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 mmahmoudian changed the title [FeatureRequest] Writing into multiple sheets of the same file [FeatureRequest] Writing into multiple sheets in the same file Mar 23, 2022
@mmahmoudian
Copy link
Contributor Author

@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
Copy link

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
Copy link
Collaborator

@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

@chainsawriot chainsawriot changed the title [FeatureRequest] Writing into multiple sheets in the same file [FeatureRequest] Writing multiple sheets into the same file Aug 22, 2023
@mmahmoudian
Copy link
Contributor Author

mmahmoudian commented Aug 22, 2023

@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 chainsawriot mentioned this issue Aug 22, 2023
4 tasks
chainsawriot added a commit that referenced this issue Aug 23, 2023
So that the list thing (#56) is a for loop
chainsawriot added a commit that referenced this issue Aug 23, 2023
* Use a similar style to pbrohan

Except camelCase...

* Improve header hygiene

* Refactor and created `write_df` [no ci]

So that the list thing (#56) is a for loop
chainsawriot added a commit that referenced this issue Aug 24, 2023
Also reduce cyclomatic complexity
@chainsawriot
Copy link
Collaborator

@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
Copy link

@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
Copy link
Collaborator

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants