How to use the command R (with examples)
R is a programming language and software environment for statistical computing and graphics. It provides a wide variety of statistical and graphical techniques and is highly extensible. This article will illustrate several use cases of the R command.
Use case 1: Start a REPL (interactive shell)
Code:
R
Motivation: Starting an R REPL allows users to interact with the R language in real-time. It is useful for exploring data, testing code snippets, and executing R commands on the fly.
Explanation:
Simply typing R
in the command line will start the R interpreter in an interactive shell mode.
Example output:
R version 4.1.1 (2021-08-10) -- "Kick Things"
...
Type 'q()' to quit R.
>
Use case 2: Check R version
Code:
R --version
Motivation: Knowing the version of R installed on your system is important for compatibility and troubleshooting purposes. This use case allows you to quickly check the installed R version.
Explanation:
Running R --version
will display the version information of the R interpreter installed on your system.
Example output:
R version 4.1.1 (2021-08-10) -- "Kick Things"
...
Use case 3: Start R in vanilla mode
Code:
R --vanilla
Motivation: Sometimes you may want to start a clean and fresh R session without any saved workspace from previous sessions. The vanilla mode ensures that no saved objects or settings interfere with your current session.
Explanation:
With the --vanilla
option, running R
will start a blank R session that doesn’t save the workspace at the end.
Example output:
R version 4.1.1 (2021-08-10) -- "Kick Things"
...
Type 'q()' to quit R.
>
Use case 4: Execute a file
Code:
R -f path/to/file.R
Motivation: Executing an R script file is helpful when you have a pre-written R code that you want to run without manually typing each command in the terminal.
Explanation:
By using the -f
option followed by the path to an R script file, you can execute the contents of the file using the R interpreter.
Example output:
R version 4.1.1 (2021-08-10) -- "Kick Things"
...
[1] "Hello, World!"
Use case 5: Execute an R expression and then exit
Code:
R -e expr
Motivation: Running a single R expression from the command line can be useful for quick calculations or evaluations without the need to open the full R interpreter.
Explanation:
With the -e
option followed by an R expression, you can execute the specified expression and then exit the R interpreter.
Example output:
R version 4.1.1 (2021-08-10) -- "Kick Things"
...
[1] 14.3
Use case 6: Run R with a debugger
Code:
R -d debugger
Motivation: Running R with a debugger can help identify and debug issues in your R code. It allows you to step through the execution of your script, set breakpoints, and inspect variables.
Explanation:
Using the -d
option followed by a specified debugger, you can run R code with debugging functionality enabled.
Example output:
R version 4.1.1 (2021-08-10) -- "Kick Things"
...
Debugging session initiated at ...
Browse[2]>
Use case 7: Check R packages from package sources
Code:
R CMD check path/to/package_source
Motivation:
Verifying the integrity and correctness of an R package from its source files is important before sharing or publishing the package. The R CMD check
command helps to ensure that the package meets the required standards.
Explanation:
Running R CMD check
followed by the path to the package source files allows you to perform checks on the package and report any issues or errors.
Example output:
R version 4.1.1 (2021-08-10) -- "Kick Things"
...
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
Conclusion:
This article covered various use cases of the R command, demonstrating how to start a REPL, check the R version, start R in vanilla mode, execute files and expressions, run R with a debugger, and check R packages from package sources. By understanding and utilizing these use cases, you can efficiently work with the R programming language and its environment.