Introduction
Life in data science is demanding and working more efficiently can help with turnaround times or providing the mental space needed to dive further into analysis before deadlines start looming on the horizon.
R Studio from Posit is one of the best development environments available. In addition to R, it also offers a very slick and cost free means of programming in Python, Stan, and several other languages.
There are several little used features available in R Studio to streamline our workflows and save precious keystrokes and reduce eye strain. We will explore a few of these below.
Option One: Snippets
Snippets are small templates for commonly used code snippets that one can make available for projects. These are accessed via intellisense keywords in R Studio.
To create snippets we have to head into “Global Options” under the “Tools” menu in R Studio. Snippets are available at the bottom of the code page.
There are some helpful snippets already built into R Studio. For example, “if” code blocks are available for the lazy and/or efficient programmer as are for loops.
To see how snippets work type “if” followed by the tab key, and hit enter. R Studio will then provide a completed if block.
Replacing the placeholders is as easy as typing and tabbing over to cycle through different points in the code.
if (condition) {
}
Creating Custom Snippets
Creating customized snippets is really easy, so easy in fact you’ll wonder why you didn’t do this years ago.
Snippets are defined with the keyword of the same name, followed by the snippet name you will call in R Studio. Then we simply type the template code and position our placeholders.
Placeholders follow the syntax ${1:PlaceHolderName} where the digit indicates which position the placeholder falls when tabbing through for replacement.
In the example below we have created a snippet called “muta” for a simple dplyr mutate function. This code can be inserted into the global options snippets.
snippet muta
mutate(${1:MyTibble}, ${2:NewVar} = ${3:Formula})
We can now test out our new muta snippet tabbing through the placeholders and entering some simple parameters. It’s a small thing, but we save several awkward keystrokes and at the same time can focus more on data wrangling and less on syntax.
We can also move on to some of the more obscure functions like dplyr with mutate across, or case_when and save even more keystrokes.
library(tidyverse)
MyFrame <- data.frame(
A <- c(1:10),
B <- c(20:29)
)
Result muta
Result <- mutate(MyFrame, C = A+B)
One thing that may apply to business users more so than academics is recurring label names like customer segmentation personas.
If we find ourselves having to type out the levels for various customer segmentation to keep them in order, we can just set up a handy snippet instead.
Simultaneous Placeholders
Another useful feature of snippets is that any placeholder that has to be repeated can be typed in multiple places at the same time. To do this, simply assign the same placeholder number to the second variable.
In our example, if we use the snippet below we can type once, and enter the tibble name in both locations. This can be a huge time saver for functions like case_when or plotting in ggplot or base.
snippet muta
mutate(${1:MyTibble}, ${1:MyTibble} = ${3:Formula})
Option Two: .Rprofile
The R Profile “.Rprofile” is just a script which is sourced when you open a project. There are several options for profile files, including a site-wide version that we will not be discussing.
For individuals, we have the option to use a “user” profile across all projects, or a project file for a single project. Although it might seem like a good idea to have a file across all projects, if we share our code it might not work on another system.
This means we have to take some care in deciding what goes into one or both profile files. We prefer to use project files and copy them between projects so they are easily available for upload to github but your mileage may vary. You may prefer to load common packages in the user file, and specific functions to the project file.
Note that R Studio will only source a single .Rprofile so if you have a user and project file, you will have to manually source the project file.
Creating .Rprofile
A profile can be created with the following code:
usethis::edit_r_profile("project")
## This option is for user files
## usethis::edit_r_profile("user")
Running this code results in a blank .Rprofile popping up in the file view of R Studio.
This can be edited as any other file in the text editor. We will add a few common options and auto load the Tidyverse packages because there is a 99% chance we will be using these packages in any given project.
options(scipen = 999)
options(StringsAsFactors = F)
library(tidyverse)
Once this is saved we can restart R Studio and it will dutifully load the options and package for us. We could of course also add customized functions that make our life easier.
Excluding R Profile
In case we plan to share out our code and need it to be reproducible we can disable loading of the .Rprofile files for the specific project under the “Tools” menu in “Project Options.”
Sourcing R Profile
If we have a user level .Rprofile then the project file has to be sourced on load, this can be accomplished with a single line of code.
source(".Rprofile")
Darker Themes in R Studio
R Studio is a lovely IDE but it comes with an eye watering white default theme. Fortunately, this can be changed easily and your pupils will thank you for it.
In “Tools” locate the “Global Options” and then “Appearance.”
There are a plethora of great colour themes to choose from, but a personal favourite is “Pastel On Dark.”
Conclusion
A more comfortable programmer is a more productive programmer.
Those us who spend many hours starring at R Studio furiously typing out code can benefit from some of the features available in R Studio designed to streamline coding and make life more comfortable. Give them a try if you haven’t already, your wrist and eyes will thank you for it!
Recent Post
Streamline Workflows in R Studio
- 23 November 2024
- 6 min read
Customer Clusters with Gaussian Mixed Models
- 22 October 2024
- 8 min read
Text Sentiment Analysis with Hugging Face
- 28 September 2024
- 4 min read
Product Graph Analytics
- 21 August 2024
- 11 min read
MLR3 Pipeline Transformations
- 18 August 2024
- 6 min read