is there a version of isTRUE()
that would also work on a symbol T
?
To be clear, I don't mean the actual shorthand for TRUE
which is T
but a value of the class symbol
.
This comes from a function that has saved its information in a sys.call()
object - which saves a TRUE
(i.e. a logical value) but when you input a T
and then use sys.call() a symbol
is saved rather than a logical
value.
An example here:
test_fun <- function(x, mc){
out <- list()
out$a <- rnorm(x)
out$sys_call <- sys.call()
out$match_call <- match.call()
return(out)
}
Where then if I want to check the resulting object by accessing the $call$mc
aspect, I don't identify the T
as a logical value.
Here the main issue:
main_example <- test_fun(2, mc = T)
main_example$sys_call$mc # showing T
main_example$match_call$mc # showing T
main_example$sys_call$mc == TRUE # resulting in FALSE
main_example$match_call$mc == TRUE # resulting in FALSE
isTRUE(main_example$sys_call$mc) # resulting in FALSE
isTRUE(main_example$match_call$mc) # resulting in FALSE
str(main_example$sys_call$mc) # showing that T is a symbol, not logical
str(main_example$match_call$mc) # showing that T is a symbol, not logical
I'm well aware that I should never use T
but rather TRUE
but how do I deal with a user inputting T
in my package and always checking whether it's true?
Just using as.logical()
throws an error:
cannot coerce type 'symbol' to vector of type 'logical'
Something like this would work, but it's a bit tedious... There must be a better way to do this :)
is.symbol(main_example$match_call$mc) && main_example$match_call$mc == as.symbol("T")
Thanks!
Some more fun comparing sys.call
and match.call
:
# here a small but unsatisfying advantage of match.call v sys.call
test_fun(2, T)$sys_call$mc
test_fun(2, T)$match_call$mc
# unsatisfying because it still does not recognise it as TRUE but just as symbol T
# but it's better than sys.call
is.logical(test_fun(2, T)$sys_call$mc)
is.logical(test_fun(2, T)$match_call$mc)
test_fun(2, mc = T)$sys_call$mc
test_fun(2, mc = T)$match_call$mc
# both fail to identify logical
is.logical(test_fun(2, mc = T)$sys_call$mc)
is.logical(test_fun(2, mc = T)$match_call$mc)
# here the advantage of match.call v sys.call
test_fun(2, TRUE)$sys_call$mc
test_fun(2, TRUE)$match_call$mc
# here the advantage of match.call v sys.call
is.logical(test_fun(2, TRUE)$sys_call$mc)
is.logical(test_fun(2, TRUE)$match_call$mc)
test_fun(2, mc = TRUE)$sys_call$mc
test_fun(2, mc = TRUE)$match_call$mc
is.logical(test_fun(2, mc = TRUE)$sys_call$mc)
is.logical(test_fun(2, mc = TRUE)$match_call$mc)
isTRUE(eval(test_fun(2, T)$call_2$mc))
match.call()
but not thesys.call()
, right?sys.call()
doesn't use the parameter names, but rather position. Thus you could doisTRUE(eval(test_fun(2, T)$call_2[[3]]))
sys.call
doesn't evaluate its parameter assignment. It does preserve name. In this case look atdput(main_example$sys_call)