RStudio Shortcuts and Tips [Updated 2022]

<h2><strong>Work Faster in RStudio - Shortcuts, Tips, &amp; Tricks</strong></h2>
<strong>Updated</strong>: February 23, 2022.

In this article, we have compiled many of our favorite RStudio shortcuts and tips to help increase your productivity while working with the RStudio IDE. We’ll also provide information about supplemental tools and techniques that are useful for data scientists that work with R.

You can expect to learn the following from today's article:
<ul><li style="font-weight: 400;"><a href="#navigate">How to Navigate RStudio</a></li><li style="font-weight: 400;"><a href="#shortcuts">How to Use Shortcuts </a></li><li style="font-weight: 400;"><a href="#execute">How to Execute and Format Code</a></li><li style="font-weight: 400;"><a href="#history">Console History & The History Pane</a></li><li style="font-weight: 400;"><a href="#tabs">Dealing with Tabs</a></li><li style="font-weight: 400;"><a href="#code-inserting">Code Inserting Shortcuts</a></li><li style="font-weight: 400;"><a href="#code-snippets">How to Use Code Snippets</a></li><li style="font-weight: 400;"><a href="#custom-snippets">How to Create Custom Snippets in RStudio</a></li><li style="font-weight: 400;"><a href="#rstudio-search">How to Use Search in RStudio</a></li><li style="font-weight: 400;"><a href="#multiple-cursors">How to Edit with Multiple Cursors</a></li><li style="font-weight: 400;"><a href="#addins">How to Use R Addins</a></li><li><a href="#python-tips">RStudio and Python - Best Tips and Tricks</a></li><li><a href="#bonus">Bonus Tips: vim settings, .RProfile, .Renviron, and Docker</a></li></ul>
<strong>Note</strong>: We present both PC and Mac shortcuts in the GIFs, but we refer only to PC shortcuts in the article. Most Mac shortcuts fall under this dependence:
<ul><li>CTRL = ⌘ Command</li><li>Alt = ⌥ Option</li></ul>
In some cases, Ctrl will also be the Control key on Mac, which might be confusing at first. We recommend you to look up proper shortcuts on <a href="https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts" target="_blank" rel="noopener noreferrer">RStudio's website</a> or within or within RStudio itself by pressing <strong>⌥ Option + Shift + K (Alt + Shift + K)</strong>.

<strong>Note</strong>: At the time of writing this article, Posit PBC was RStudio PBC. We use RStudio and Posit interchangeably in this text (e.g. RStudio Connect == Posit Connect).

<hr />

<h2 id="navigate">How to Navigate the RStudio IDE</h2>
We guarantee you'll use at least a few RStudio panes regularly. If you want to speed up your workflow, you should learn how to focus between them quickly, without a mouse or a trackpad. Start by pressing <b>Ctrl </b>(in this case also Control on Mac) and a number corresponding to the desired pane.
<blockquote><strong>Working with Workbench? Use Appsilon's <a href="https://appsilon.com/top-5-tips-for-rstudio-desktop-workbench/" target="_blank" rel="noopener noreferrer">Top 5 Tips for RStudio Workbench and Desktop</a>.</strong></blockquote>
Add <b>Shift </b>to the combination if you want to toggle maximize pane for the one that you are switching to at the same time. The only pane with a different access shortcut is the terminal (<b>Shift+Alt+t</b>). Preset windows:
<ul><li>Help: <strong>3</strong></li><li>History: <strong>4</strong></li><li>Plots: <strong>5</strong></li><li>Environment: <strong>8</strong></li></ul>
The two you'll mostly use are <i>Source Editor</i> (<b>1</b>) and <i>Console</i> (<b>2</b>). 

<img class="alignnone size-full wp-image-12197" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0222df75d40a9af727f72_1_blog_movingfocus_optimized-1.gif" alt="" width="882" height="488" />

Let’s now discuss how you can improve how you work in those.
<h2 id="shortcuts">Tips for How to Use Shortcuts in RStudio</h2>
Usually, the first thing you have to do when starting a new R project is to write some code. There are some features that can make writing code both easier and faster. Even basic tricks can have a significant impact once you master them - especially when combined.
<h3>Code Completion</h3>
A suggestion list will pop up as you type or can be accessed manually by either pressing <b>Tab </b>or <b>Ctrl + Space</b>. You can adjust the settings in <b>Global Options -&gt;  Code -&gt; Completion</b>. To fill in the suggested phrase you have to press either <b>Tab </b>or <b>Enter</b>. Pressing <b>Ctrl + Space</b> with the auto-completion list open will close it. You can navigate through the suggestion list with arrows or just hover over the item before filling it in.

<img class="size-full wp-image-12199 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0222e9bd6df84c7f62f02_2_unnamed-1.gif" alt="" width="512" height="284" />

If the list is too long, try writing more letters to narrow it down.

It’s good to know that auto-completion in R supports <b>fuzzy matching</b>. This means you don’t really have to type all the letters - you can skip any of them as long as those typed are in order and identify what you are looking for. It is especially useful for long function names that you use often. Mastering this will allow you to type code much faster. Note that for fuzzy matching to work with auto-completion, the suggestion popup must be already active. In case it doesn’t behave as you would expect, try tweaking it in code completion options.
<h3>Paths</h3>
When you need to type a path, can use file path auto-complete which can be brought up by pressing the auto-completion shortcut (<b>Tab </b>or <b>Ctrl + Space</b>) from a pair of double or single quotes. 

By default it starts in your working directory, you can navigate from the root location like in shell console starting with "/", or step-up levels in the directory tree by stacking "../"

<img class="size-full wp-image-12201 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0222e40e2bdeffdb128f9_3_file_autocomplete-1.gif" alt="" width="546" height="268" />
<h2 id="execute">How to Execute and Format Code in RStudio</h2>
We'll now go over a set of useful RStudio shortcuts and tips that allow you to run the code in R scripts in different ways, reformat the R script, fix indentations, and move code blocks throughout the file.
<h3>Shortcuts to run R code</h3>
Leverage the following shortcuts to make running your R code easier:
<ul><li style="font-weight: 400;"><b>Ctrl + Enter</b> - Runs the current line and jumps to the next one, or runs the selected part without jumping further.</li><li style="font-weight: 400;"><b>Alt + Enter</b> - Allows running code without moving the cursor to the next line if you want to run one line of code multiple times without selecting it.</li><li style="font-weight: 400;"><b>Ctrl + Alt + R</b>  - Runs the entire script.</li><li style="font-weight: 400;"><b>Ctrl + Alt + B/E</b> - Run the script from the <b>B</b>eginning to the current line and from the current line to the <b>E</b>nd.</li></ul>
<img class="alignnone size-full wp-image-12203" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b022305b956c19cd8ea707_4_blog_code_execution_optimized-1.gif" alt="" width="916" height="578" />
<h3>Reformat R scripts</h3>
Use the following shortcuts in RStudio to reformat your code:
<ul><li style="font-weight: 400;"><b>Ctrl + I</b> - Fixes line indentations.</li><li style="font-weight: 400;"><b>Ctrl + Shift + A - </b>Does a complete reformat of the selected part of a code.</li></ul>
<img class="alignnone size-full wp-image-12205" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02230d9e8a647cdc5265c_5_blog_reformat_optimized-1.gif" alt="" width="864" height="537" />

If you are not happy with the outcome of those you can always undo the changes. We recommend the <a href="https://cran.r-project.org/web/packages/styler/styler.pdf" target="_blank" rel="noopener noreferrer">styler</a> package if you're looking for a more flexible solution for styling.
<h3>Move code blocks through the R script</h3>
Moving lines of code up and down is easily achieved with an <b>Alt + Up/Down </b>combination; there is no need to cut and paste. You can move a single active line that way, or even a whole selection. If you need to remove something <b>Ctrl + D</b> will delete the current line/selection in no time.

<img class="alignnone size-full wp-image-12207" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02231feb7b3e305d896e8_6_blog_movingcode-1.gif" alt="" width="929" height="319" />
<h2 id="history">Console History &amp; History Pane</h2>
You only have to run a piece of code once in the console. Accessing previously executed lines is as easy as navigating with the up arrow and down arrows to cycle between them in chronological order. For more visual feedback, press the <strong>Ctrl + Up</strong> arrow to get a list of the last commands. If you combine it with typing in a part of the searched phrase you can narrow it down and easily find even complicated commands that are buried deep in the history. It will also override autocomplete popup if it's active.

<strong>Note</strong>: searching console history doesn’t support fuzzy matching. If you want to clear your console, use <b>Ctrl+L</b>. The command history will be preserved.

<img class="alignnone size-full wp-image-12209 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02232a2bcff34651683e1_7_console_history_optimized-1.gif" alt="" width="1385" height="574" />

There is also a <i>History</i> pane(<b>4</b>) that stores executed commands. It allows search and easy selection of the ones you need (pick range with <b>Shift</b> or gather individual positions with <b>Ctrl</b>). Then insert them back into the console (<b>Enter</b>) or source file (<b>Shift</b> + <b>Enter</b>). The latter helps you avoid copying multiple commands from the console to source manually which is troublesome due to line signs ">" that get copied as well and would otherwise have to be removed.
<h2 id="tabs">Dealing with Tabs</h2>
You'll work with more than one tab more often than not. Switching between them is easy with <b>Ctrl+Tab</b> and <b>Ctrl+Shift+Tab</b> combinations. It allows you to jump to the next and previous tab, respectively. An alternative way to do the same is with <b>Ctrl + F11</b>/<b> F12 </b>if it suits you better. It is also possible to jump to the first or last one by adding <b>Shift</b> to those. The last option is navigating through tabs in the order they were accessed with <b>Ctrl + F9/F10</b>.
<h3>Navigate tabs history</h3>
<img class="size-full wp-image-12211 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02233d9e8a647cdc5289b_8_tabs-navigate-tab-history-1.gif-and-shift-tab-1.gif" alt="" width="734" height="420" />
<h3>Jumping tabs</h3>
<img class="size-full wp-image-12213 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b022356738007ca36721f0_9_tabs_next_previous_first_last-1.gif" alt="" width="734" height="420" />
<h3>Going through tabs</h3>
<img class="alignnone size-full wp-image-12215 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02236a20f7e6672b83295_10_jumpingtabs_merged-1.gif" alt="" width="1089" height="618" />
<h3>Closing tabs</h3>
You can close a current tab with<b> Ctrl + w</b>. It is a much better choice than using those small "x" buttons on the right side of your tabs. If you get to the point where you have a huge amount of tabs open you can:

<i>Close All</i>  |<b> Ctrl + Shift + w</b> (+ <b>Alt</b> to keep the currently open one):

<img class="alignnone size-full wp-image-12217 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02238a2bcff34651689c2_11_closing_tabs-1.gif" alt="" width="1088" height="682" />

Or if you prefer to keep many tabs open, you can search through your open tabs with <b>Ctrl + Shift + </b>. Remember to be exact, as there's no fuzzy matching here. This search can also be activated with the"<b>&gt;&gt;</b>" icon on the tabs bar.

<img class="alignnone size-full wp-image-12219 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b02239a597077171c05d00_12_blog_tabs_search-1.gif" alt="" width="1010" height="462" />

The above shortcuts are also accessible from the File dropdown menu - this can come in handy while using the RStudio browser session or simply if you forget them.
<h2 id="code-inserting">Code Inserting Shortcuts in RStudio</h2>
We'll now go over a couple of interesting shortcuts that automatically insert code to your R scripts.
<h3>Operators and sections</h3>
<ul><li><b>Alt + (-)</b> - Inserts the assignment operator (<code>&lt;-</code>) with spaces surrounding it.</li><li><b>Ctrl + Shift + M</b> - Inserts the magrittr/pipe operator (<code>%&gt;%</code>) with spaces surrounding it.</li><li><b>Ctrl + Shift + R</b> - Creates a foldable comment section in your code. These are used for code externalization with <code>knitr:read_chunk()</code> function. <a href="https://yihui.name/knitr/demo/externalization/" target="_blank" rel="noopener noreferrer">Learn more</a>.</li><li><b>Alt + L</b> - Collapse active section.</li><li><strong>Alt + O</strong> - Collapse all sections.</li><li><b>Alt + Shift + L</b> - Open active section.</li><li><strong>Alt + Shift + O</strong> - Open all sections</li></ul>
<img class="size-full wp-image-12221 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0223b4d04f717e1cb5cda_13_insertcode7-1.gif" alt="" width="936" height="389" />
<h3>Function/Variable extraction</h3>
If you have a statement that you want to convert into a function, don’t start from scratch. Select it and press <b>Ctrl + Alt + X</b> - a shortcut for "extract into function". You only need to provide the function name, all necessary inputs will be filled in automatically.

There is also a similar shortcut for a variable extraction available with <b>Ctrl + Alt + V. </b>Here's a usage example:

<img class="alignnone size-full wp-image-12223 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0223b31010cba0b0e8821_14_extract_fun_var-1.gif" alt="" width="936" height="422" />
<h3>Renaming in Scope</h3>
If you have to change a variable name in multiple places but you are afraid that "find and replace" will mess up your code, fear not. It's possible to rename in scope only. It's achieved by selecting the function or variable we want to change and pressing <b>Ctrl + Shift + Alt + M.</b>

It selects all occurrences in scope, you will have to just type a new name.

Yes, the shortcut is long, but it can be helpful. We find it to be easier to remember as an extension of the magrittr operator shortcut, so <b>Pipe</b> <b>+</b> <b>Alt</b>.

<img class="alignnone size-full wp-image-12225 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0223c92a26c843b64b548_15_rename_in_scope-1.gif" alt="" width="936" height="400" />
<h2 id="code-snippets">Using Code Snippets in RStudio</h2>
Tired of writing the same chunks of code over and over and having to remember all of the brackets and required parameters for functions? A good way to avoid it is by using code snippets. This part of our RStudio shortcuts and tips article teaches you everything you need to know.
<h3>What are code snippets?</h3>
Code snippets are pieces of re-usable boilerplate code.

<img class="alignnone size-full wp-image-12227 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0223db90a6b94c7bd7b01_16_shinyapp_snippet-1.gif" alt="" width="634" height="302" />

Snippets are perfect for automatically inserting boilerplate code and avoiding the duplication of simple tasks. If you are looking for a way to speed up writing large parts of code when time is limited (e.g. live coding during a presentation), code snippets can be very useful.
<h3>How do I use code snippets?</h3>
Snippets can be recognized on your auto-completion list by a {snippet} tag.

Write the snippet name, press <b>Shift + Tab</b>, or <b>Tab</b> twice to use it. If your input is needed to complete it - just fill out positions with elements that are important. You can cycle through them with <b>Tab</b>.

Some of the snippets which are available by default include:
<ul><li style="font-weight: 400;">Declarations - lib, req, fun, ret, mat</li><li style="font-weight: 400;">Loops - for, while, switch</li><li style="font-weight: 400;">Conditionals - if, el, and ei for conditionals</li><li style="font-weight: 400;">Apply family functions - apply, lapply, sapply, etc.</li><li style="font-weight: 400;">S4 classes/methods definitions - sc, sm, and sg.</li><li style="font-weight: 400;">Shiny App template - shinyapp</li></ul>
<img class="alignnone size-full wp-image-12229 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0223e8587215be0b15d1a_17_snippets4-1.gif" alt="" width="578" height="507" />

And that's just for R! There are also snippets for other languages and it is very easy to customize and define your own!

You might have noticed that we used insertOperatorsExample, a simple <b>custom snippet we</b> created on the first GIF demonstrating operator shortcuts.
<h2 id="custom-snippets">How to Create Custom Code Snippets in RStudio</h2>
For customizing or creating your own snippets use the <b>Edit Snippets</b> button under the <b>Snippets</b> section in <em>Tools -&gt; Global Options -&gt; Code</em><b>.</b>

To better understand how to can create snippets, let’s take a look at a matrix and function snippets declarations code as an example.

<pre>matrix(${1:data}, nrow = ${2:rows}, ncol = ${3:cols})
</pre>
$ sign is used as a special character to denote where the cursor should jump after completing each section of a snippet. Inside the brackets, we have a field index (the order in which the cursor will jump after pressing the tab), 0 is used as the last field, and the text after a colon is used as information on what should be placed in that spot. In order to insert a literal "$" inside a snippet, it must be escaped as \$.

Snippets, besides generating code templates, can also run R code. It allows you to create dynamic snippets. By using r expr anywhere in your snippet your R code will be executed when the snippet is expanded, and the result inserted into the document.  

As an example, take a look at the timestamp snippet declaration that is available by default.

<pre>`r paste("#", date(), "------------------------------\n")`
</pre>
It runs a paste function to insert a comment with a current date into the code. Its execution resolves into something like this:

Let's now create a custom snippet for inserting the pipe operator, but instead of space we will have a new line right after it:

<pre>`r paste(" %&gt;%\n")`
</pre>
<img class="alignnone size-full wp-image-12231 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0224022f94709a7bfe978_18_pipesnippet-1-1.gif" alt="" width="448" height="186" />
<h2 id="rstudio-search">Using Search in RStudio</h2>
In case you don’t have a lot of code yet, there are tools to generate it quickly. The next question is then, how to find things that you are looking for. There are multiple options available for search that you can use.

Go to file function <b>Ctrl + (.)</b>.  In there you can quickly search your project for a file or function and jump directly to it. It supports fuzzy matching so it’s easy to find what you need.

<img class="alignnone size-full wp-image-12233 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b022419b9335fb8c0b3661_19_searching2-1.gif" alt="" width="918" height="415" />

For more robustness, press <b>Ctrl + Shift + F</b> to call the Find in Files window. It allows you to search through files in a directory that you can specify (even outside the project). You can jump between elements you found by double-clicking them in the Find in Files window which opens next to the console.

<img class="alignnone size-full wp-image-12235 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b022436eae3fdad86c7171_20_searching2-1-1.gif" alt="" width="1020" height="512" />

If you want to search only inside an active source tab you can use the find bar with <b>Ctrl + F</b>. It brings several additional options like replacing texts and searching inside a selected part of code only. It can also be useful for multiple cursor editing, which we'll discuss in the section below.

We've covered more methods in <a href="https://appsilon.com/r-studio-shortcuts-and-tips/">an additional RStudio article </a>- search within console history and search through your tabs. You can refer to it if you want to get more details on those.
<blockquote><strong>With R Shiny you can build beautiful, interactive web apps. Discover the possibilities with <a href="https://demo.appsilon.com/" target="_blank" rel="noopener noreferrer">Appsilon's Shiny Demos</a>. </strong></blockquote>
<h2 id="multiple-cursors">How to Edit With Multiple Cursors in RStudio</h2>
In RStudio, you can write and edit in more than one place at a time with multiple cursors. Press <b>Ctrl + Alt + (Up/Down)</b> to create a new cursor in the direction in which you press. If you want to quickly select more lines use <b>Alt</b> and drag with the mouse to create a rectangular selection, or <b>Alt + Shift</b> and click to create a rectangular selection from the current cursor position to the clicked position. 

This way of editing may look intimidating at first, and may not be easy to operate initially. However, knowing it is there can save you time when you encounter repetitive multi-line tasks. Try playing around with using multiple cursors and see how it feels.

Below you can see an example of how using multiple cursors might look:

<img class="alignnone size-full wp-image-12237 aligncenter" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0224331010cba0b0e88ba_21_multiple-line-editing4-1.gif" alt="" width="1503" height="818" />

Another way is to use the Find/Replace toolbar from the previous paragraph to place multiple cursors. Just search for a phrase and press the <b>All </b>button to select all matching items. It will create a cursor for each matching phrase. If you don’t want to search throughout the entire file you can also limit the area for a searched phrase by selecting a part you are interested in and checking the box with the "In selection" option.

<img class="alignnone size-full wp-image-12241" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b0224540e2bdeffdb13956_22_multiple-line-editing4_1-1.gif" alt="" width="996" height="555" />
<h2 id="addins">How to Use R Addins</h2>
R Addins are a broad topic that deserves an article on its own. We'll give you a brief introduction to the concept.
<h3>What are R addins?</h3>
R Addins allow you to execute R functions in an interactive way right from RStudio. Addins are distributed as R packages and can be launched either through the <i>Addins</i> dropdown on the toolbar or through assigned keyboard shortcuts.

We distinguish two types of addins:
<ul><li><strong>Text macros</strong> - They insert text into the console/source pane or can transform text within the source pane.</li><li><strong>Shiny Gadgets</strong> - Interactive Shiny applications launched inside RStudio which may also perform transformations like text macros, but their possibilities are much more extensive.</li></ul>
<h3>Testing out some addins</h3>
To quickly try addins you can install some examples from RStudio Github.

<pre>devtools::install_github("rstudio/addinexamples", type = "source")
</pre>
It will give you a text macro for inserting <code>%in%</code> operator, as well as three shiny gadgets for a small sneak, peek of what's possible.

As we mentioned, you can assign a keyboard shortcut to an addin the same way as you do it with regular shortcuts.  You can find them easily by filtering "Addin" (all of them have their scope set like that).
<h3>Make your own R addins</h3>
If you want to check out more of them try the <a href="https://github.com/daattali/addinslist" target="_blank" rel="noopener noreferrer">addinslist</a> package by Dean Attali.

If you would like to create your own addins, you can find more information on how to do it <a href="https://rstudio.github.io/rstudioaddins/#developing-addins" target="_blank" rel="noopener noreferrer">here</a>.
<h2 id="python-tips">RStudio IDE and Python - Best Tips and Tricks</h2>
If you're working on a project that primarily uses R with occasional Python scripts, RStudio should be your IDE of choice. <strong>Go to File -&gt; New File -&gt; Python Script</strong> to create a new Python file.
<h3>How to run Python scripts from R</h3>
Create a new Python script in RStudio - we've named ours <code>pyscript.py</code>. It prints the sum of two numbers:

<pre>if __name__ == '__main__':
 x = 5
 y = 10
 print(f"{x} + {y} = {x + y}")
</pre>
To run the script from R, you have to import the <code>reticulate</code> package and call the <code>run_py_file()</code> function with the path of the Python script provided as an argument:

<pre>library(reticulate)

py_run_file("")
</pre>
<h3>How to call Python in R scripts</h3>
It's recommended to use the <code>reticulate</code> package when you're writing the code in both R and Python, but R is the dominant language. In the opposite case, opt for <code>rpy2</code>.

The following code snippet shows you how to create a crosstab from the MTCars dataset, convert it into a Pandas DataFrame, and then back to R:

<pre>library(reticulate)

# Import Pandas module
pd &lt;- import(module = "pandas", as = "pd", convert = FALSE)

data &lt;- mtcars

# From R to Python
mtcars_crosstab &lt;- pd$crosstab(
 index = data$cyl,
 columns = data$gear
)

# From Python to R
mtcars_crosstab_r &lt;- py_to_r(mtcars_crosstab)
</pre>
<h3>Use different Python environment</h3>
Functions <code>use_virtualenv()</code> and <code>use_condaenv()</code> functions enable you to specify versions of Python in virtual or conda environments. Here's how to use them:

<pre>library(reticulate)
use_virtualenv("myenv")
use_condaenv("myenv")
</pre>
<h2 id="bonus">Bonus RStudio Tips</h2>
<h3>Tip: Use vim settings</h3>
Keep your hands in one place! It’s a powerful method for programmers. Examples: <code>dd</code> to delete the whole line, <code>7dd</code> to delete 7 lines, navigate, macros, jumping around whole words instead of letters.
<h3>Tip: Use .RProfile</h3>
When you develop an R package, it’s useful to load frequently used dev packages in the .RProfile file (placed in the main package directory). For example:

<pre>library(devtools)
library(testthat)
</pre>
This way you can use functions like test(), check() without specific package reference or loading the packages on your own.
<h3>Tip: Increase security with .Renviron</h3>
Do not keep credentials inside your project code. A good practice is to keep them “gitignored” inside the <b>.Renviron file</b>:

<pre>db_password=MySecretPassword
</pre>
And use a variable in the code with Sys.getenv("db_password").
<h3>Tip: Use Docker</h3>
If you want to keep a consistent environment for your project development within a team, use a dockerized version of RStudio (<a href="https://hub.docker.com/r/rocker/rstudio/" target="_blank" rel="noopener noreferrer">https://hub.docker.com/r/rocker/rstudio/</a>).

<hr />

<h2>Tell Us About Your RStudio Shortcuts and Tips</h2>
There is obviously plenty more to explore on the topic of improving your RStudio workflow, and we hope you are inspired to pursue further exploration and experiment on your own. If you end up with something useful as a result - be it a code snippet, an addition, or just something useful that we did not mention here, please share it as a comment below. We’ll be updating this page regularly with more RStudio shortcuts and tips.

If you're interested in making more out of your data, check out how Appsilon builds the world's most <a href="https://appsilon.com/shiny/" target="_blank" rel="noopener noreferrer">advanced R Shiny Dashboards</a>. And be sure to sign-up for updates on Appsilon's <a href="https://appsilon.com/blog/" target="_blank" rel="noopener noreferrer">R and Shiny educational blog</a>. We provide the R community with the best tools, tips, shortcuts, and <a href="https://appsilon.com/opensource/" target="_blank" rel="noopener noreferrer">open-source packages</a> to make the most out of your data science needs.
<h2>Related RStudio Reads</h2><ul><li><a href="https://appsilon.com/r-studio-shortcuts-and-tips-2/">RStudio Shortcuts and Tips</a></li><li><a href="https://appsilon.com/r-studio-shortcuts-and-tips-part-2/">RStudio Shortcuts and Tips part 2</a></li><li><a href="https://appsilon.com/top-5-tips-for-rstudio-desktop-workbench/">Top Tips for RStudio Workbench and Desktop</a></li></ul>
<h2>Further Reading</h2>
If you’re looking for more R tutorials, try these out:
<ul><li><a href="https://appsilon.com/video-tutorial-create-and-customize-a-simple-shiny-dashboard/" target="_blank" rel="noopener noreferrer">Video Tutorial: Create and Customize a Simple Shiny Dashboard</a></li><li><a href="https://appsilon.com/is-it-possible-to-build-a-video-game-in-r-shiny/" target="_blank" rel="noopener noreferrer">Tutorial: How I Built a Video Game in R Shiny</a></li><li><a href="https://appsilon.com/howto-css-and-shiny/" target="_blank" rel="noopener noreferrer">Tutorial: How to Use CSS to Style Your R Shiny Dashboards</a></li></ul>

Have questions or insights?

Engage with experts, share ideas and take your data journey to the next level!

Is Your Software GxP Compliant?

Download a checklist designed for clinical managers in data departments to make sure that software meets requirements for FDA and EMA submissions.
Explore Possibilities

Share Your Data Goals with Us

From advanced analytics to platform development and pharma consulting, we craft solutions tailored to your needs.

Talk to our Experts
shiny dashboards
r
data analytics
rstudio
tutorials