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

[FR] skipEmptyColumn should keep empty named columns #554

Closed
JanMarvin opened this issue Feb 28, 2023 · 0 comments
Closed

[FR] skipEmptyColumn should keep empty named columns #554

JanMarvin opened this issue Feb 28, 2023 · 0 comments

Comments

@JanMarvin
Copy link
Owner

It was requested that openxlsx2 could keep empty columns with wb_to_df(skipEmptyColumn = TRUE) so that only columns which are NAs are kept. This should be doable, we only have to move the output creating part of wb_to_df() behind the skipEmtpyColumn part.

library(openxlsx2)

# feature request skipEmptyCols skips should keep named empty columns

# removes empty column Species
# dat <- wb_to_df(wb, skipEmptyCols = TRUE)

## initialize empty cells
na_mat <- matrix(NA, nrow = 22, ncol = 7)

## create artificial data with empty column
dat    <- iris[1:20,]
dat$Species <- NA

## create the workbook
wb <- wb_workbook()$
  add_worksheet()$
  add_data(x = na_mat, colNames = FALSE)$
  add_data(x = dat)


# workaround
header <- wb_to_df(wb, rows = 1, colNames = FALSE, skipEmptyCols = TRUE)
dat <- read_xlsx(wb, cols = names(header))

head(dat)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 2          5.1         3.5          1.4         0.2      NA
#> 3          4.9         3.0          1.4         0.2      NA
#> 4          4.7         3.2          1.3         0.2      NA
#> 5          4.6         3.1          1.5         0.2      NA
#> 6          5.0         3.6          1.4         0.2      NA
#> 7          5.4         3.9          1.7         0.4      NA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant