【发布时间】:2020-02-02 06:12:19
【问题描述】:
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'd 时,制表符将被保留。
- 为什么 RGui 不保留制表符?
- 这种行为记录在哪里?
- RGui 在解析(多行)字符串方面还有哪些其他非直观的相关行为?
【问题讨论】:
-
无法在
R 3.6.0复制它 -
您可以用
\t表示<TAB>以制作可重现的示例。我也无法重现 R 3.4.2 或 R 3.5.1 的问题。 -
@akrun 我正在从文件中运行代码。更具体地说,我使用 RGui (Windows) 并按 CTRL+R 来运行示例代码。我刚刚在 RStudio(v1.1.463 和 R 3.5.3)中尝试了类似的东西(按 CTRL + Enter)并得到了不同的结果(标签被保留)。
-
@thelatemail 我不太确定
\t。文件中的序列\t是反斜杠,后跟t。我有制表符(ASCII 十进制代码 9)。 -
@AnaNimbus - 当你像你展示的那样在 R 控制台输入它时不是这样。
foo <- 'LINE1\n\tLINE2\n\tLINE3'应该代表您描述的数据。但你是对的,RGui 文本编辑器在提交代码时不尊重标签。
标签: r string parsing multiline rgui