68

Let's assume I have 2 source files, the first one named example1.r and the second one example2.r (given below).

example1.r

plot(1:10,1:10)

example2.r

qplot(1:10,1:10)

When I source example1.r, the graph is drawn. It does not, however, when I source example2.r. What is the solution here?

(qplot in example2.r is ggplot2's function)

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Grega Kešpret
  • 11,827
  • 6
  • 39
  • 44

1 Answers1

80

Update:

  • .R files: source's option print.eval=TRUE will lead to printing behaviour of the evaluation result like in the interactive command line.

source("Script.R", print.eval=TRUE)

  • .Rnw files: knitr by default emulates the behaviour of the interactive command line wrt. printing. Note that knitr can be specified as Sweaving engine also for R package vignettes.


This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).

This is the famous FAQ 7.22: Why do lattice/trellis graphics not work?.

For grid graphics like ggplot2 or lattice, you need to print the graphics object in order to actually draw it.

Interactively on the command line this is done automatically. Everywhere else (inside files to be sourced, loops, functions, Sweave chunks) you need to print it explicitly.

print (qplot (1 : 10, 1 : 10))

Alternatively, you can redefine qplot to do the printing:

qplot <- function (x, y = NULL, z = NULL, ...) {
  p <- ggplot2::qplot (x = x, y = y, z = z, ...)
  print (p)
}

(this changes the axis labels to x and y).

I use this approach in vignettes where I want to write code exactly as a user in an interactive session would type it.

Travis Heeter
  • 13,002
  • 13
  • 87
  • 129
cbeleites unhappy with SX
  • 13,717
  • 5
  • 45
  • 57
  • Minor point: you don't need `invisible(p)` since `print(p)` returns `p`. – Richie Cotton Jul 13 '11 at 13:38
  • Thanks Richie, `print (p)` returns actually `invisible (p)` - which is what I want (if it would be visible, using it from the command line would produced the plots twice). Edited the code accordingly. – cbeleites unhappy with SX Jul 15 '11 at 09:47
  • Thank you for your answer (+1)! I was going nuts trying to figure this out for a couple of hours. I have two additional relevant questions, if you don't mind: 1) Will redefining `qplot()` shown above work nicely with `ggplot2`'s layering functions? 2) Is similar redefinition necessary for the other `ggplot2`'s function, producing output - `ggplot()`? – Aleksandr Blekh May 24 '14 at 12:20
  • 2
    @AleksandrBlekh: Where I have used this approach so far, it worked. But I'm using it less and less as I hardly ever `source` .R files but instead use .Rnw files. I've almost completely changed to using `knitr` instead of `Sweave`, and `knitr` by default emulates the `print` behaviour of the R command line, so the answer is largeyl obsolete for me. – cbeleites unhappy with SX May 24 '14 at 13:00
  • I see. Thank you for the comment! However, I didn't understand your answer in regard to `ggplot` - does it also require redefinition, similarly to the one for `qplot`? – Aleksandr Blekh May 24 '14 at 13:12
  • 2
    Oh, I see. Yes if you use `ggplot ()` instead of `qplot ()` you can and need to apply the same strategy. – cbeleites unhappy with SX May 24 '14 at 13:27
  • I tried to integrate your advice with my project's environment and so far it failed. I created a separate question for this problem: http://stackoverflow.com/questions/23851465/environment-error-when-trying-to-use-redefined-ggplotqplot. Would greatly appreciate, if you could take a look and help! Thank you! – Aleksandr Blekh May 25 '14 at 02:31
  • I have a slightly similar problem, I calling the R script using Rscript but it doesn't seem to work. Here's the question https://stackoverflow.com/questions/59181206/running-rscript-containing-ggplot-from-linux-command-line-through-xterm – Herman Toothrot Dec 05 '19 at 13:03