[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't. IntroductionI am working on finishing up a few things with my new R package {tidyAML} before I release it to CRAN. One of those things is the ability of a user to build a model using a command that might be something like generate_model(). One of the things that is necessary to do is to match the function arguments from the generate_model() to the actual parsnip call.

This is where and argument matcher of sorts may come in handy. I am doing this because it will take one most step of abstraction away, and instead of say calling linear_reg() or mars() or something like that, you can just instead use generate_model() and type in your engine or the parsnip function call there.

Now I am not one hundred percent certain that I’ll actually implement this or not, but the exercise was fun enough that I decided to share it. So let’s get into it.

FunctionHere is the current state of the function.

argument\_matcher <- function(.f = "linear\_reg", .args = list()){ # TidyEval ---- fns <- as.character(.f) fns\_args <- formalArgs(fns) fns\_args\_list <- as.list(fns\_args) names(fns\_args\_list) <- fns\_args arg\_list <- .args arg\_list\_names <- unique(names(arg\_list)) l <- list(arg\_list, fns\_args\_list) arg\_idx <- which(arg\_list\_names %in% fns\_args\_list) bad\_arg\_idx <- which(!arg\_list\_names %in% fns\_args\_list) bad\_args <- arg\_list[bad\_arg\_idx] bad\_arg\_names <- unique(names(bad\_args)) final\_args <- arg\_list[arg\_idx] # Return ---- if (length(bad\_arg\_names > 0)){ rlang::inform( message = paste0("bad arguments passed: ", bad\_arg\_names), use\_cli\_format = TRUE ) } return(final\_args)} When working with R functions, it’s not uncommon to encounter a situation where you need to pass arguments to another function. This can be especially challenging when the arguments are not properly matched. Fortunately, the argument_matcher function provides an elegant solution to this problem.

The argument_matcher function takes two arguments: .f and .args. The .f argument is a string that specifies the name of the function you want to pass arguments to, while the .args argument is a list that contains the arguments you want to pass to the specified function.

The argument_matcher function first uses the formalArgs function to extract the formal arguments of the specified function and store them in fns_args. The names of the formal arguments are then used to create a list, fns_args_list.

Next, the function extracts the names of the arguments in .args and stores them in arg_list_names. It then checks if the names of the arguments in .args match the names of the formal arguments of the specified function, and stores the matching arguments in final_args. Any arguments that don’t match the formal arguments are stored in bad_args, and a warning message is printed indicating that bad arguments were passed.

The final step is to return the final_args list, which contains only the arguments that match the formal arguments of the specified function.

In conclusion, the argument_matcher function is a useful tool for ensuring that arguments are properly matched when passed to another function. Whether you’re working with linear regression models or any other type of function, the argument_matcher function will help you select the right arguments and avoid common errors.

ExampleLet’s see a simple example.

suppressPackageStartupMessages(library(tidymodels))argument\_matcher( .args = list( mode = "regression", engine = "lm", cost = 0.5, trees = 1, mtry = 1 ) )

bad arguments passed: costbad arguments passed: treesbad arguments passed: mtry

$mode[1] "regression"$engine[1] "lm" Voila!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.


R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.


Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.Continue reading: The Argument Matcher: A Function for Selecting the Right Arguments {tidyAML}