Use wrapper to change default arguments of function


#1

Followup from this question.

It works just fine making a "thin" wrapper, however I'd like to make an actual wrapper and just change the default value of bootstrap_options in case I want to change some of the other arguments. I've never done this before, and I couldn't really find a basic recipe for this with google or on SO.

I tried just making a simple function:

kable_table <- function(kable_input, bootstrap_options, latex_options, 
                        full_width, position, font_size, row_label_position, ...){
                            kableExtra::kable_styling(kable_input, bootstrap_options = c("condensed", "bordered", "striped"),
                                                      latex_options = "basic", full_width = FALSE, position = "center",  
                                                      font_size = NULL, row_label_position = "l", ...)
}

And this works for the new bootstrap_options, but when I change e.g. position nothing happens. I also tried setting position = NULL, but that throws an error: Error in if (position == "center") { : argument is of length zero.

Is there a simple way to just copy a function and change some of the default arguments, or does it require a lot more programming?


#2

Simplifying your code a bit, suppose you had a function

# Your kable_styling
f = function(x, y = 1) x + y

Then you could create

# Note the x = x and y = y bit
g = function(x, y = 10) f(x = x, y = y)

So now g() is effectively f() with different default values

f(1, 1)
# 2
g(1, 1) 
# 2
f(1)
# 2
g(1) 
#[1] 11