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.
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.
**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
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.
ctrl
+enter
runs current line or expression
(lines bound by %>%
)alt
+-
inserts <-
ctrl
+D
deletes the current linealt
+M
inserts %>%
alt
+up
/down
moves the current
line/statement up or downctrl
+alt
+up
/down
copies the current line/statement above or belowctrl
+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 Rmarkdownctrl
+alt
+N
runs next chunk in
Rmarkdownctrl
+alt
+P
runs all previous
chunk in Rmarkdownctrl
+alt
+I
inserts new chunk
in Rmarkdownctrl
+click
on a web link to open it in
browserPress ctrl
+alt
+K
in RStudio
for an overview of shortcuts.
They can also be customised.
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')
.
Four resources:
r
tag; likewise, individual packages have their own tags.When asking for help, provide a minimal reproducible example if at all possible.
usethis
package: utilities for easy code
developmentMostly 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 codeuse_git_*
to quickly set up or configure a git repo in
your project, link to Github etc.dplyr::tally(weight = )
/
dplyr::add_tally(weight = )
skimr::skim(df)
/
df %>% group_by(var) %>% skim()
ggplot2
See the recent R Journal article for an overview of packages for automated EDA.
data.table::fread()
for faster CSV readingvroom
for fast
CSV reading of large datasetsreadr::read_csv2()
for reading semicolon-separated CSVs
with decimal commareadr::guess_encoding()
if the text comes in
garbled.haven
for importing SPSS, STATA, SAS filesdata.table
can be
worth the speed improvement, and dtplyr
and
tidytable
provide tidy “front ends” to it.readr::write_excel_csv[2]()
for Excel-friendly
CSVsvalidate
,
assertr
,
pointblank
,
dlookr
janitor
readr::type_convert()
: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.
https://geocompr.robinlovelace.net
CzechData
-
recommended:
There is also RCzechia
,
which is on CRAN.
pragr
provides utilities for mapping Prague.
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.
tsibble
, feasts
,
fable
- see tidyverts.org; brolgar
Two things:
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
.
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.
Have a look at bookdown
usethis
package: utilities for easy code
development