Skip to content

Commit

Permalink
support writing tibbles. closes #581 (#582)
Browse files Browse the repository at this point in the history
* support writing tibbles

* General solution for data.table and tibble suggested by @jmbarbone
  • Loading branch information
JanMarvin committed Apr 11, 2023
1 parent 2361046 commit 5db0a61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

* Add `skipHiddenCols` and `skipHiddenRows` to `wb_to_df()`. In this way, hidden columns and rows are ignored, assuming that the person who has hidden them assumes that they are not important. [579](https://github.com/JanMarvin/openxlsx2/pull/579)

* When writing `tibble` use `to.data.frame()` just like in the `data.table` case. [582](https://github.com/JanMarvin/openxlsx2/pull/582)


***************************************************************************

Expand Down
2 changes: 1 addition & 1 deletion R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ write_data2 <- function(
if (inherits(data, "data.frame") || inherits(data, "matrix")) {
is_data_frame <- TRUE

if (inherits(data, "data.table")) data <- as.data.frame(data)
if (is.data.frame(data)) data <- as.data.frame(data)

sel <- !dc %in% c(4, 5, 10)
data[sel] <- lapply(data[sel], as.character)
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,21 @@ test_that("writing na.strings = NULL works", {
expect_equal(exp, got)

})

test_that("write tibbles", {

tbl <- structure(
list(
a_number = c(1234, 4321),
a_string = c("hello", "world"),
a_date = structure(c(19358, 19448), class = "Date"),
a_boolean = c(FALSE, TRUE)
),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -2L)
)

wb <- wb_workbook() %>%

This comment has been minimized.

Copy link
@JanMarvin

JanMarvin Apr 11, 2023

Author Owner

to quick, wanted something like expect_silent()

wb_add_worksheet() %>%
wb_add_data(x = tbl)
})

0 comments on commit 5db0a61

Please sign in to comment.