Home > Enterprise >  When writing a DateTime value using openxlsx, a "x" is written followed by DateTime value
When writing a DateTime value using openxlsx, a "x" is written followed by DateTime value

Time:05-26

I would like to write DateTime values to an excel sheet using openxlsx. When I try to do this, instead of just the DateTime value, I get a lowercase "x" on one row followed by the DateTime in the subsequent row. This occurs whether I use write.xlsx or writeData. I also tried converting the DateTime using as.POSIXlt or as.POSIXct, converting the date with timezone specified or not, and get the same result.

The UTC DateTime values are coming from a PerkinElmer microplate reader file.

Below is a code snippet that gives me this result. Any advice or help is appreciated, Thanks!

library(openxlsx)
library(lubridate)

date <- as_datetime("2022-04-07T22:15:08 0000", tz = "America/Los_Angeles")
options(openxlsx.datetimeFormat = "yyyy-mm-dd hh:mm:ss")

write.xlsx(date,"test.xlsx",overwrite = TRUE)

CodePudding user response:

The documentation of write.xlsx says in section Arguments that x is (my emphasis)

A data.frame or a (named) list of objects that can be handled by writeData() or writeDataTable() to write to file.

So apparently an atomic vector is first coerced to data.frame and since the data argument name is x, so is its column header.

This also happens when writing a named list date_list <- list(date = date). A workbook with a sheet named date is created and the data in it has a column header x.

  • Related