10 useful R features you might not know about
- Transfer

The R is full of various functions. Below I will give ten of the most interesting of them, which many might not know about. The article appeared after I discovered that my stories about some of the R features that I use in my work are enthusiastically accepted by familiar programmers. If you already know everything about it, then I apologize for the time spent. At the same time, if you have something to share, advise something useful in the comments.
Skillbox recommends: Practical course «Python-developer" .
We remind you: for all readers of “Habr” - a discount of 10,000 rubles when registering for any Skillbox course using the “Habr” promo code.
Switch function
I really, really like switch (). In fact, this is a convenient abbreviation of the if statement when choosing a value according to the value of another variable. I find this especially useful when writing code that should load a specific data set according to a previous choice. For example, if there is a variable named animal and you need to select a specific data set depending on whether the animal is a dog, cat or rabbit, write this:
data <- read.csv (
switch (animal,
“dog” = “dogdata.csv ”,
“ Cat ”=“ catdata.csv ”,
“ rabbit ”=“ rabbitdata.csv ”)
)
This feature will be useful in Shiny applications where you need to load different data sets or environment files depending on one or several entry menu items .
Keyboard Shortcuts for RStudio
This hack is not so much for R as for RStudio IDE. Nevertheless, hot keys are always very convenient, allowing you to save time when entering text. My favorites are Ctrl + Shift + M for the%>% operator and Alt + for the <- operator.
To view all the hot keys, just press Alt + Shift + K in RStudio.
Flexdashboard package
When you need to quickly launch the Shiny control panel, there is nothing better than the dashboard package. It provides the ability to work with HTML shortcuts, which in turn make it easy to create sidebars, rows and columns easily and without problems. There is also the ability to use the title bar, which allows you to place it on different pages of the application, leave icons, shortcuts on Github, email addresses and much more.
The package allows you to work within the framework of Rmarkdown, so that you can place all applications in one Rmd file, rather than distributing them to different servers and UI files, as is done, for example, using shinydashboard. I use flexdashboard whenever I need to create a simple prototype control panel before starting to work on something complicated. This feature allows you to create a prototype in an hour.
Req and validate functions in R Shiny
Development in R Shiny can confuse you, especially when you constantly get strange error messages that make it impossible to understand what is happening. But over time, Shiny develops and improves, more and more functions appear here that allow you to understand the cause of the error. So, req () solves the problem with a “silent” error, when in general it is not clear what is the matter. With it, you can display the user interface elements associated with previous actions. Let’s explain with an example:
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 everything before rendering and allows you to display an error message - for example, that the user uploaded the wrong file:
# get csv input file
inFile <- input $ file1
data <- inFile $ datapath
# render table only if it is dogs
shiny :: 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 information about all these functions , see here .
Storing your credentials for yourself in the system environment
If you plan to share the code where you want to enter access data, use the system environment in order to avoid placing your own credentials in Github or another service. Placement example:
Sys.setenv (
DSN = "database_name",
UID = "User ID",
PASS = "Password"
)
Now you can log in using environment variables:
db <- DBI :: dbConnect (
drv = odbc :: odbc () ,
dsn = Sys.getenv ("DSN"),
uid = Sys.getenv ("UID"),
pwd = Sys.getenv ("PASS")
)
It is even more convenient (especially if you use data frequently) to set them as environment variables directly in the operating system. In this case, they will always be available and you will not have to specify them in the code.
Tidyverse automation with styler
The styler package can help to clean the code, which has many features for automatically converting the code style to tidyverse. All that is needed for this is to run styler :: style_file () for your problematic script. The package will do a lot (but not all) in order to restore order.
Document Parameterization R Markdown
So, you have created an excellent R Markdown document in which you analyze various facts about dogs. And then it occurs to you that it would be better to do the same work, but only with cats. It's okay, you can automate the reporting of cats with just one command. To do this, you only need to parameterize your R markdown document.
You can do this by setting parameters for the YAML header in the specified document, and then value parameters.
- 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 all the variables in the document code as params $ animal_name and params $ years_of_study. Then we will use the Knit drop-down menu (or knit_with_parameters ()) and get the opportunity to select parameters.

revealjs
revealjs - a package that allows you to create great HTML-presentations with built-in R-code, intuitive navigation and slide menus. HTML shortcuts allow you to quickly create a nested slide structure with different styles. Well, HTML will run on any device, so the presentation can be opened on every phone, tablet or laptop. Information disclosure can be configured by installing the package and calling it in the YAML header. Here is an example:
- title: “Exporing the Edge of the People Analytics Universe”
author: “Keith McNulty”
output:
revealjs :: revealjs_presentation:
center: yes
template: starwars.html
theme: black
date: “HR Analytics Meetup London - March 18 , 2019 »
resource_files:
- darth.png
- deathstar.png
- hanchewy.png
- millenium.png
- r2d2-threepio.png
- starwars.html
- starwars.png
- stormtrooper.png
---
The presentation source code is located here , and rpubs.com/keithmcnulty/hr_meetup_london itself > presentation is here.

HTML tags in R Shiny
Most programmers do not take full advantage of the HTML tags that R Shiny has. But these are just 110 tags that make it possible to create a short call for an HTML function or media playback. For example, I recently used tags $ audio to play a “winning” sound, which warned the user about the completion of a task.
Praise package
Using this package is very simple, but you need it to display praise to the user. It seems strange, but they really like it.

Skillbox recommends:
- Two-year practical course “I am a PRO Web Developer” .
- Online course "C # -developer" .
- Practical annual course "PHP-developer from 0 to PRO" .