3

can anyone explain why if I run this from the command line:

>R #So I'm in the R environment
>plot(cars)
>sessionInfo()

The plot is produced as expected and the output from sessionInfo is:

> sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C  

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base  

However, when I put the two lines of code into a script called Test.R that looks like this:

#!/usr/bin/Rscript
plot(cars)
sessionInfo()

And run "Rscript Test.r"

No plot is produced and the output from session info is:

R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  base   

The two sessionInfo's look the same, I am logged in to my terminal with the "-X" parameter, and typing "xclock" produces a picture of a clock and I've used Rscript before to produce plots. I've also added in a line "require(methods)" as I read here that might help. If someone could provide assistance on what I could do to pinpoint the problem I'd appreciate it.

Edit 1: Response to Amit's Comment below: I'm actually using unix on a Mac, I think your method is for windows, so I edited the command slightly to what I think it should be, but let me know if I've done it wrong. I did this:

$ /usr/bin/Rscript --vanilla --slave Test2.r 
Loading required package: methods
R version 3.2.3 (2015-12-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu precise (12.04.5 LTS)

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] methods   stats     graphics  grDevices utils     datasets  base 
Community
  • 1
  • 1
user1288515
  • 195
  • 1
  • 10
  • Rscript can be wonky. what happens if you go to cmd and write: `"C:\Program Files\R\R-3.2.2\bin\x64\R.exe" --vanilla --slave CMD BATCH "D:\PATH\FILE.R"` (obviously changing the R path to the version you're using and the path/file to yours? – Amit Kohli Nov 17 '16 at 09:40
  • I've included the output as a comment above, I apologise, I got an error saying my reply post (i.e. the code) was too long to paste here. – user1288515 Nov 17 '16 at 09:52
  • you're still using Rscript... use `usr/bin/R` and then specify your path to `Test2.r`. and are you sure it's `.r` not `.R`? – Amit Kohli Nov 17 '16 at 10:00
  • Thanks so much, so If i type /usr/bin/R /path/to/Test2.r ; I just get: ARGUMENT 'path/to/Test2.R' __ignored__ and then the R console/environment starts up? Then I changed the extension from ".r" to ".R" and the same thing happens? – user1288515 Nov 17 '16 at 10:10
  • `"/usr/bin/R" --vanilla --slave CMD BATCH "/path/to/Test2.r"` – Amit Kohli Nov 17 '16 at 10:20
  • Oh sorry I thought that the CMD BATCH was related to windows. When i run that exact command, it makes a file called Test2.r.Rout and in that is the line: Fatal error: cannot open file '/path/to/Test2.r': no such file or dir. (then, to check, I just typed "Rscript /path/to/Test2.r" and it does print the sessionInfo for that script as I ask it to). I also added in a line of code "cars" to print the "cars" output, it prints if I do "Rscript /path/to/Test2.r" (but the plot still doesn't work) , but I still get the same error using your method and it's not printing. – user1288515 Nov 17 '16 at 10:32
  • Update, when I change plot(cars) to print(plot(cars)) (like someone suggested here (http://stackoverflow.com/questions/26643852/ggplot-plots-in-scripts-do-not-display-in-rstudio) ) everything "runs" as in there's no errors, except that I get "NULL" as the output to print(plot(cars)). – user1288515 Nov 17 '16 at 16:55
  • 5
    Dear @user1288515 you are simply mistaken in assuming that the_scripting_ variant would open a (graphical) plotting device in the same way the _interactive_ environment does. It doesn't, and that is documented. You _can_ print to `pdf()` or `png()` or ... a number of other devices by _explicitly_ opening them as you do in your answer. No more, no less, and works as _documented and expected_. – Dirk Eddelbuettel Nov 17 '16 at 17:05
  • 1
    I had the same issue. [This](https://stackoverflow.com/a/42545005/4999991) solved my problem. – Foad S. Farimani May 10 '18 at 01:20

1 Answers1

0

I can get the plot to print if I print straight to PDF like this:

#!/bin/Rscript
require(methods)
pdf("cars.pdf")
plot(cars)
dev.off()
user1288515
  • 195
  • 1
  • 10