zeehio/condformat

theme_grob not working - parameters conflicting?

Closed this issue · 2 comments

I cannot seem to get the theme_grob example below from the reference to work. It doesn't throw errors, but the theme_grob call doesn't seem to affect rendering. When I try to alter the theme, I get various errors (see below).

data(iris)
cf <- condformat(head(iris)) %>%
theme_grob(base_size = 10, base_colour = "red")
## Not run:
print(cf)
## End(Not run)

The theme_caption example works fine - adding My Caption to the table caption.
I have tried a number of different approaches below, spawning different errors. I hope I am missing something obvious, but I can't see a change in font size, colour or family, using the R Studio Viewer, tableGrob, ggsave, base plot, htmlTable, grid.arrange etc.

## simple data frame
data(iris)
df <- head(iris)
## condformat the data frame
cd_df <- condformat(df)

## Approach 1
## convert to grob first
grob_df <- condformat2grob(cd_df, draw = F)
## add theme to grob
grob_thm_df <- theme_grob(grob_df, base_size = 10, base_colour = "red")

# Error in as.data.frame.default(value, stringsAsFactors = FALSE) : 
#   cannot coerce class ‘c("gtable", "gTree", "grob", "gDesc")’ to a data.frame


## Approach 2
## add theme to condformat df before conversion
thm_cd_df <- theme_grob(cd_df,base_size = 10, base_colour = "red")
## convert to grob
grob_thm_df <- condformat2grob(thm_cd_df,draw = F)

# Error in gtable_table(d, name = "core", fg_fun = theme$core$fg_fun, bg_fun = theme$core$bg_fun,  : 
#   unused arguments (base_size = 10, base_colour = "red")


## Approach 3
## try adding fg_params parameter according to gtable_table param requirements
params_thm_df <- theme_grob(cd_df, fg_params=list(fontfamily = "Corbel"))
grob_params <- condformat2grob(params_thm_df, draw=F)

# Error in gtable_table(d, name = "core", fg_fun = theme$core$fg_fun, bg_fun = theme$core$bg_fun,  : 
#   formal argument "fg_params" matched by multiple actual arguments
zeehio commented

Indeed I have to fix the documentation. Here is a reproducible example that works. Hopefully it is good enough for your needs, otherwise please tell me. I'm no grob expert but I'll do my best

library(condformat)

data(iris)
cf <- condformat(head(iris)) %>%
  theme_grob(
    rows = NULL,
    theme = gridExtra::ttheme_default(base_size = 10, base_colour = "red")
  )
condformat2grob(cf)

Created on 2023-08-23 with reprex v2.0.2

zeehio commented

Thank you very much for reporting this. The reproducible example you provided was key to fix the issue with the example.

This issue will be fixed on the next condformat release. The code is fine as it is, but it is hard to use because the example in the help page is wrong.

The website already contains the fixed example.

Thanks again, and keep opening issues if you find them!