0

Is it possible to generate PDFs/PNGs of graphs directly from the command line using R?

I attempted this with the command:

Rscript -e 'pdf(\'Graph.pdf\');plot(c(1,2,3,4,5));dev.off()'

which executes, but no file is generated.

Matthew Lueder
  • 953
  • 2
  • 12
  • 25
  • 1
    Your use of single-quotes and double-quotes is partly to blame: `Rscript.exe -e "pdf(file='Graph.pdf');plot(c(1,2,3,4,5));dev.off();"` works for me (as @DirkEddelbuettel just showed with his answer). – r2evans Sep 13 '16 at 00:39
  • Switching from single quotes to double quotes on the outside worked! thanks – Matthew Lueder Sep 13 '16 at 01:20

1 Answers1

1

Works here:

edd@max:~$ mkdir /tmp/demo
edd@max:~$ cd /tmp/demo
edd@max:/tmp/demo$ Rscript -e 'pdf("foo.pdf"); \
                               plot(cumsum(rnorm(100)), type="l"); \
                               dev.off()'
null device 
          1 
edd@max:/tmp/demo$ ls -l
total 8
-rw-rw-r-- 1 edd edd 5132 Sep 12 19:36 foo.pdf
edd@max:/tmp/demo$ 

The chart (converted to png for inclusion here) is below.

enter image description here

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725