字符转义

基本类型 / 字符串

# \t tab, \n newline, \r carriage return
# \" double quote, \' single quote, \\ backslash

escaped <- "She said \"Hello!\" to me."
# escaped = She said "Hello!" to me.

# 换用另一种引号:无需转义
other_quotes <- 'She said "Hello!" to me.'
apostrophe <- "It's fine"

# 通过十六进制代码和 Unicode 码点
capital_a <- c("\x41", "\u0041")
# capital_a = A A

# \\ 表示一个反斜杠
path <- "C:\\Temp\\new"
# path = C:\Temp\new

# 添加引号:FALSE 则得到普通引号
quoted <- dQuote("Hello!", FALSE)
# quoted = "Hello!"

# 用于 shell 命令
shell_text <- shQuote("my file")
# shell_text = 'my file'

cat("escaped =", escaped, "\n")
cat("other_quotes =", other_quotes, "\n")
cat("apostrophe =", apostrophe, "\n")
cat("capital_a =", capital_a, "\n")
cat("path =", path, "\n")
cat("quoted =", quoted, "\n")
cat("shell_text =", shell_text, "\n")

# print() 会再次显示转义
print(path)