0

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>"
6
  • Where is paste() imported from (or is it your own function?) Commented Jun 6, 2013 at 20:28
  • paste is just base R (base::paste) - it's only to each line into a single character vector (without it you would get a vector of length 4 split by line breaks) Commented Jun 6, 2013 at 20:30
  • I was reading this as a question about using R in Python not the other way around. My bad :) Commented Jun 6, 2013 at 20:32
  • Ah, that makes sense. Please let me know if you think it's confusing to read and I will do my best to remove the ambiguity. Commented Jun 6, 2013 at 20:33
  • The ambiguity was all on my side. There is a Python command called system() (from os import system would let you invoke it like that though commonly it is invoked like os.system()) that does the same thing (though with different arguments) and a module called paste though with no functionality like R's paste. Your first sentence is clear. I don't know R but could it be representing tab characters as \t and not actually populating the string with `\` and `t`? Commented Jun 6, 2013 at 20:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I spoke too soon. That does not actually work for my purposes. I need to store a literal character string that reads what is printed when when cat() is applied to the result.
You mean instead of "\t", you want to see " " (8 spaces), but without using cat? That's pretty weird. Can you provide more background?
I edit the original post, but I was basically just having a dumb day. My issue clearly isn't with storing the data it's pumping it through a shiny interface to display correctly. I"ll just accept your answer since my question doesn't really make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.