【发布时间】:2018-05-18 07:08:52
【问题描述】:
我正在尝试删除生成的“\”,所以我运行 cat() 来清除它。但是需要将该输出分配给一个变量,以便稍后在 gsubfn 字符串中匹配。
>topheader <-'<div id="editor1" class="shinytinymce shiny-bound-input"
style="resize: none; width: 100%; height: 100%; border-style: none;
background: gainsboro;">'
>topheader
[1] "<div id=\"editor1\" class=\"shinytinymce shiny-bound-input\"
style=\"resize: none; width: 100%; height: 100%; border-style: none;
background: gainsboro;\">"
>cat(topheader)
[1] <div id="editor1" class="shinytinymce shiny-bound-input" style="resize:
none; width: 100%; height: 100%; border-style: none; background:
gainsboro;">
> test<-cat(topheader)
> test
NULL
【问题讨论】:
-
你不能。
cat总是返回NULL。它严格用于打印到控制台/文件。这些反斜杠只是为了逃避内部引号。它们实际上并不存在。 -
@RichScriven 实际上您可以使用
capture.output()分配来自cat的输出。然而,这在这里无济于事,因为真正的问题是其他的......(见下一条评论)。 -
@DannyRamirez 你有一个基本的误解。 \ 字符实际上并不存在于字符串中 - 当您将输出打印到控制台时,
print只是以这种方式显示它们。 -
试试
nchar('\"editor1\"')。它输出 9 而不是 11。 -
如果你使用
capture.output,你会遇到同样的问题。