1

The RGui (Windows; R version 3.5.3) appears to ignore tab characters that occur at the beginning of a line within a character string (press CTRL+R over the lines of code):

# REPLACE "<TAB>" WITH AN ACTUAL TAB CHARACTER TO GET THE CODE INTENDED BELOW.
foo <- 'LINE1
<TAB>LINE2
<TAB>LINE3
'

foo

# [1] "LINE1\nLINE2\nLINE3\n"

longstring <- removetabsatbeginningoflines('
<TAB>Sometimes I have really long strings that I format
<TAB>so that they read nicely (not with too long of a
<TAB>line length). Tabs at the beginning of the lines
<TAB>within a string preserve my code indenting scheme
<TAB>that I use to make the code more readable. If the
<TAB>tabs are not removed automatically by the parser,
<TAB>then I need to wrap the string in a function that
<TAB>removes them.')

The tab characters are preserved when the above code is source'd from a file.

  1. Why doesn't RGui keep the tab characters?
  2. Where is this behavior documented?
  3. What other non-intuitive, related behaviors does RGui have with regard to parsing (multiline) strings?
Ana Nimbus
  • 635
  • 3
  • 16
  • 2
    Couldn't reproduce it in `R 3.6.0` – akrun Oct 03 '19 at 21:53
  • You can represent a `` with `\t` to make a reproducible example. I can't reproduce the problem with R 3.4.2 or R 3.5.1 either. – thelatemail Oct 03 '19 at 22:24
  • @akrun I am running the code from a file. More specifically, I am using RGui (Windows) and pressing CTRL+R to run the example code. I just tried the analogous thing in RStudio (v1.1.463 with R 3.5.3) (press CTRL + Enter) and got different results (the tabs are preserved). – Ana Nimbus Oct 03 '19 at 22:35
  • @thelatemail I am not so sure about `\t`. The sequence `\t` within a file is backslash followed by `t`. I have the tab character (ASCII decimal code 9). – Ana Nimbus Oct 03 '19 at 22:36
  • @AnaNimbus - not when you're entering it at the R console like you show. `foo <- 'LINE1\n\tLINE2\n\tLINE3'` should represent your data as described. But you are right, the RGui text editor doesn't respect tabs when submitting code. – thelatemail Oct 03 '19 at 22:39
  • @thelatemail No. I hope my further edits to the example code make it more clear what I am talking about, by way of example. Maybe I am not formatting my example properly. This code resides in a file that is being edited during the RGui session. It is not code that is entered _directly_ into the console. – Ana Nimbus Oct 03 '19 at 22:49
  • @AnaNimbus - you might have missed my last sentence. RGui's basic editor doesn't respect tabs if they are just entered with the Tab key. It is an extremely basic text editor with little-to-no-functionality. If you are working on serious code editing, I'd use RStudio or Emacs or Notepad++ or something else to interact with R. – thelatemail Oct 03 '19 at 22:53
  • @thelatemail Thank you. yes, I had missed your last sentence---sorry. Does Emacs behave when submitting code to R? I would expect that process to have the same result as entering directly into the console. The only feature I like about the RGui text editor is the ability to press CTRL + R and run the code that is selected or that is on the current line when no text is selected. RStudio is too constraining for my taste---too many panes, too much "baggage." I do use it for "help", however---that I like---very organized. – Ana Nimbus Oct 03 '19 at 23:19
  • @AnaNimbus - Emacs+ESS does indeed behave and properly represent a tab when submitting to R (once you've entered a tab using https://superuser.com/questions/602510/how-to-insert-tab-character-in-text-mode ) – thelatemail Oct 03 '19 at 23:27

1 Answers1

0

I can't find anywhere that this is documented, so this is a worthwhile question to publicly answer.

If you are using RGui's built-in 'R Editor', then all Tab characters entered via the Tab key, or already existing in a text file that you have opened into the 'R Editor', will not be respected when submitting using Ctrl-R (This is difficult to represent here in an example given that tabs are stripped from answers).

I imagine the 'R Editor' is not meant to be used for serious code editing, and you may be better off using a dedicated IDE (e.g. RStudio) or more full-featured editor (e.g. Emacs, Notepad++).

You can route around this issue in RGui by manually replacing tabs as \t when editing in RGui, but this may not be appropriate if you want to keep actual Tabs in your file. Tabs will also be correctly processed when using source() to directly run the code stored in the text file.

thelatemail
  • 91,185
  • 12
  • 128
  • 188