4

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)

5
  • 3
    You can always evaluate the symbol. ie isTRUE(eval(test_fun(2, T)$call_2$mc))
    – Onyambu
    Sep 17, 2021 at 20:40
  • Right, so this would work on the match.call() but not the sys.call(), right? Sep 17, 2021 at 20:42
  • 4
    sys.call() doesn't use the parameter names, but rather position. Thus you could do isTRUE(eval(test_fun(2, T)$call_2[[3]]))
    – Onyambu
    Sep 17, 2021 at 20:44
  • Right, I understand!! Thanks a bunch Onyambu!! Sep 17, 2021 at 20:48
  • @Onyambu : I think it might be more accurate to say that sys.call doesn't evaluate its parameter assignment. It does preserve name. In this case look at dput(main_example$sys_call)
    – IRTFM
    Sep 17, 2021 at 23:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.