moj-analytical-services / xltabr

xltabr: An R package for writing formatted cross tabulations (contingency tables) to Excel using openxlsx
https://moj-analytical-services.github.io/xltabr/
32 stars 6 forks source link

Styles do not read in correctly in certain circumstances #88

Closed RobinL closed 7 years ago

RobinL commented 7 years ago

This file doesn't work:

https://github.com/moj-analytical-services/OMSQ_Tables_/blob/b92577d6a58de2739ebeb63773e901f2443a04ce/STYLES.xlsx

Reproduce the problem with this code:

library(xltabr)

path <- system.file("extdata", "synthetic_data.csv", package="xltabr")
xltabr::set_style_path("styles2.xlsx")

df <- read.csv(path, stringsAsFactors = FALSE)
ct <- reshape2::dcast(df, drive + age  ~ type, value.var= "value", margins=c("drive", "age"), fun.aggregate = sum)

ct <- dplyr::arrange(ct, -row_number())

titles = c("Breakdown of car statistics", "Cross tabulation of drive and age against type*")
footers = "*age as of January 2015"
tab <- xltabr::auto_crosstab_to_wb(ct, titles = titles, footers = footers, return_tab = TRUE)

tab$style_catalogue$title
tab$title

openxlsx::openXL(tab$wb)
RobinL commented 7 years ago

https://github.com/moj-analytical-services/xltabr/blob/3c869291f270c0fd58db67d4844d5f38f26273da/R/style_catalogue.R#L60

Because a list can be accessed by number or by index, this overwrites a style if value is a number

So for instance, if a cell anywhere has the number 3 in it, and the 3rd item in the list is title, then the title will be overwritten

RobinL commented 7 years ago

Note the underlying problem was essentialy this

> a <- list()
> a[[1]] <- 100
> a[["1"]] <- 20
> a
[[1]]
[1] 100

$`1`
[1] 20

> a <- list()
> a[["1"]] <- 20
> a[[1]] <- 100
> a
$`1`
[1] 100
RobinL commented 7 years ago

Closed by #89