[英]Deparse and (un)escape quotes
I have the following list:我有以下清单:
lst <- list(a = "", b = 2)
and want to deparse it to text:并想将其解析为文本:
> deparse(lst, width.cutoff = 100)
[1] "structure(list(a = \"\", b = \"\"), .Names = c(\"a\", \"b\"))"
and then i want to run the text as code (in the console):然后我想将文本作为代码运行(在控制台中):
> structure(list(a = \"\", b = \"\"), .Names = c(\"a\", \"b\"))
Error: unexpected input in "structure(list(a = \"
I know it Fails, because of the backslashes: (without them it works obviously).我知道它失败了,因为反斜杠:(没有它们,它显然有效)。
structure(list(a = "", b = ""), .Names = c("a", "b"))
Question:题:
How can i avoid creating the backslashes?如何避免创建反斜杠?
What i tried:我试过的:
I went through the given Parameter.我通过了给定的参数。 Backtick seemed like a candidate, but didnt work.
反引号看起来像一个候选人,但没有奏效。 Using gsub to replace them is a candidate, but way to dirty i guess,..
使用 gsub 来替换它们是一个候选者,但我猜想弄脏了,..
Tools:工具:
R 3.4.3 Rstudio 1.1.447 R 3.4.3 工作室 1.1.447
Update: Upgrade to 3.6 - similar issue.更新:升级到 3.6 - 类似问题。
> lst <- list(a = "", b = 2)
> deparse(lst, width.cutoff = 100)
[1] "list(a = \"\", b = 2)"
> list(a = \"\", b = 2)
Error: unexpected input in "list(a = \"
There are couple of ways to modify the behavior.有几种方法可以修改行为。 One way is to print with
cat
on the console, copy and paste that (used R 3.6.0
)一种方法是在控制台上使用
cat
打印,复制并粘贴(使用R 3.6.0
)
out <- deparse(lst, width.cutoff = 100)
cat(out, sep="\n")
#list(a = "", b = 2)
list(a = "", b = 2)
#$a
#[1] ""
#$b
#[1] 2
Note that cat
also have a file
argument if the output needs to be written to some file and used it later请注意,如果需要将输出写入某个文件并稍后使用,则
cat
也有一个file
参数
Or use gsub
to remove the "
and replace with single quotes ( '
)或使用
gsub
删除"
并替换为单引号 ( '
)
out1 <- gsub('"', "'", out)
out1
#[1] "list(a = '', b = 2)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.