[英]R system call returning stdin without escape characters
編輯:這個問題被深思熟慮。 我的問題實際上是簡單地嘗試通過閃亮顯示文本-不存儲它。 有一天,你只是想不清楚。
我有一個python腳本,可以將一些結果打印到stdin中,我想將其讀入R中的字符向量。通常使用system(...,intern=TRUE)
可以很好地工作,但是在這種情況下,它對我不起作用當添加轉義字符時(腳本返回HTML,添加轉義字符可能會導致HTML格式錯誤)。 我可以通過將python的輸出保存到一個臨時文件中並將該文件讀取到R中來解決此問題,但是如果有一個我想不到的簡單解決方法,我寧願避免這種情況。 這是我的意思的示例:
> #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>"
您的代碼運行正常,R只是將轉義的字符打印為轉義符。 如果你這樣做
cat(paste(system("./f.py", intern=TRUE), collapse=""))
您應該看到所需的輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.