<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/4-ways-to-be-more-productive-using-rstudios-terminal/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://jozefhajnala.gitlab.io/r/r905-rstudio-terminal/"> Jozef's Rblog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<h1>Introduction</h1>
<p>RStudio version 1.1 introduced the <code>Terminal</code> 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.</p>
<blockquote>
<p>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.</p>
</blockquote>
<div>
<img alt="RStudio Terminal Fun" src="https://i2.wp.com/jozefhajnala.gitlab.io/r/img/r905-01-terminal.gif?w=456&ssl=1" data-recalc-dims="1">
<p>RStudio Terminal Fun</p>
</div>
<p>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.</p>
<p>The experience may vary based on each user’s setup, this experience comes mostly from using RStudio server on a Linux-based system.</p>
</div>
<div>
<h1>Four common use-cases</h1>
<div>
<h2>1. Execute resource-heavy R code in the Terminal quickly</h2>
<p>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 <code>putty</code> or other software, however doing it within RStudio brings</p>
<ol>
<li>seamless keyboard shortcut integration between the editor window and the Terminal</li>
<li>ability to use multiple Terminals easily</li>
<li>no need to use other software</li>
</ol>
<p>To run commands in the terminal, we simply press:</p>
<ul>
<li><code>Shift + Alt + R</code> to open a new terminal</li>
<li>launch <code>R</code> in the Terminal</li>
<li><code>Ctrl + 1</code> to focus back to the editor window</li>
<li><code>Ctrl + Alt + Enter</code> to send commands to be executed directly to the Terminal</li>
</ul>
<p>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</p>
<ul>
<li><code>Ctrl + Alt + F11</code> – Previous terminal</li>
<li><code>Ctrl + Alt + F12</code> – Next terminal</li>
</ul>
<p>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.</p>
</div>
<div>
<h2>2. Advanced version control directly within RStudio</h2>
<p>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, <code>git rebase</code> and <code>git push --force</code> being just a couple of examples. Thanks to the Terminal, you can very easily do all those operations without ever leaving your RStudio IDE.</p>
</div>
<div>
<h2>3. Serving your Shiny app/Blogdown site without blocking or slowdown</h2>
<p>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.</p>
<p>Running a Shiny app. We can use a pre-selected port to make viewing later easier:</p>
<pre><code># Send to the terminal with Ctrl + Alt + Enter:
R -e 'library(shiny); runApp(&quot;appdir&quot;, port = 9999, launch.browser = FALSE)'
# Then show in the viewer with Ctrl + Enter
rstudioapi::viewer(&quot;http://127.0.0.1:9999&quot;)</code></pre>
<p>Similarly, serving a Blogdown site:</p>
<pre><code># 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(&quot;http://127.0.0.1:9999&quot;)</code></pre>
<p>Alternatively, we can also use <code>rstudioapi</code> to send commands to the Terminal:</p>
<pre><code>termId &lt;- rstudioapi::terminalExecute(&quot;R -e 'getwd(); library(shiny); runApp(\&quot;appdir\&quot;, port = 9999, launch.browser = FALSE)'&quot;)
# Then show in the viewer with Ctrl + Enter
rstudioapi::viewer(&quot;http://127.0.0.1:9999&quot;)
# When done, we can kill that terminal
rstudioapi::terminalKill(termId)</code></pre>
</div>
<div>
<h2>4. Test your bash, python and much more conveniently</h2>
<p>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.</p>
<p>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:</p>
<pre><code># Ctrl + Alt + Enter to send to the Terminal
# Launch python
python
# Run some python code
1 + 1
# When done with python, exit
exit()</code></pre>
<p>Testing a random bash script</p>
<pre><code># Ctrl + Alt + Enter to send to the Terminal
echo &quot;Run tmux, split window and run top&quot;
tmux new -s &quot;Fun&quot;
tmux switch -t &quot;Fun&quot;
tmux split-window -h
tmux select-pane -t 0
top</code></pre>
</div>
</div>
<div>
<h1>Quick notes</h1>
<ul>
<li>By default, the processes in Terminal run as child processes of the main <code>rsession</code> process, therefore restarting R session will kill those. We can workaround this fact using tools like <code>screen</code> or <code>tmux</code></li>
<li>You can specify what the Terminal sessions are open with under <code>Tools -> Global Options... -> Terminal</code></li>
<li>The Terminal can be interfaced with using the <code>rstudioapi</code> package functionality. Read the <a rel="nofollow" href="https://cran.rstudio.com/web/packages/rstudioapi/vignettes/terminal.html" target="_blank">Interacting with Terminals</a> vignette to learn more.</li>
<li>If the default keyboard shortcuts are not the most convenient for you, they can be updated and more added under the <code>Tools -> Modify Keyboard Shortcuts...</code> menu in RStudio</li>
</ul>
</div>
<div>
<h1>Resources</h1>
<ul>
<li><a rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/115010737148-Using-the-RStudio-Terminal" target="_blank">Using the RStudio Terminal</a>, a great guide by Gary Ritchie</li>
<li><a rel="nofollow" href="https://cran.rstudio.com/web/packages/rstudioapi/vignettes/terminal.html" target="_blank">Interacting with Terminals</a> vignette of the rstudioapi package</li>
<li><a rel="nofollow" href="https://support.rstudio.com/hc/en-us/articles/206382178?version=1.2.792&mode=server" target="_blank">Customizing Keyboard Shortcuts</a> in RStudio by Kevin Ushey</li>
<li><a rel="nofollow" href="https://www.gnu.org/software/screen/" target="_blank">Introduction to GNU Screen</a></li>
<li><a rel="nofollow" href="https://leanpub.com/the-tao-of-tmux/read" target="_blank">The Tao of tmux</a></li>
</ul>
<blockquote>
<p><img alt="Coat of arms of Slovakia" width="40" align="left" src="https://upload.wikimedia.org/wikipedia/commons/d/d2/Coat_of_arms_of_Slovakia.svg">The last Saturday of September 20 years ago <a rel="nofollow" href="https://en.wikipedia.org/wiki/Slovak_parliamentary_election,_1998" target="_blank">a key parliamentary election</a> was held in Slovakia, resulting in the end of the reign of Vladimír Mečiar’s government and Slovakia being able to <a rel="nofollow" href="https://www.theguardian.com/world/2004/apr/28/eu.politics2" target="_blank">conduct crucial reforms and become a member of the EU and NATO</a>.</p>
</blockquote>
<h2>Did you find the article helpful or interesting? Help others find it by sharing</h2>
<div></div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://jozefhajnala.gitlab.io/r/r905-rstudio-terminal/"> Jozef's Rblog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/-MlxONkf6RI" height="1"></a></div>