[英]R system call returning stdin without escape characters
EDIT: This question was poorly thought out. 编辑:这个问题被深思熟虑。 My issue is actually with simply trying to display the text via shiny - not storing it.
我的问题实际上是简单地尝试通过闪亮显示文本-不存储它。 Somedays you just don't think clearly.
有一天,你只是想不清楚。
I have a python script that prints some results to stdin that I would like to read into a character vector in R. This generally works great using system(...,intern=TRUE)
, however in this case it does not work for me when escape characters are added on (the script returns HTML and adding escape characters can lead to malformed HTML). 我有一个python脚本,可以将一些结果打印到stdin中,我想将其读入R中的字符向量。通常使用
system(...,intern=TRUE)
可以很好地工作,但是在这种情况下,它对我不起作用当添加转义字符时(脚本返回HTML,添加转义字符可能会导致HTML格式错误)。 I can get around this by saving the output from python into a temporary file and reading that file into R, but I'd rather avoid that if there is an easy fix that I can't think of. 我可以通过将python的输出保存到一个临时文件中并将该文件读取到R中来解决此问题,但是如果有一个我想不到的简单解决方法,我宁愿避免这种情况。 Here is an example of what I mean:
这是我的意思的示例:
> #f.py is a text file containing
>
> # #!/usr/bin/python
> #
> # html = """
> # <HTML>
> # <p>some content</p>
> # <p> some more content </p>
> # </HTML>"""
> #
> # print html
>
> #the /t, among other escapes, break the html
> v1 <- paste(system("./f.py",intern=TRUE),collapse="")
> v1
[1] "\t\t<HTML>\t\t\t<p>some content</p>\t\t \t<p> some more content </p>\t\t</HTML>"
>
> #this is what I want... but it needs to be saved into an object
> system("./f.py")
<HTML>
<p>some content</p>
<p> some more content </p>
</HTML>
> #or equivalently
> cat(v1)
<HTML> <p>some content</p> <p> some more content </p> </HTML>
>
> #I thought capture.output() would work, but the string still has the escaped characters
> v2 <- capture.output(cat(v1))
> v2
[1] "\t\t<HTML>\t\t\t<p>some content</p>\t\t \t<p> some more content </p>\t\t</HTML>"
Your code is working fine, R is just printing your escaped characters as escapes. 您的代码运行正常,R只是将转义的字符打印为转义符。 If you do
如果你这样做
cat(paste(system("./f.py", intern=TRUE), collapse=""))
you should see your desired output. 您应该看到所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.