Ten random useful things in R that you might not know about
Because the R ecosystem is so rich and constantly growing, people can often miss out on knowing about something that can really help them in a task that they have to complete
By Keith McNulty, McKinsey & Company
Often I find myself telling my colleagues and fellow coders simple things that I use in R that really help me with the tasks that I need to progress on. These range from trivial shortcuts, to little known functions, to handy little tricks.
Because the R ecosystem is so rich and constantly growing, people can often miss out on knowing about something that can really help them in a task that they have to complete. So I often get a surprised reaction from my audience along the lines of ‘I never knew about that!’.
Here are ten things that make my life easier working in R. If you already know them all, sorry for wasting your reading time, and please consider adding a comment with something else that you find useful for the benefit of other readers.
switch(). It’s basically a convenient shortening of an
if statement that chooses its value according to the value of another variable. I find it particularly useful when I am writing code that needs to load a different dataset according to a prior choice you make. For example, if you have a variable called
animal and you want to load a different set of data according to whether
animal is a dog, cat or rabbit you might write this:
This is particularly useful in Shiny apps where you might want to load different datasets or even environment files depending on one or more of the input menu choices.
2. RStudio shortcut keys
This is less an R hack and more about the RStudio IDE, but the shortcut keys available for common commands are super useful and can save a lot of typing time. My two favourite are Ctrl+Shift+M for the pipe operator
%>% and Alt+- for the assignment operator
<-. If you want to see a full set of these awesome shortcuts just type Atl+Shift+K in RStudio.
3. The flexdashboard package
If you want to get a quick Shiny dashboard up and running with a minimum of fuss, the
flexdashboard package has everything you need. It provides simple HTML shortcuts that allow easy construction of sidebars and the organization of your display into rows and columns. It also has a super flexible title bar where you can organize your app into different pages and put in icons and links to Github code or an email address or whatever. As a package which operates within
RMarkdown it also allows you to keep all your app in one
Rmd file rather than needing to break it out into separate server and UI files like for example
shinydashboard. I use
flexdashboard whenever I need to create a simple prototype version of a dashboard before moving it on to more advanced design. I can often get dashboards up and running within an hour using
4. The req and validate functions in R Shiny
R Shiny development can be frustrating, especially when you get generic error messages that don’t help you understand what is going wrong under the hood. As Shiny develops, more and more validation and testing functions are being added to help better diagnose and alert when specific errors occur. The
req() function allows you to prevent an action from occurring unless a another variable is present in the environment, but does so silently and without displaying an error. So you can make the display of UI elements conditional on previous actions. For example, with reference to my example no 1 above:
validate() checks before rendering an output and enables you to return a tailored error message should a certain condition not be fulfilled, for example if the user uploaded the wrong file:
For more on these function see my other article here.
5. Keeping all my credentials to myself using the system environment
If you are sharing code that requires login credentials to databases and the like, you can use the system environment to avoid posting those credentials to Github or other spaces where they might be at risk. You can put credentials as named environment variables in your R session, for example:
Then in your shared script, you can log in using these environment variables. For example:
Even more handy, if these are credentials you use frequently, you can set them as environment variables in your operating system, so that they will always be available to you whenever you are working in R, but you’ll never have to display them in your code. (Just be careful about who has access to these!)
6. Automate tidyverse styling with styler
Its been a tough day, you’ve had a lot on your plate. Your code isn’t as neat as you’d like and you don’t have time to line edit it. Fear not. The
stylerpackage has numerous functions to allow automatic restyling of your code to match tidyverse style. It’s a simple as running
styler::style_file() on your messy script and it will do a lot (though not all) of the work for you.
7. Parameterizing R Markdown documents
So you write a lovely R Markdown document where you’ve analyzed a whole bunch of facts about dogs. And then you get told — ‘nah, I’m more interested in cats’. Never fear. You can automate a similar report about cats in just one command if you parameterize your R markdown document.
You can do this by defining parameters in the YAML header of your R Markdown document, and giving each parameter a value. For example:
Now you can write these variables into the R code in your document as
params$years_of_study. If you knit your document as normal, it will knit with the default values of these parameters as per the
value variable. However, if you knit with parameters by selecting this option in RStudio’s Knit dropdown (or by using
knit_with_parameters()), a lovely menu option appears for you to select your parameters before you knit the document. Awesome!
revealjs is a package which allows you to create beautiful presentations in HTML with an intuitive slide navigation menu, with embedded R code. It can be used inside R Markdown and has very intuitive HTML shortcuts to allow you to create a nested, logical structure of pretty slides with a variety of styling options. The fact that the presentation is in HTML means that people can follow along on their tablets or phones as they listen to you speak, which is really handy. You can set up a
revealjspresentation by installing the package and then calling it in your YAML header. Here’s an example YAML header of a talk I gave recently using
9. HTML tags in R Shiny (eg play audio in your Shiny app)
Most people don’t take full advantage of the HTML tags available in R Shiny. There are 110 tags which offer shortcuts to various HTML formatting and other commands. Recently I built a shiny app that took a long time to perform a task. Knowing that the user would likely multitask while waiting for it to complete, I used
tags$audio to have the app play a victory fanfare to alert the user when the task was complete.
10. The praise package
Ridiculously simple but also awesome, the
praise package delivers praise to users. While this can appear like pointless self-admiration, it’s actually super useful in writing R packages where you can offer praise or encouragement to someone if they do something right, for example if a process completes successfully. You can also just put it at the end of a complicated script to give you that extra shot of happiness when it runs successfully.
Originally I was a Pure Mathematician, then I became a Psychometrician and a Data Scientist. I am passionate about applying the rigor of all those disciplines to complex people questions. I’m also a coding geek and a massive fan of Japanese RPGs. Find me on LinkedIn or on Twitter.
Bio: Keith McNulty is a Data Scientist at McKinsey & Company.
Original. Reposted with permission.
- Top R Packages for Data Cleaning
- 2018’s Top 7 R Packages for Data Science and AI
- Automated Web Scraping in R