从包含单引号和双引号的字符创建字符串?

有没有办法从一些包含单字'和双字的文本创建一个字符串"引号引号字符的?

例子

假设我们有一些包含单引号和双引号的文本(假设实际用例会更长):

Here's a message: "hello, world!"

将其包裹在', 或"不起作用:

thing <- "Here's a message: "hello, world!""
Error: unexpected symbol in "thing <- "Here's a message: "hello"

thing <- 'Here's a message: "hello, world!"'
Error: unexpected symbol in "thing <- 'Here's"

我可以想到两种可能有效的方法。1. 将文本转储到 RStudio(或文本编辑器)中并查找和替换。2. 粘贴到文本文件中并读取带有readLines(). 我只是想看看有没有更简单的方法

回答

如果您有 4.0.0 或更高版本的 R,则可以使用r"(all of your text here)".

text <- r"(My text "has" double and 'single' quotes)"

> cat(text)
My text "has" double and 'single' quotes

可以通过运行找到这方面的文档help("Quotes")


以上是从包含单引号和双引号的字符创建字符串?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>