An alternative to using a batch file is to do it entirely in a Windows shortcut.
For instance, on my Windows system:
Create a shortcut to c:/path/to/bin/Rscript.exe
.
For me, Windows had a "wizard" help me do this, which forced me to just identify the executable. From here, either Alt-Enter on the link or right-click and select Properties, then continue:
Change "Start in" to the appropriate directory. If you are relying on specific paths for dependencies, then make sure you set this (and/or use setwd(...)
within your R script).
- Add the filename to the command line in "Target", perhaps something like
C:\R\R-3.3.3\bin\Rscript.exe myscript.R
. In this example, myscript.R
must exist in the directory in "Start in", though there's nothing stopping you from hard-coding the full path.
Using the link in @neilfws's comment or information here (How can I read command line parameters from an R script?, both Marek's and Dirk's answers), you can easily make it react to run-time arguments. For example, if your script includes:
opts <- commandArgs()
then opts
will be a character
vector with
c("C:\\R\\R-3.3.3\\bin\\x64\\Rterm.exe", "--slave", "--no-restore", "--file=myscript.R", "--args", "C:\\path\\to\\dragged_file")
and your script can "react" to the file (or directory) dragged onto the icon.
One issue with all of this is that it is entirely non-interactive. If you need popups, graphics, or other "discourse" with the user, you'll need something a little different.