Ten random useful things in R that you might not know about | by Keith McNulty | Towards Data Science

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.
I LOVE 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:
data <- read.csv(  switch(animal,          "dog" = "dogdata.csv",          "cat" = "catdata.csv",         "rabbit" = "rabbitdata.csv"))
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.
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.
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 flexdashboard.
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:
output$go_button <- shiny::renderUI({  # only display button if an animal input has been chosen    shiny::req(input$animal)  # display button  shiny::actionButton("go",                       paste("Conduct", input$animal, "analysis!")   )})
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:
# get csv input fileinFile <- input$file1data <- inFile$datapath# render table only if it is dogsshiny::renderTable({  # check that it is the dog file, not cats or rabbits  shiny::validate(    need("Dog Name" %in% colnames(data)),    "Dog Name column not found - did you load the right file?"  )  data})
For more on these function see my other article here.
If you are sharing code that requires login credentials to databases and the like, you can use the .Reviron file to avoid posting those credentials to Github or other spaces where they might be at risk. .Renviron is a file where you can store important environment variables, and is easily editable using the function edit_r_environ() inside the usethis package. As an example, you can set an alias for your remote database credentials in .Renviron, for example:
  DSN = "database_name",  UID = "User ID",  PASS = "Password"
Then in your shared script, you can call these variables. For example:
db <- DBI::dbConnect(  drv = odbc::odbc(),  dsn = Sys.getenv("DSN"),  uid = Sys.getenv("UID"),  pwd = Sys.getenv("PASS"))
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.
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:
---title: "Animal Analysis"author: "Keith McNulty"date: "21 March 2019"output:  html_document:    code_folding: "hide"params:  animal_name:    value: Dog    choices:      - Dog      - Cat      - Rabbit  years_of_study:    input: slider    min: 2000    max: 2019    step: 1    round: 1    sep: ''    value: [2010, 2017]---
Now you can write these variables into the R code in your document as params$animal_name and 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 revealjs
---title: "Exporing the Edge of the People Analytics Universe"author: "Keith McNulty"output:  revealjs::revealjs_presentation:    center: yes    template: starwars.html    theme: blackdate: "HR Analytics Meetup London - 18 March, 2019"resource_files:- darth.png- deathstar.png- hanchewy.png- millenium.png- r2d2-threepio.png- starwars.html- starwars.png- stormtrooper.png---
and here’s an example page. You can find the code here and the presentation here.
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.
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.