Home > Back-end >  openxlsx, openxlsx_setOp("headerStyle)
openxlsx, openxlsx_setOp("headerStyle)

Time:08-19

A question on package openxlsx in R :

I tried everything to change the default option for "headerStyle" with function openxlsx_setOp () but I couldn't.

In order to not define headerStyle in writeData() everytime.

For example :

openxlsx_getOp("headerStyle")
NULL

style <- createStyle(borderColour = "grey")

openxlsx_setOp("headerStyle", style)

openxlsx_getOp("headerStyle")
NULL

Thanks in advance.

CodePudding user response:

This could be a bug. IMHO the issue is that openxlsx_setOp under the hood converts the value argument to a list using as.list. As a workaround you could set the headerStyle via options like so:

library(openxlsx)

openxlsx_getOp("headerStyle")
#> NULL

style <- createStyle(borderColour = "grey")

options(openxlsx.headerStyle = createStyle(borderColour = "grey"))

openxlsx_getOp("headerStyle")
#> A custom cell style. 
#> 
#>  Cell formatting: GENERAL 
#> 

Simply replacing as.list by list seems to fix the issue (at least for this case):

library(openxlsx)

openxlsx_getOp("headerStyle")
#> NULL

style <- createStyle(borderColour = "grey")

openxlsx_setOp2 <- function (x, value) {
  if (is.list(x)) {
    if (is.null(names(x))) {
      stop("x cannot be an unnamed list", call. = FALSE)
    }
    return(invisible(mapply(openxlsx_setOp, x = names(x), 
                            value = x)))
  }
  
  value <- list(value)
  names(value) <- openxlsx:::check_openxlsx_op(x)
  
  options(value)
}

openxlsx_setOp2("headerStyle", createStyle(borderColour = "grey"))
openxlsx_getOp("headerStyle")
#> A custom cell style. 
#> 
#>  Cell formatting: GENERAL 
#> 
  • Related