R 中 scan() 的替代方法(此处文档样式)
An alternative to scan() in R (here-document style)
我正在寻找一种将文本读入向量的方法,这样每一行都是一个不同的元素,所有这些都发生在 R 脚本中。
我发现的一种方法是这样的:
bla <- scan(text = "line1
line2
line3",
what = character())
哪个正确地给了我:
> bla
[1] "line1" "line2" "line3"
但是,存在几个问题。首先,它是缩进的。我不必这样做,但任何自动缩进功能都会将其弹回以进行对齐(我通常使用)。其次,如果我想使用双引号,这需要转义码。
有没有办法在 R 脚本中执行类似于 Here-Document 方法 (<< EOF
) 的操作?
我在 Windows 上使用 RStudio 作为 IDE、运行。最好有一种独立于平台的方式来做到这一点。
编辑
Do you need to have the text inside the R script?
是的。
我想做的一个例子:
R script here
⋮
bla <- <SOMETHING - BEGIN>
line1
line2
line3
<SOMETHING - END>
⋮
more R script here
这里的要求是,我可以自由地打字,而不必担心自动缩进会向前移动行,并且在打字 "
.
时无需担心转义码
如果可以选择编写 Rmarkdown 文档而不是 R 脚本,我们可以使用 knitr cat
引擎
---
title: "Untitled"
output: html_document
---
```{cat engine.opts=list(file='foo')}
line1
line2
"line3"
'line4'
```
```{r}
bla <- readLines("foo")
bla
```
这两个问题都可以用scan
函数和两个小技巧解决,我觉得:
scan(text = '
line1
"line2" uses quotation mark
line3
', what = character(), sep = "\n")
Read 3 items
[1] "line1" "\"line2\" uses quotation mark"
[3] "line3"
当您将引号单独放在一行时,自动缩进不会有问题(使用 RStudio 测试)。如果文本中只有双引号,则可以使用单引号来开始和结束字符对象。如果文本中有单引号,请使用双引号作为字符。如果两者都有,您可能应该使用搜索和替换来使它们统一。
我还添加了sep = "\n"
,所以每一行都是结果字符向量的一个元素。
从 R 4.0 版开始,我们有了原始字符串(参见 ?Quotes
)
bla <- r"(line1
line2
"line3"
'line4'
Here is indentation
Here is a backslash \
)"
#> [1] "line1\nline2\n\"line3\"\n'line4'\n Here is indentation\nHere is a backslash \\n"
请注意,尽管它给出了一个字符串,而不是单独的元素。我们可以用 strsplit
:
拆分它
bla <- strsplit(bla, "\n")[[1]]
#> [1] "line1"
#> [2] "line2"
#> [3] "\"line3\""
#> [4] "'line4'"
#> [5] " Here is indentation"
#> [6] "Here is a backslash \"
我正在寻找一种将文本读入向量的方法,这样每一行都是一个不同的元素,所有这些都发生在 R 脚本中。
我发现的一种方法是这样的:
bla <- scan(text = "line1
line2
line3",
what = character())
哪个正确地给了我:
> bla
[1] "line1" "line2" "line3"
但是,存在几个问题。首先,它是缩进的。我不必这样做,但任何自动缩进功能都会将其弹回以进行对齐(我通常使用)。其次,如果我想使用双引号,这需要转义码。
有没有办法在 R 脚本中执行类似于 Here-Document 方法 (<< EOF
) 的操作?
我在 Windows 上使用 RStudio 作为 IDE、运行。最好有一种独立于平台的方式来做到这一点。
编辑
Do you need to have the text inside the R script?
是的。
我想做的一个例子:
R script here
⋮
bla <- <SOMETHING - BEGIN>
line1
line2
line3
<SOMETHING - END>
⋮
more R script here
这里的要求是,我可以自由地打字,而不必担心自动缩进会向前移动行,并且在打字 "
.
如果可以选择编写 Rmarkdown 文档而不是 R 脚本,我们可以使用 knitr cat
引擎
---
title: "Untitled"
output: html_document
---
```{cat engine.opts=list(file='foo')}
line1
line2
"line3"
'line4'
```
```{r}
bla <- readLines("foo")
bla
```
这两个问题都可以用scan
函数和两个小技巧解决,我觉得:
scan(text = '
line1
"line2" uses quotation mark
line3
', what = character(), sep = "\n")
Read 3 items
[1] "line1" "\"line2\" uses quotation mark"
[3] "line3"
当您将引号单独放在一行时,自动缩进不会有问题(使用 RStudio 测试)。如果文本中只有双引号,则可以使用单引号来开始和结束字符对象。如果文本中有单引号,请使用双引号作为字符。如果两者都有,您可能应该使用搜索和替换来使它们统一。
我还添加了sep = "\n"
,所以每一行都是结果字符向量的一个元素。
从 R 4.0 版开始,我们有了原始字符串(参见 ?Quotes
)
bla <- r"(line1
line2
"line3"
'line4'
Here is indentation
Here is a backslash \
)"
#> [1] "line1\nline2\n\"line3\"\n'line4'\n Here is indentation\nHere is a backslash \\n"
请注意,尽管它给出了一个字符串,而不是单独的元素。我们可以用 strsplit
:
bla <- strsplit(bla, "\n")[[1]]
#> [1] "line1"
#> [2] "line2"
#> [3] "\"line3\""
#> [4] "'line4'"
#> [5] " Here is indentation"
#> [6] "Here is a backslash \"