To install R packages from the shell (command line), you can follow these steps:
Steps:
- Open the shell or terminal.
- Use the
R
command to enter the R environment by typing the following:R
This will open the R console in the terminal.
- Install R packages using the
install.packages()
function.install.packages("package_name")
For example, to install the
ggplot2
package:install.packages("ggplot2")
- Exit the R console by typing:
q()
Then confirm by typing
n
ory
if asked whether to save the workspace.
Alternatively: Install R Packages Directly from Shell
You can also install R packages directly from the shell without entering the R console by using the Rscript
command. Here’s the syntax:
Rscript -e "install.packages('package_name')"
For example, to install ggplot2
directly from the shell, run:
Rscript -e "install.packages('ggplot2')"
Installing Multiple Packages at Once
To install multiple packages at once from the shell, you can pass a vector of package names like this:
Rscript -e "install.packages(c('ggplot2', 'dplyr', 'tidyr'))"
Summary:
- To enter R and install packages interactively: Use the
R
command andinstall.packages()
. - To install packages directly from the shell: Use the
Rscript -e
command with theinstall.packages()
function.