RGui(Windows; R版本3.5.3)似乎忽略了出现在字符串中一行开头的制表符(在代码行上按CTRL + R):
# 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.')
当从文件中将上述代码source
保留下来时,制表符被保留。
Tab键输入的所有制表符,或者已经存在于您已在“ R编辑器”中打开的文本文件中,都将
不在使用Ctrl-R进行提交时受到尊重(鉴于选项卡已从答案中剥离出来,因此在此示例中很难表示出来)。
我想'R编辑器'并不打算用于认真的代码编辑,使用专用的IDE(例如RStudio)或更全功能的编辑器(例如Emacs,Notepad ++)可能会更好。您可以通过在RGui中进行编辑时手动将制表符替换为\t
来解决RGui中的此问题,但是如果要在文件中保留实际的制表符,这可能不合适。使用source()
直接运行存储在文本文件中的代码时,选项卡也将得到正确处理。