0

I am trying to create a function that outputs a kbl object. I would like to be able to pass arguments in the function call for both kbl() and kable_styling() to fine-tune the table as required for a specific dataset. I can get it to work using ... for one of the functions but not both.

library(kableExtra)
table_fun <- function(x, format_f  = "latex", ...) {
  kbl(x,
      digits = 2,
      align = NULL, # default numeric columns are right-aligned, and other columns are left-aligned
      format.args = list(decimal.mark  = '.', big.mark  = " "),
      booktabs = TRUE,
      format = format_f,
      escape = FALSE,
      ...) %>% # this does not allow for any additional arguments to be passed
  kable_styling(latex_options  = c("hold_position"),
      full_width  = F,
      protect_latex = TRUE, 
      ...) 
}
table_fun(iris, 
      format_f = "html", # for this example
      caption = "This is a table", # argument in kbl(); error thrown
      font_size = 20) # argument in kable_styling()

>> Error in kable_styling(., latex_options = c("hold_position"), full_width = F,  : 
  unused argument (caption = "caption")

So I change the font size in kable_styling(), but I can't change the caption in kbl(). How would I go about fixing this function? Thanks!

Rachel
  • 109
  • 8
  • You are passing `...` to both `kbl()` and `kable_styling()` but only the former actually has a parameter with that name. When you use `...` everything is passed through. It doesn't check if the function will know what to do with it. You would need to move `caption=` to it's own parameter rather than letting it to fall into `...` so it can be passed to only one of the functions. – MrFlick Aug 28 '23 at 18:22
  • Maybe check out this existing question if you really want to try to split up the `...` yourself: https://stackoverflow.com/questions/4124900/is-there-a-way-to-use-two-statements-in-a-function-in-r – MrFlick Aug 28 '23 at 18:23

0 Answers0