How to Change the Default Arguments for a Function

I have this function called xlkabular:

xlkabular <- function(...) knitr::kable(escape = FALSE, booktabs = TRUE, xltabular = TRUE,
  linesep = "\\addlinespace", ...) |>
  kableExtra::kable_styling(latex_options = "repeat_header")

I set the default linesep to \\addlinespace, but if the user wants to change it like this:

xlkabular(df, linesep = "\\hline", caption = "Table")

It gives an error because the linesep argument is given twice. How do I make it so that it sets the default to \\adddlinespace but the user can change it if they want?

Does this work for you? I don't have the packages to check it.

xlkabular <- function(DF, Mylinesep = "\\addlinespace", ...) knitr::kable(DF, escape = FALSE, booktabs = TRUE, xltabular = TRUE,
                                        linesep = Mylinesep, ...) |>
  kableExtra::kable_styling(latex_options = "repeat_header")

or this

xlkabular <- function(DF, linesep = "\\addlinespace", ...) knitr::kable(DF, escape = FALSE, booktabs = TRUE, xltabular = TRUE,
                                        linesep = linesep, ...) |>
  kableExtra::kable_styling(latex_options = "repeat_header")
2 Likes

Yep, that was it. Thanks.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.