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

[wb_add_data] unable to writetibble logicals #581

Closed
pbchase opened this issue Apr 11, 2023 · 2 comments
Closed

[wb_add_data] unable to writetibble logicals #581

pbchase opened this issue Apr 11, 2023 · 2 comments
Labels
enhancement 😀 New feature or request Third party 📦 Using or impacting external library or software

Comments

@pbchase
Copy link

pbchase commented Apr 11, 2023

openxlsx2:: wb_add_data_table() in openxlsx2 v0.6 consistently produces the error 'list' object cannot be coerced to type 'logical' when I ask it to add a dataframe with a logical column to a worksheet in a workbook object. Here is my example:

library(openxlsx2)
library(tidyverse)

df <- tribble(
  ~a_number, ~a_string, ~a_date, ~a_boolean,
  1234, "hello", "2023-01-01", FALSE,
  4321, "world", "2023-04-01", TRUE
)

# works
wb_workbook() %>%
  wb_add_worksheet() %>%
  wb_add_data_table(x = df %>% mutate(a_boolean = as.character(a_boolean)))

# fails
wb_workbook() %>%
  wb_add_worksheet() %>%
  wb_add_data_table(x = df)
#> Error in write_data2(wb = wb, sheet = sheet, data = x, name = name, colNames = colNames, : 'list' object cannot be coerced to type 'logical'

Created on 2023-04-11 with reprex v2.0.2

It does not complain about numerics, dates, or strings, but it chokes on logicals.

@pbchase pbchase changed the title openxlsx2:: wb_add_data_table() will not accept logical columns in the x dataframe openxlsx2::wb_add_data_table() will not accept logical columns in the x dataframe Apr 11, 2023
@JanMarvin
Copy link
Owner

This appears to be some third party issue. Not sure what the tibble logical is, but it is not our issue. Maybe you can have a look @jmbarbone , I do not really do tidyverse stuff. Maybe we need some if (inherits("tbl", x)) x <- as.data.frame(x) if we want to tibble?

library(openxlsx2)
library(tibble)

df <- data.frame(
  a_number = c(1234, 4321),
  a_string = c("hello", "world"),
  a_date = as.Date(c("2023-01-01", "2023-04-01")),
  a_boolean = c(FALSE, TRUE)
)

wb <- wb_workbook() %>%
  wb_add_worksheet() %>%
  wb_add_data_table(x = df)

# same as in the example above. a_date is type character.
tbl <- tibble::tribble(
  ~a_number, ~a_string, ~a_date, ~a_boolean,
  1234, "hello", "2023-01-01", FALSE,
  4321, "world", "2023-04-01", TRUE
)

wb <- wb_workbook() %>%
  wb_add_worksheet() %>%
  wb_add_data_table(x = as.data.frame(tbl))

@JanMarvin JanMarvin added Third party 📦 Using or impacting external library or software enhancement 😀 New feature or request labels Apr 11, 2023
@JanMarvin JanMarvin changed the title openxlsx2::wb_add_data_table() will not accept logical columns in the x dataframe [wb_add_data] unable to writetibble logicals Apr 11, 2023
@JanMarvin
Copy link
Owner

A possible solution is in #582.
Thanks for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 😀 New feature or request Third party 📦 Using or impacting external library or software
Projects
None yet
Development

No branches or pull requests

2 participants