Skip to content

[newbie] separating plot output from debug output

3 messages · ilai, Tom Roche

#
I'm attempting to refactor an R script that does a lot of plotting,
among other things. Ideally I'd like to do something like

setup     # does pdf(...)
for each part of input {
  plot(process(part))
}
cleanup   # does dev.off()

but have problems:

1 I'm plotting to PDF, so everytime I dev.off() creates a new file,
  and I want everything in one file (as does my boss :-)

2 I'm doing the work on a cluster, where I very much do not have root,
  and which has a fairly minimal set of installed packages, so I can't
  just call something external like 'pdftk' to merge the PDFs as I go.

3 As part of the processing, I printf status and debug messages, which
  I don't want in my PDF(s).

The solutions I can imagine are

1 Append to a single PDF, but I understand this is not feasible, no?

2 Create a buncha PDFs with code above, download them to my laptop,
  merge them to a single PDF, upload it. Feasible but annoying and
  kludgey.

3 Separate processing from plotting, e.g.,

setup     # but not pdf(...)
for each part of input {
  write(process1(part), intermediate)
}
pdf(...)
for each part of intermediate {
  plot(process2(part))
}
cleanup   # does dev.off()

  Again, feasible but kludgey.

4 No status and debug messages. I hope to be that good someday :-)

Am I missing something? Are there clean solutions to this problem?

TIA, Tom Roche <Tom_Roche at pobox.com>
#
If you don't dev.off(), all plots will be sent to the open graphical
device. That usually doesn't impact behavior of other output types:

pdf(file='fooout.pdf')
hist(x <- rnorm(100))
y <- sin(x)
print(str(y))
cat(y,file='fooout.txt')
plot(x,y)
dev.off()

Hope this helps
On Wed, Feb 15, 2012 at 3:43 PM, Tom Roche <Tom_Roche at pobox.com> wrote:
#
Tom Roche Wed, 15 Feb 2012 17:43:05 -0500
ilai Wed, 15 Feb 2012 16:34:34 -0700
Doh! I totally missed that, and was having PDF problems for other
reasons. And thanks for the self-contained example (slightly extended
by me):

pdf.reader -> "xpdf"
pdf(file='fooout.pdf')
hist(x <- rnorm(100))
y <- sin(x)
print(str(y))
cat(y,file='fooout.txt')
plot(x,y)
dev.off()
system(paste(pdf.reader, 'fooout.pdf'))
system('cat fooout.txt')

thanks again, Tom Roche <Tom_Roche at pobox.com>