1

I understand that R highlights specific text depending on its function, but what does it mean when the variable you are assigning is highlighted? And can it cause any problems? For example, T is highlighted automatically in:

T highlighted

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Liz
  • 35
  • 5

2 Answers2

4

It is better not to use T in R as a variable. The reason T is highlighted is that it is used as an abbreviation for TRUE. If you use T as a variable, you can no longer use it as TRUE and its definition will be overwritten.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Reza
  • 1,945
  • 1
  • 9
  • 17
  • 3
    No, `t` transposes a matrix. `T` is a variable name which by default is assigned to `TRUE` but can be overwritten. Similarly `F` is a synonym for `FALSE` – Allan Cameron Jul 19 '20 at 19:03
  • You could also link to [this blog post](https://www.r-bloggers.com/r-tip-avoid-using-t-and-f-as-synonyms-for-true-and-false/) for an example of why it is best to avoid using `T` and `F` as synonyms for TRUE/FALSE (and by extension avoid using T as a variable name as well ...) – Ben Bolker Jul 19 '20 at 19:12
  • people are fast here, I fixed it a min after I posted, then I saw the comment, I didn't pay attention to question enough – Reza Jul 19 '20 at 19:25
2

You've been told not to use T as a variable name but you've not yet been given an answer to your question. It's not really an issue of R syntax, but rather a "feature" of whatever IDE you are using. Most IDEs are syntax aware and will therefore highlight in blue items that are meaningful inside the R language. If you run R from a Terminal session you would not see the highlighting.

If you need that feature you can get a package: highlighting

Description: Syntax highlighter for R code based on the results of the R parser. Rendering in HTML and latex markup. Custom Sweave driver performing syntax highlighting of R code chunks.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • 1
    good information (although "The reason T is highlighted is that it is used as an abbreviation for TRUE" is, technically, an answer to the question ...) – Ben Bolker Jul 19 '20 at 21:37
  • 1
    the `highlighting` package will give you syntax highlighting **in Sweave/LaTeX output**, not in the terminal. Apparently `colorout` would work ... https://stackoverflow.com/questions/14355369/r-syntax-highlighting-in-terminal – Ben Bolker Jul 19 '20 at 21:48