Andreas
Andreas

Reputation: 6738

How to source file.R without output

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

Upvotes: 2

Views: 3214

Answers (4)

Richie Cotton
Richie Cotton

Reputation: 121127

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.

Upvotes: 2

William Doane
William Doane

Reputation: 1476

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/

Upvotes: 1

hadley
hadley

Reputation: 103938

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).

Upvotes: 3

PaulHurleyuk
PaulHurleyuk

Reputation: 8169

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

Upvotes: 0

Related Questions