Tips and Tricks for R and Rstudio

This vignette provides some handy tips and tricks for working with R in RStudio. For more fundamental principles and guidance on getting R and RStudio setup for the Schola workflow, see the Setup vignette. For guidance on the workflow itself, see the Workflow vignette.

RStudio tips

All keyboard shortcuts below are for Windows. See https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts for all shortcuts on all platforms.

General helpers

**Code completion*: when typing a function name, press Tab and RStudio will show functions/objects which match that name. Use the arrows to navigate the entry you need and press Tab again. Likewise with parameters once you are inside parentheses. Note that you can type e.g. usegitrel to get a suggestion for use_github_release() as RStudio looks intelligently through functions names some_function_name(). (This is also a good reason to use underscores in your own object/function names).

Paths completion: when inside quotes, type or press tab and RStudio will suggest files inside the current project.

Go to anything: in RStudio, press ctrl+ ., and start typing. RStudio will suggest files and functions in this project and if you click/arrow+enter into one, will take you to the code.

Cheatsheets: in help > Cheatsheets, you can get to two-page cheatsheets on some of the most used packages. They are also online.

Multiple cursors: when making changes to similar lines, you can use multiple cursors: press alt and drag up or down or press ctrl+alt+up/down to add cursor in the above or below line. Press ctrl+alt and click to add cursors anywhere. Then type, move around, delete etc.; when done, press esc.

Object view: holding ctrl while clicking on any object name in the code editor, or pressing F2, lets you view that object: code for functions, dataset viewer for data.

Accessing help: pressing F1 when your cursor is on a function name will display the documentatin for that function, same for package names

Snippets

When you start typing, some of the suggestions are snippets: they are marked ‘snippet’ and have a paper snippet icon.

If you press tab, they insert bits (e.g. the snippet for %in% which appears when you type in) of code or mini-templates (e.g. the fun snippet). For the mini-templates, you can then Tab through the different bits to fill in.

You can also define your own.

Handy keyboard shortcuts

  • ctrl+enter runs current line or expression (lines bound by %>%)
  • alt+- inserts <-
  • ctrl+D deletes the current line
  • alt+M inserts %>%
  • alt+up/down moves the current line/statement up or down
  • ctrl+alt+up/down copies the current line/statement above or below
  • ctrl+shift+R inserts section break in R code (visible in document outline which you can turn on in the top right corner of the code pane)
  • ctrl+F10 restarts R (do it often!)

In RMarkdown:

  • ctrl+shift+enter runs current chunk in Rmarkdown
  • ctrl+alt+N runs next chunk in Rmarkdown
  • ctrl+alt+P runs all previous chunk in Rmarkdown
  • ctrl+alt+I inserts new chunk in Rmarkdown
  • ctrl+click on a web link to open it in browser

Press ctrl+alt+K in RStudio for an overview of shortcuts.

They can also be customised.

Retrieving documentation

Besides the F1 key, you can use ?function to get help for function and ??term to search for term in all documentation of all installed packages.

Longer-form documentation for packages is often found in vignettes. Run vignette('vignette-name', 'package-name') to get a particular one. (Intro vignettes often have the name of the package, e.g. vignette('reschola', 'reschola')). Get list of vignettes for any package browseVignettes('package-name').

Getting help beyond the documentation

Four resources:

  1. Google the error message or its more generic variant if you can figure it out. This may bring you to the other resources.
  2. StackExchange: filter for the r tag; likewise, individual packages have their own tags.
  3. The RStudio Community
  4. Twitter #rstats

When asking for help, provide a minimal reproducible example if at all possible.

The usethis package: utilities for easy code development

Mostly useful for package development, but some utilities also make your life easier in any project:

  • edit_* for shortcuts to editing setup files e.g. .Rprofile
  • ui_* for creating nice console output in your code
  • use_git_* to quickly set up or configure a git repo in your project, link to Github etc.

Data exploration

  • dplyr::tally(weight = ) / dplyr::add_tally(weight = )
  • skimr::skim(df) / df %>% group_by(var) %>% skim()
  • visdat for quick data overview
  • naniar for missing data analysis
  • ggcorrplot for correlation matrices in ggplot2
  • dlookr is a fairly complete toolbox for EDA and related tasks.

See the recent R Journal article for an overview of packages for automated EDA.

Data input

  • data.table::fread() for faster CSV reading
  • vroom for fast CSV reading of large datasets
  • readr::read_csv2() for reading semicolon-separated CSVs with decimal comma
  • readr::guess_encoding() if the text comes in garbled.
  • haven for importing SPSS, STATA, SAS files

From LimeSurvey

  • limer worked fine when I last used it, but it has been a while.

From Google sheets / Forms

Data manipulation

  • for heavy-duty data manipulation, data.table can be worth the speed improvement, and dtplyr and tidytable provide tidy “front ends” to it.

Data output

  • readr::write_excel_csv[2]() for Excel-friendly CSVs

Data checking

Data cleaning

  • package janitor
  • readr::type_convert():

Resources for different data types or analysis areas

This section points to packages and resources you can use in different parts of your analysis or to perform various specialised tasks.

CRAN’s task views can also be useful: they contain collections of packages organised by task.

Web scraping

  • rvest for a scraping tool
  • httr for a simple HTTP client

Modeling

  • broom for extracting model parameters in a consistent way
  • tidymodels for tidy modeling generally

Spatial data

sf + guides

https://geocompr.robinlovelace.net

Displaying geospatial data and making maps

Geodata on Czech Republic

CzechData - recommended:

  • provides access to all basic geospatial datasets for CZ (boundaries of admin units, topology of things such as rivers, roads, railways etc.)
  • has a set of codebooks and code conversion tables for spatial entities (municipalities etc.)
  • provides basic demographic data on municipalities
  • not on CRAN but stable and well maintained.

There is also RCzechia, which is on CRAN.

pragr provides utilities for mapping Prague.

Statistical data

  • eurostat: provides intuitive access from R to all Eurostat open data
  • oecd: ditto for OECD
  • czso: ditto for Czech Statistical Office

Czech public finance data

statnipokladna provides friction-free access to

For other related datasets, see Ondřej Kokeš’s od and knod (not R, more software-agnostic or Python) and the APIs by Hlídač státu and CityVizor.

Time series

Network data 

tidygraph ggraph for visualisation

Text data

tidytext

Getting stuff on the web

Two things:

  1. Any Rmarkdown document, unless you have tons of LaTex-specific stuff in it, should render into html; just switch the output argument in the YAML header to html_document.

  2. Netlify is an amazing service that lets you quickly get any website to [something].netlify.com.

  3. For public repositories on Github, you can use the docs/ folder to publish to scholaempirica.github.io/{reponame}, or you can use the gh-pages branch for that. See Github Pages.

Long-form, multipart reports

Have a look at bookdown