简体   繁体   中英

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). 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. 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. If you do

cat(paste(system("./f.py", intern=TRUE), collapse=""))

you should see your desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM