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+enterruns current line or expression (lines bound by%>%) -
alt+-inserts<- -
ctrl+Ddeletes the current line -
alt+Minserts%>% -
alt+up/downmoves the current line/statement up or down -
ctrl+alt+up/downcopies the current line/statement above or below -
ctrl+shift+Rinserts section break in R code (visible in document outline which you can turn on in the top right corner of the code pane) -
ctrl+F10restarts R (do it often!)
In RMarkdown:
-
ctrl+shift+enterruns current chunk in Rmarkdown -
ctrl+alt+Nruns next chunk in Rmarkdown -
ctrl+alt+Pruns all previous chunk in Rmarkdown -
ctrl+alt+Iinserts new chunk in Rmarkdown -
ctrl+clickon 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:
- Google the error message or its more generic variant if you can figure it out. This may bring you to the other resources.
- StackExchange: filter for the
rtag; likewise, individual packages have their own tags. - The RStudio Community
- 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 -
vroomfor 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. -
havenfor 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
- see googlesheets4
Data manipulation
- for heavy-duty data manipulation,
data.tablecan be worth the speed improvement, anddtplyrandtidytableprovide tidy “front ends” to it.
Data checking
- packages
validate,assertr,pointblank,dlookr
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.
Modeling
- broom for extracting model parameters in a consistent way
- tidymodels for tidy modeling generally
Spatial data
https://geocompr.robinlovelace.net
Displaying geospatial data and making maps
- see Making charts vignette
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.
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
- packages
tsibble,feasts,fable- see tidyverts.org;brolgar
Getting stuff on the web
Two things:
Any Rmarkdown document, unless you have tons of LaTex-specific stuff in it, should render into html; just switch the
outputargument in the YAML header tohtml_document.Netlify is an amazing service that lets you quickly get any website to [something].netlify.com.
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