How to run R scripts from the Windows command line (CMD)

R is great for data transformation, modeling, etc. automatization. In my case, I was collecting data and using library RDCOMClient to send summary information to colleges with Microsoft Outlook, but I needed it to do that automatically. It is possible to run R scripts with the help of the Windows command line and later run them automatically with Windows Task Scheduler, but there are a few things you should know.

There are two ways to do that.

The first possible solution that you put in the batch file looks like this.

"C:\Program Files\R\R-3.4.3\bin\Rscript.exe" C:\Users\myusername\Documents\R\Send_Outlook_Email.R

The second one looks like this.

"C:\Program Files\R\R-3.4.3\bin\R.exe" CMD BATCH C:\Users\myusername\Documents\R\Send_Outlook_Email.R

Remember to use quotation marks when there is space in the file path.

Here is how it goes step by step.

1. Find the path to R.exe or Rscript.exe on your computer. If you try to run R.exe from the command line, you enter an R terminal.

In Rscript.exe case you can see additional usage options.

2. Find the path to R file.

3. Open Notepad and combine paths together (with quotation marks if needed and additional commands “CMD BATCH” if you choose to go with R.exe).

"C:\Program Files\R\R-3.4.3\bin\R.exe" CMD BATCH C:\Users\myusername\Documents\R\Send_Outlook_Email.R

4. Save as file with extension .bat (for example send_email.bat).

5. Run that batch file to execute R script.

5 comments on “How to run R scripts from the Windows command line (CMD)

  1. Subhadip Datta

    How to hide the CMD window in the case of CMD BATCH?

  2. Thank you! I actually used this and helped a lot.

  3. It appears that libraries are not started when running R by a batch file – the command “library(corrplot)” does not seem to be run. Are you aware of that? Do you know how to solve that?

    • Janis Sturis

      If the command “library(corrplot)” is at the beginning of the script, usually everything is running smoothly.

      There might be 2 problems:
      1. You need a specific R bit version for that package. To run 64bit R use …bin\i386\Rscript.exe
      I guess I should mention that in this post (I will add that later).

      2. Your library might be installed in a specific location. In that case, you should specify that in your script. For example “.libPaths(“C:/MySpecificLocation/R”)”. It is also possible to add that in your RProfile:

      If none of that helps, please let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *