Standalone R scripts are not executed by the program R
(R.exe
under Windows), but by a program called Rscript
(Rscript.exe
), which is included in your R
installation by default.
To hint at this fact, standalone R scripts start with a special line called Shebang line, which holds the following content: #!/usr/bin/env Rscript
. Under Windows, an additional measure is needed, which is detailled later.
The following simple standalone R script saves a histogram under the file name "hist.png" from numbers it receives as input:
#!/usr/bin/env Rscript
# User message (\n = end the line)
cat("Input numbers, separated by space:\n")
# Read user input as one string (n=1 -> Read only one line)
input <- readLines(file('stdin'), n=1)
# Split the string at each space (\\s == any space)
input <- strsplit(input, "\\s")[[1]]
# convert the obtained vector of strings to numbers
input <- as.numeric(input)
# Open the output picture file
png("hist.png",width=400, height=300)
# Draw the histogram
hist(input)
# Close the output file
dev.off()
You can see several key elements of a standalone R script. In the first line, you see the Shebang line. Followed by that, cat("....\n")
is used to print a message to the user. Use file("stdin")
whenever you want to specify "User input on console" as a data origin. This can be used instead of a file name in several data reading functions (scan
, read.table
, read.csv
,...). After the user input is converted from strings to numbers, the plotting begins. There, it can be seen, that plotting commands which are meant to be written to a file must be enclosed in two commands. These are in this case png(.)
and dev.off()
. The first function depends on the desired output file format (other common choices being jpeg(.)
and pdf(.)
). The second function, dev.off()
is always required. It writes the plot to the file and ends the plotting process.
The standalone script's file must first be made executable. This can happen by right-clicking the file, opening "Properties" in the opening menu and checking the "Executable" checkbox in the "Permissions" tab. Alternatively, the command
chmod +x PATH/TO/SCRIPT/SCRIPTNAME.R
can be called in a Terminal.
For each standalone script, a batch file must be written with the following contents:
"C:\Program Files\R-XXXXXXX\bin\Rscript.exe" "%~dp0\XXXXXXX.R" %*
A batch file is a normal text file, but which has a *.bat
extension except a *.txt
extension. Create it using a text editor like notepad
(not Word
) or similar and put the file name into quotation marks "FILENAME.bat"
) in the save dialog. To edit an existing batch file, right-click on it and select "Edit".
You have to adapt the code shown above everywhere XXX...
is written:
Explanation of the elements in the code: The first part "C:\...\Rscript.exe"
tells Windows where to find the Rscript.exe
program. The second part "%~dp0\XXX.R"
tells Rscript
to execute the R script you've written which resides in the same folder as the batch file (%~dp0
stands for the batch file folder). Finally, %*
forwards any command line arguments you give to the batch file to the R script.
If you double-click on the batch file, the R script is executed. If you drag files on the batch file, the corresponding file names are given to the R script as command line arguments.