RStudio version 1.1 introduced the
Terminal functionality, which does not seem to be getting enough deserved attention and love even though it is very well integrated with the rest of the IDE and can be extremely useful for several daily use-cases.
In this post we will try to cover 4 very common scenarios where the Terminal can be very useful and productive, and how to get the most of it.
In short, the RStudio Terminal provides access to the system shell directly from the RStudio IDE, supporting xterm emulation, full-screen terminal applications, command line operations and more. It also has useful customizable keyboard shortcut bindings to make frequent usage more efficient and enables usage of multiple such Terminals simultaneously.
The experience may vary based on each user’s setup, this experience comes mostly from using RStudio server on a Linux-based system.
Four common use-cases
1. Execute resource-heavy R code in the Terminal quickly
A very common use case where the Terminal makes my life a lot easier is when I need to execute a longer running or resource-heavy tasks in R. Using the RStudio IDE’s session for such tasks can be challenging because running them can slow the entire IDE down, sometimes even so much that it is barely usable. We can easily prevent this by running such tasks in a separate R process within the Terminal. We could of course do this using
putty or other software, however doing it within RStudio brings
- seamless keyboard shortcut integration between the editor window and the Terminal
- ability to use multiple Terminals easily
- no need to use other software
To run commands in the terminal, we simply press:
Shift + Alt + Rto open a new terminal
Rin the Terminal
Ctrl + 1to focus back to the editor window
Ctrl + Alt + Enterto send commands to be executed directly to the Terminal
We can also do this with multiple Terminals if we need to run multiple such “jobs”, and easily switch between Terminal windows using keyboard shortcuts
Ctrl + Alt + F11– Previous terminal
Ctrl + Alt + F12– Next terminal
Note the shortcuts mentioned above are default and more than likely not Mac-relevant, but you can easily find those as well in case you are a Mac user, and change them to your liking as well.
2. Advanced version control directly within RStudio
RStudio has a neat version control integration which is a very nice addition to the IDE, however there are some advanced version control operations that are not possible to handle there directly,
git rebase and
git push --force being just a couple of examples. Thanks to the Terminal, you can very easily do all those operations without ever leaving your RStudio IDE.
3. Serving your Shiny app/Blogdown site without blocking or slowdown
My favourite use of the Terminal when writing this blog is to serve the site via the Terminal and see the changes I make live, without the IDE being slowed down and laggy, which often happens when serving the site directly from RStudio’s R session. A very similar point also applies when running a Shiny app from within RStudio. This simple use of the Terminal makes things more convenient for me.
Running a Shiny app. We can use a pre-selected port to make viewing later easier:
# Send to the terminal with Ctrl + Alt + Enter: R -e 'library(shiny); runApp("appdir", port = 9999, launch.browser = FALSE)' # Then show in the viewer with Ctrl + Enter rstudioapi::viewer("http://127.0.0.1:9999")
Similarly, serving a Blogdown site:
# Send to the terminal with Ctrl + Alt + Enter: R -e 'library(blogdown); blogdown::serve_site(port = 9999, browser = FALSE)' # Then show in the viewer with Ctrl + Enter rstudioapi::viewer("http://127.0.0.1:9999")
Alternatively, we can also use
rstudioapi to send commands to the Terminal:
termId <- rstudioapi::terminalExecute("R -e 'getwd(); library(shiny); runApp(\"appdir\", port = 9999, launch.browser = FALSE)'") # Then show in the viewer with Ctrl + Enter rstudioapi::viewer("http://127.0.0.1:9999") # When done, we can kill that terminal rstudioapi::terminalKill(termId)
4. Test your bash, python and much more conveniently
Since the Terminal is really just system shell access, you can get very creative with its use. To me, the key here is the keyboard shortcut integration between the editor and the terminal.
Very basic example using the Terminal to run python code. Note that this (somewhat obviously) works around R and the need for an R to Python interface package:
# Ctrl + Alt + Enter to send to the Terminal # Launch python python # Run some python code 1 + 1 # When done with python, exit exit()
Testing a random bash script
# Ctrl + Alt + Enter to send to the Terminal echo "Run tmux, split window and run top" tmux new -s "Fun" tmux switch -t "Fun" tmux split-window -h tmux select-pane -t 0 top
- By default, the processes in Terminal run as child processes of the main
rsessionprocess, therefore restarting R session will kill those. We can workaround this fact using tools like
- You can specify what the Terminal sessions are open with under
Tools -> Global Options... -> Terminal
- The Terminal can be interfaced with using the
rstudioapipackage functionality. Read the Interacting with Terminals vignette to learn more.
- If the default keyboard shortcuts are not the most convenient for you, they can be updated and more added under the
Tools -> Modify Keyboard Shortcuts...menu in RStudio
- Using the RStudio Terminal, a great guide by Gary Ritchie
- Interacting with Terminals vignette of the rstudioapi package
- Customizing Keyboard Shortcuts in RStudio by Kevin Ushey
- Introduction to GNU Screen
- The Tao of tmux
The last Saturday of September 20 years ago a key parliamentary election was held in Slovakia, resulting in the end of the reign of Vladimír Mečiar’s government and Slovakia being able to conduct crucial reforms and become a member of the EU and NATO.
Did you find the article helpful or interesting? Help others find it by sharing