dimanche 23 mai 2021

Access the if statement via base::if() in R

I am coding in R and due to stability purposes when I have to deploy something, I call every function with the syntax package::function(arguments) just to avoid conflicts that as you know may happen when using a lot of packages. It helped me a lot over the years.

I know that if is a reserved word so technically speaking it is impossible (or at least it should be in my knowledge) for someone to define an object and name it if.

I am also aware that it belongs to control flow statement (which I think are a different "thing") and due to the previous consideration I am also aware that the following questions might be useless. My pure technical doubts are:

  1. Why if I embrace it in back-ticks the function class returns "function" as a result?
  2. Why without back-ticks I get an error? and last but most important
  3. Why I am unable to access it via the usual base::if() syntax?

As I said, most likely useless questions but at this point I am curious about the details underneath it.

> class(if)
Error: unexpected ')' in "class(if)"

> class(`if`)
[1] "function"

> base::if(T) T
Error: unexpected 'if' in "base::if"
> if(T) T
[1] TRUE
> base::if(`T`) T
Error: unexpected 'if' in "base::if"

Aucun commentaire:

Enregistrer un commentaire