R is a powerful language and environment for statistical computing and graphics. Often, you’ll want to execute R scripts directly from the command line (terminal) for automation, batch processing, or integration with other tools. This tutorial details several ways to accomplish this, catering to different needs and levels of customization.
Basic Execution with Rscript
The most straightforward way to run an R script from the command line is using the Rscript
executable. This is the recommended approach for most use cases.
-
Ensure
Rscript
is in your PATH:Rscript
should be available in your system’s PATH environment variable. If it’s not, you may need to add the directory containingRscript
to your PATH (the exact process depends on your operating system). Typically,Rscript
is located in the R installation directory’sbin
folder (e.g.,/usr/local/bin
on macOS or Linux). -
Basic Usage: Assuming your R script is named
my_script.R
, you can execute it as follows:Rscript my_script.R
This will execute the script and print any output to the console.
Example Script:
Let’s consider a simple R script:
# my_script.R
sayHello <- function(){
print('hello')
}
sayHello()
Running Rscript my_script.R
from the command line will produce the output:
[1] "hello"
Controlling Output with R CMD BATCH
Another option is to use R CMD BATCH
. However, it behaves differently than Rscript
. Instead of printing output to the console, R CMD BATCH
redirects output to a file named script_name.Rout
.
R CMD BATCH my_script.R
cat my_script.Rout
The cat
command displays the contents of the my_script.Rout
file, which contains the script’s output.
Shebang for Direct Execution
For convenience, you can add a "shebang" line to the top of your R script to make it directly executable (on Unix-like systems). This allows you to run the script as if it were a shell script.
#!/usr/bin/env Rscript
sayHello <- function(){
print('hello')
}
sayHello()
-
Make the script executable: Use the
chmod
command to give the script execute permissions:chmod +x my_script.R
-
Run the script:
./my_script.R
The
./
prefix indicates that the script is in the current directory.
Other Execution Methods
-
R < script.R
: This will run the script in interactive mode, which might not be what you want for automated tasks. -
R --no-save < script.R
: This runs the script and prevents R from saving the workspace after completion. Useful for scripts that don’t require persisting data. -
R --save < script.R
: This runs the script and saves the workspace after completion.
Installing Packages from the Command Line
You can also use R from the command line to install packages. This is helpful for setting up environments programmatically.
-
Install to default location:
R -e 'install.packages(c("package1", "package2"))'
-
Install to a specific location:
R -e 'install.packages(c("package1", "package2"), lib="/path/to/library")'
Replace /path/to/library
with the desired installation directory. This often requires appropriate permissions (e.g., sudo
).
Running R Markdown Documents
If you have an R Markdown document (.Rmd
), you can render it to HTML (or other formats) from the command line using rmarkdown::render()
.
R -e 'library("rmarkdown"); rmarkdown::render("my_document.Rmd")'
This will generate an HTML file (e.g., my_document.html
) in the same directory as the R Markdown document. You can then upload this HTML file, for instance, to RPubs using the command
R -e 'library("markdown"); markdown::rpubsUpload("account", "filename")'
Important Considerations:
- Permissions: Ensure that you have the necessary permissions to execute the R script and write any output files.
- Environment Variables: R scripts can access environment variables set in your shell. This can be useful for configuring the script’s behavior.
- Error Handling: Consider adding error handling to your R scripts to gracefully handle unexpected situations.
- Dependencies: Make sure that all required R packages are installed before running your script.