I have run into a package that uses the substitute
function to parse parameters in one of its functions.
The function used is more or less as follows:
f <- function(...) {
ref <- as.list(substitute(list(...))[-1])
# for clarity
print(ref)
counter <- 0L
for (i in ref) {
if (as.character(i[1]) == "==") {
counter <- counter + 1L
}
}
return(counter)
}
f(df1$a == df1$b, df1$b == df1$c)
#> [[1]]
#> df1$a == df1$b
#>
#> [[2]]
#> df1$b == df1$c
#> [1] 2
Created on 2019-10-24 by the reprex package (v0.3.0)
I would like to pass df1$a == df1$b, df1$b == df1$c
as a character, but I cannot find a way to do it as I cannot override the substitute
function. So far I have tried a few options but none of them seem to work:
char <- "df1$a == df1$b, df1$b == df1$c"
f(get(char))
#> [[1]]
#> get(char)
#> [1] 0
f(parse(text = char))
#> [[1]]
#> parse(text = char)
#> [1] 0
f(eval(parse(text = char)))
#> [[1]]
#> eval(parse(text = char))
#> [1] 0
Created on 2019-10-24 by the reprex package (v0.3.0)
EDIT:
After @Roland's answer solving my initial question, I'm wondering if there is also an easy way to solve a slightly more general case with a function using another parameter:
f2 <- function(parameter, ...) {
ref <- as.list(substitute(list(...))[-1])
# for clarity
print(ref)
counter <- 0L
for (i in ref) {
if (as.character(i[1]) == "==") {
counter <- counter + 1L
}
}
return(counter)
}