1

I am trying to schedule an R code execution. I was referrig Scheduling R Script and I am creating the following batch file:

C:\R\R-2.10.1\bin\Rcmd.exe BATCH D:\mydocuments\mycode.R

However, when I run the .bat file, a black command window shows up for a second and nothing else is happening. I was also trying to use Rscript.exe instead of Rcmd.exe, but that did not help.

Have you got any suggestions?

Here is the code:

r <- matrix(rnorm(100,1,1), ncol=10, nrow=10)
write.csv(r, file = "D:/aa/ttt.csv", row.names = F)

What I get after running the .bat file is the workspace and .Rout file, but this contains only a log of the code that was ran.

enter image description here

Community
  • 1
  • 1
marco11
  • 235
  • 2
  • 8
  • 1
    See http://www.statmethods.net/interface/batch.html – amwill04 Jan 18 '16 at 21:21
  • @amwill04 I have also tried that. – marco11 Jan 18 '16 at 21:22
  • 1
    What do you expect to happen? "black command window shows up for a second" should be exactly what you should be expecting in this case. – Alexander Radev Jan 18 '16 at 21:34
  • @AlexanderRadev So that, where does the output go? – marco11 Jan 18 '16 at 21:41
  • @AlexanderRadev For example, my code should create some files, but it is not doing that, so that I concluded it is not ran properly. – marco11 Jan 18 '16 at 21:46
  • 1
    Post the R code. We can't know where your output would be if we can't see the script. – RLH Jan 18 '16 at 21:50
  • 1
    Can you append 'pause' on the line after the call to R so that the window does not disappear and post a screenshot? – Alexander Radev Jan 18 '16 at 22:03
  • @AlexanderRadev Picture added to the original post. – marco11 Jan 18 '16 at 22:07
  • The main problem is that the .csv file is not being generated; output is in the saved workspace though. – marco11 Jan 18 '16 at 22:08
  • 1
    Can you try substituting 'write.csv(r, file = "D:/aa/ttt.csv", row.names = F)' with 'write.csv(r, file = file.path("D:", "aa" , "ttt.csv"), row.names = F)' or if the bat file is located in D:\aa - simply use ' 'write.csv(r, file = "ttt.csv", row.names = F)'? – Alexander Radev Jan 18 '16 at 22:18
  • @AlexanderRadev It works, thank you! Do you know what can be a reason for that or most important - when could this problem occur again? Only in the case of exporting files? – marco11 Jan 18 '16 at 22:26
  • File path should always be created with the R function file.path in order to avoid such bugs. Will promote my comment to an answer :) – Alexander Radev Jan 18 '16 at 22:37
  • I think your path should have been separated with double backslash characters not forward slashes. e.g. `D:\\aa\\ttt.csv` . I don't have R to test with but a little research showed that syntax. – RLH Jan 18 '16 at 23:08

1 Answers1

2

Try substituting write.csv(r, file = "D:/aa/ttt.csv", row.names = F) with write.csv(r, file = file.path("D:", "aa" , "ttt.csv"), row.names = F).

If the bat file is located in D:\aa - simply use write.csv(r, file = "ttt.csv", row.names = F)

Alexander Radev
  • 652
  • 5
  • 11