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

[write] do not try to apply a cell style to data frames with zero rows. fixes #985 #987

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -5,6 +5,12 @@
* Helper to read sensitivity labels from files and apply them to workbooks.
[983](https://github.com/JanMarvin/openxlsx2/pull/983)

## Fixes

* Allow writing data frames with zero rows. [987](https://github.com/JanMarvin/openxlsx2/pull/987)


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

# openxlsx2 1.5

2 changes: 2 additions & 0 deletions R/write.R
Original file line number Diff line number Diff line change
@@ -277,6 +277,8 @@ write_data2 <- function(
dc <- c(c("_rowNames_" = openxlsx2_celltype[["character"]]), dc)
}

if (nrow(data) == 0) applyCellStyle <- FALSE

# add colnames
if (colNames) {
# its quicker to convert data to character and append the colnames
4 changes: 4 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ JOHAB
Kaku
LastModifiedBy
Libreoffice
MIP
OEM
ORCID
Openoffice
@@ -34,6 +35,7 @@ RGBA
RedToBlack
RowStripe
RowSubheading
Rprofile
SHIFTJIS
STDEV
STDEVP
@@ -159,6 +161,7 @@ mediumDashDot
mediumDashed
mediumGray
minAxisType
mips
mmm
mmss
mschart
@@ -203,6 +206,7 @@ queryTables
reimported
rels
rgb
richData
rightToLeft
rowColHeaders
rowNames
27 changes: 27 additions & 0 deletions tests/testthat/test-write.R
Original file line number Diff line number Diff line change
@@ -959,3 +959,30 @@ test_that("dims size warnings work", {
)

})

test_that("writing zero row data frames works", {

# write an empty data frame
dat <- data.frame()
expect_silent(wb <- wb_workbook()$add_worksheet()$add_data(x = dat))

exp <- NULL
expect_message(got <- wb_to_df(wb), "sheet found, but contains no data")
expect_equal(exp, got)

# write a data frame containing an empty date vector
dat <- data.frame(date = base::as.Date(NULL))
expect_silent(wb <- wb_workbook()$add_worksheet()$add_data(x = dat))

exp <- "date"
got <- names(wb_to_df(wb))
expect_equal(exp, got)

# try the same with write_xlsx()
expect_silent(wb <- write_xlsx(x = dat))

exp <- "date"
got <- names(wb_to_df(wb))
expect_equal(exp, got)

})