tags:

views:

316

answers:

4

Is it possible to source a file without printing all the charts etc (already tried with echo=F)?

In my case I call the png("filename%03d.png") device early in the script. It is not to big a hassle to comment this out - but all the charts do take a lot of time to render. (the specific file I am working with now uses base-graphics - but mostly I'll be using ggplot2 - which makes the issue somewhat more important (ggplot2 is excellent, but in the current implementation not the fastest))

Thanks

+1  A: 

It's not the best sounding solution, but If you might be running this script often like this, you could declare a boolean whether graphics are required (graphics_required=TRUE) and then enclose all your plot commands in if/then clauses based on your boolean, then just change the boolean to change the behaviour of the script

PaulHurleyuk
Thank you all who answered this question. I think the boolean approach is what comes closest. Thank you all - and especially PaulHurleyuk
Andreas
+3  A: 

It's not a problem for ggplot2 or lattice graphics - you always have to explicitly print them when they are called in non-interactive settings (like from within a script).

hadley
But if i have a script with lots of print (plot) - is there a way to source the file without the print statements? Maybe the best way to do this would be to have separate scripts for plots and for other data mingling.
Andreas
Yes, define a boolean toggle variable and the decide to print/not to print.
Dirk Eddelbuettel
+1  A: 

Perhaps this might be of some help...

"A package that provides a null graphics device; includes a vignette, "devNull", that documents how to create a new graphics device as an add on package. "

from http://developer.r-project.org/

William Doane
+2  A: 

Good practise for coding R means wrapping as much of your code as possible into functions. (See, e.g., Chapter 5 of the R Inferno, pdf.) If you place your plotting code inside a function, it need not be displayed when you source it. Compare the following.

File foo.r contains

plot(1:10)

When you call source('foo.r'), the plot is shown.

File bar.r contains

bar <- function() plot(1:20)

When you call source('bar.r'), the plot is not shown. You can display it at your convenience by typing bar() at the command prompt.

Richie Cotton
Hi Richie, thank you so much for the link to what Dante would have written had R been around in the days :-)
Andreas