Terminal
Shell is an application that allows you to communicate directly with the operating system via code rather than pointing and clicking. It takes commands via text, and is often used for viewing, editing, and handling files, advanced source control operations, or execution of long-running jobs.
Shell/Terminal is also known as CLI (Command Line Interface) or Bash (Bourne Again Shell). It predates graphical user interfaces (GUI), but remains superior in some areas of functionality and efficiency.
The terminal in RStudio also allows you to access the system shell, but the terminal can also be launched from Applications
on Mac, or as Command Prompt
on Windows.
Using the Terminal in RStudio
You can access the R Terminal through the ‘terminal’ tab near the console
If the tab is not already showing: Tools -> Terminal -> New Terminal
You can have multiple terminal sessions simultaneously using the ‘New Terminal’ option, and switch between them using the arrows in the top left
Terminals can be closed with the ‘X’ or with the ‘close terminal’ option
Helpful Commands
Command | Description |
---|---|
whoami | Who is the user? |
cdate / date | Get date, time and timezone |
cal | Display calendar |
clear | Clear the screen |
The clear
command clears only the visible part of the terminal; to fully wipe it, use Ctrl+L.
View Documentation
Command | Description |
---|---|
man | Display manual pages for a command |
whatis | Single line description of a command |
Check or Change Working Directory
Command | Description |
---|---|
pwd | Print working directory |
ls | List directory contents |
cd | Change current working directory |
mkdir | Create directory |
rmdir | Remove/delete directory |
Managing Files
Command | Description |
---|---|
touch | Create empty file(s)/change timestamp |
cp | Copy files & folders |
mv | Rename/move file |
rm | Remove/delete file |
diff | Compare files |
If the file does not exist, touch
will create an empty file of the given name:
touch myanalysis.R
We can create a copy of a file called release_names.txt
and name it release_names_2.txt
with the following:
cp release_names.txt release_names_2.txt
Running R Code in Terminal
R -e
will launch R and then run the code specified within quotes. Use a semi-colon to execute multiple expressions. Windows users must ensure that R is added to the path environment.
R -e "head(mtcars); tail(mtcars)"
Rscript -e
will run code without launching R, or execute an R script (.R file):
Rscript -e "head(mtcars)"
Rscript analysis.R
While editing files in RStudio, the current line or selected lines can be sent to the active terminal via Ctrl+Alt+Enter/Cmd+Opt+Return
(on a Mac).
Programs can be running in the Terminal and in RStudio simultaneously.
Running R Code on a Remote Server
If you are working with data that is too large or your analysis requires too much memory, you may need to access a remote server. We can do so with ssh
or secure shell.
In a Terminal, the most straightforward way to access a remote server is with the ssh
command and connecting to the IP address or domain name of the remote_host
with your assigned username
.Ma
ssh username@remote_host
You’ll need to provide a password to ensure you have proper authorization to access that server. Once you have access to that machine, you may run commands just as you might work in the Terminal on your local machine.
To quit/exit, the remote server, type
exit
At Macalester, you could practice this by logging into the MSCS remote server.
If you are on campus (or are currently running only a Mac VPN), you can open the Terminal and type (use your Mac username)
ssh username@mscs.olri.macalester.edu
and provide your Mac password (same as email, 1600Grand, etc.).
Try the following commands to create new folders and files and navigate through those folders:
pwd
mkdir DS212
ls
cd DS212
mkdir homework
cd homework
touch hw1.txt
exit
This server does not have R installed so the R
commands won’t work on this server but they could work on another server that did have R installed.
Transfering files to/from a Remote Server
If you are wanting to copy files from your local machine to a remote server,
scp file.txt username@remote_host:remote_path
If you are wanting to copy files from a remote server to your local machine,
scp username@remote_host:remote_path/file.txt local_path
At Macalester, you could practice this by logging into the MSCS remote server.
If you are on campus (or are currently running only a Mac VPN), you can open the Terminal and type (use your Mac username)
cd ~/Desktop
touch temp_file_212.txt
scp temp_file_212.txt username@mscs.olri.macalester.edu:DS212/
ssh username@mscs.olri.macalester.edu
and provide your Mac password (same as email, 1600Grand, etc.).
Try the following commands to see the new file you copied from your local machine to the remote server within the DS212
folder called temp_file_212.txt
cd DS212
ls
exit
Resources
- https://blog.rsquaredacademy.com/command-line-basics-for-r-users/#shellrstudio
- https://bash-intro.rsquaredacademy.com/
- https://docs.posit.co/ide/user/ide/guide/tools/terminal.html
- https://support.posit.co/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal-in-the-RStudio-IDE
- https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/