tags:

views:

373

answers:

4

Is there a way to have an R Device (postscript would be great) write the output into a variable instead of a file?

For example I know this:

postscript(file="|cat")
plot(1:10)
dev.off()

Will send the postscript text to STDOUT. How can I get that text into a variable within R?

+1  A: 

postscript takes a command argument, hence postscript(file="",command="|cat")

Jeff
@Jeff, it's actually postscript(file="",command="cat") (not there is no pipe), but that does the same thing I describe above, pipe it to STDOUT. I'd like that content to be written to a variable within the R session.
Mark
Sorry bout that. I shouldn't only pay half attention here.
Jeff
+1  A: 

Why on earth would you want to do that? R is not a very good system for manipulating Postscript files. If nothing else, you can use tempfile() to write the image to a file, which you can then read in using standard file functions. If you wanted to be fancy, you could perhaps use fifo() pipes, but I doubt it'll be much faster. But I suspect you'd be better off with a different approach.

Harlan
@Harlan, the reason is somewhat convoluted. I'm using rpy2 to interact with R in a python web application. I'd like to take advantage of a plotting function written in R, pass the data back with rpy2 and then stream this to the browser. Just curious if I could do the whole routine without resorting to temp files.
Mark
OK, in that case, I'm pretty sure you want to use a fifo() connection between the R process and the python process. If fifos are new to you, you may want to pick up a book on POSIX programming.
Harlan
Another thought -- you seem to be on a unix-alike. UNIX file systems are, if I recall, extremely efficient assuming they're set up with reasonably large RAM caches. The OS (and its derivatives) was designed to create and use temp files extensively. It may in fact be simplest and easiest to just use temp files. Try it. I'd be curious to know if the temp file were in fact the slowest link in the chain. I doubt it.
Harlan
A: 

You should be able to use a textConnection as follows.

tc <- textConnection("string", "w")

postscript(tc)
plot(1:10)
dev.off()

But string remains blank - maybe a bug?

hadley
No can do. postscript takes a character vector defining either a file name or a file name format. No connections. This is the motivating reason I came up with the R Connections patch: to stream graphics to a connection.
Jeff
@Jeff, if it won't stream to any connection, does this also mean Harlan's fifos above are a no go?
Mark
A: 

I've had success in getting the Binary of a plot into an R variable as a string. Its got some read/write overhead. In the snippet below, R saves the plot as a temp file and reads it back in.

## create a plot
x <- rnorm(100,0,1)
hist(x, col="light blue")

## save plot as temp file
png(filename="temp.png", width=500, height=500)
print(p)
dev.off()

## read temp file as a binary string
plot_binary <- paste(readBin("temp.png", what="raw", n=1e6), collapse="")

Maybe this is helpful to you.

stotastic