I want to a function where I throw any given other function with multiple parameters that use both single and double quotes.
For example:
just_params("some_function(param1="this",param2='that')")
or
just_params(some_function(param1="this",param2='that'))
which results in the function not being called but two new string variables param1 and param2 being declared in the environment. Either options works but I think #1 would be easier.
Throwing this all in a string directly is tough because of a mix of double and single quotes. I am hoping to use this while debugging other people's code and am assuming that they are using double and single quotes willy-nilly.
Yes, I know I can just copy and delete the commas but sometimes I am debugging function calls with 20 parameters (also cringe, I know) but that just how it is and I don't want to go in and copy and paste and delete commas each time.
Here's one way to do it:
just_params <- function(call) {
# Get the expression that was in the call without evaluating it.
call <- substitute(call)
# Expand all arguments to their full names. This needs the
# called function to exist and be visible to our caller.
call <- match.call(get(call[[1]], mode = "function",
envir = parent.frame()),
call)
# Change the name of the function to "list" to return the arguments
call[[1]] <- `list`
# Evaluate it in the parent frame
eval(call, envir = parent.frame())
}
some_function <- function(param1, param2, longname) {}
just_params(some_function(param1 = "this", param2 = 'that', long = 1 + 1))
#> $param1
#> [1] "this"
#>
#> $param2
#> [1] "that"
#>
#> $longname
#> [1] 2
Created on 2023-09-03 with reprex v2.0.2
Notes:
long
instead of longname
)1 + 1
but returned 2
.