trinker / plotflow

A group of tools to speed up work flow associated with plotting tasks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

merge_PDF skips first plot unless code is run line-by-line

eipi10 opened this issue · comments

I just tried out plotflow and ran into a strange problem. Here's a reproducible example:

library(plotflow)
library(ggplot2)

dat = data.frame(x=1:10, y=rnorm(10))
p1=ggplot(dat, aes(x,y)) + geom_line()
p2=ggplot(dat, aes(x,y)) + geom_line()
p3=ggplot(dat, aes(x,y)) + geom_line()

## Plot heights, widths
widths = c(9, 8, 10)
heights = c(5, 11, 6)

merge_pdf(length(widths), file = "test.pdf", 
          widths = widths,
          heights = heights)

plot(p1)
plot(p2)
plot(p3)

If I run the code line by line (by typing Command - Return (I'm on a Mac) over and over again on my keyboard), it works fine. But if I run the code all at once (for example, by selecting Run Region - Run All in the RStudio Code menu), the first plot "slot" (Enter plot 1:) somehow fails to actually input the first plot and moves to Enter plot 2:. This results in an invalid page count error. I've pasted in the output below.

Any ideas on what's causing this or how to fix it? I'll post to Stack Overflow if this isn't already a known problem.

> merge_pdf(length(widths), file = "test.pdf", 
+           widths = widths,
+           heights = heights)
Enter plot 1:

Enter plot 2:
plot(p1)
Enter plot 3:
plot(p2)
   **** Warning:  Invalid Page count.
   No pages will be processed (FirstPage > LastPage).
   **** Warning:  Invalid Page count.

   **** This file had errors that were repaired or ignored.
   **** The file was produced by: 
   **** >>>> R 3.1.3 <<<<
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.

It's interactive so after you callmerge_pdf it wants a plot input. You input a blank space, hence the error. Try it like this (identical to yours but with a blank line removed):

library(plotflow)
library(ggplot2)

dat = data.frame(x=1:10, y=rnorm(10))
p1=ggplot(dat, aes(x,y)) + geom_line()
p2=ggplot(dat, aes(x,y)) + geom_line()
p3=ggplot(dat, aes(x,y)) + geom_line()

## Plot heights, widths
widths = c(9, 8, 10)
heights = c(5, 11, 6)

merge_pdf(length(widths), file = "test.pdf", 
          widths = widths,
          heights = heights)
plot(p1)
plot(p2)
plot(p3)

Thanks Tyler!