+ - 0:00:00
Notes for current slide
Notes for next slide

20 random tips and tricks for working with R, Rmarkdown, and RStudio

ISU Graphics Group Meeting

Ashirwad Barnwal

2021-09-09

Welcome!

Ashirwad Barnwal

Ph.D. Candidate (Civil) | M.S. Student (Statistics)

Institute for Transportation (InTrans)

@ashirwad
@ashirwad1992
ashirwad.netlify.app

Tip #01: Use setup label for your setup chunk

"There is one chunk name that imbues special behaviour: setup. When you’re in a notebook mode, the chunk named setup will be run automatically once, before any other code is run."

R4DS

Tip #02: If you don't like the default arguments in an R function, change it!

Available at GIPHY

Tip #03: Don't import the whole namespace if all you need is a couple of functions

Tip #04: Handle ambiguous function names elegantly using the conflicted package

"The goal of conflicted is to provide an alternative conflict resolution strategy. R’s default conflict resolution system gives precedence to the most recently loaded package. This can make it hard to detect conflicts, particularly when introduced by an update to an existing package. conflicted takes a different approach, making every conflict an error and forcing you to choose which function to use."

Hadley Wickham

Tip #05: Use here::here() for generating file paths

"If the first line of your R script is
setwd("C:\Users\jenny\path\that\only\I\have")
I will come into your office and SET YOUR COMPUTER ON FIRE πŸ”₯."

Jenny Bryan

Thanks @krlmlr!

Tip #06: Switch to fs package for file system operations

"fs functions smooth over some of the idiosyncrasies of file handling with base R functions:

  • Vectorization. All fs functions are vectorized, accepting multiple paths as input. Base functions are inconsistently vectorized."

Jim Hester

Tip #07: Use fs::file_show() to open files or directories

file_show(path = ".", browser = getOption("browser"))

function reference

Tip #08: Upgrade RStudio and start using the visual markdown editor

"Visual markdown editing is available in RStudio v1.4 or higher. You can download the latest version of RStudio here: https://rstudio.com/products/rstudio/download/."

"Markdown documents can be edited in either source or visual mode. To switch into visual mode for a given document, use the button at the top-right of the document toolbar (or alternatively the βŒƒβ‡§ F4 keyboard shortcut)."

rstudio.github.io

Tip #09: Use Mathpix Snip to generate LaTeX code for math equations

"Export images and PDFs to LaTex, DOCX, Overleaf, Markdown, Excel, ChemDraw and more, with our AI powered document conversion technology."

"Save time preparing scientific documents with Mathpix Snip."

mathpix.com

Tip #10: Use TablesGenerator for generating markdown tables

"Quickly and easily generate markup for tables in LaTeX, HTML, plain text, Markdown and MediaWiki formats."

onethingwell.org

Tip #11: Use geom_blank() for setting pretty axis limits for faceted plots

"Using facet_wrap() in ggplot2 is a great way to create multiple panelled plots. Though when I am running these, particularly on datasets with different scales, the axis are not as clean as I like."

Christopher Chizinski

Tip #12: Use latex2exp package to convert latex into plotmath expressions

"I find plotmath expressions to be quite opaque, while LaTeX is a de-facto standard for mathematical expressions, so [latex2exp] package might be useful to others as well."

Stefano Meschiari

Tip #13: Use scales::show_col() function to preview colors in RStudio viewer

Tip #14: Use tikzDevice package to generate tikz code for ggplot

"The tikzDevice package provides a graphics output device for R that records plots in a LaTeX-friendly format. The device transforms plotting commands issued by R functions into LaTeX code blocks. When included in a paper typeset by LaTeX, these blocks are interpreted with the help of TikZβ€”a graphics package for TeX and friends written by Till Tantau."

Charlie Sharpsteen

Tip #15: Use cache.extra knitr chunk option to enable cache dependency on a file

"If I have a chunk that reads a data file then modifying the data file doesn't invalidate the cached chunk."

Barry Rowlingson

Tip #16: Use crop and optipng knitr chunk options for cropping and optimizing png plots

Tip #17: Use datapasta package for copying and pasting data to and from R

"datapasta is about reducing resistance associated with copying and pasting data to and from R."

Miles McBain

Tip #18: Use rio package for data import and export

"A Swiss-Army Knife for Data I/O"

Chung-hong Chan

Tip #19: Add labels to factor levels using sjlabelled package for pretty printing

"There seems to be a difference between levels and labels of a factor in R. Up to now, I always thought that levels were the 'real' name of factor levels, and labels were the names used for output (such as tables and plots)."

From SO question by donodarazao

Tip #20: Use RStudio snippets to add header templates to R scripts

"Do you ever face a blank script, and not know where to start? This is one reason I find it useful to have a template to follow for my R scripts."

From Dr Timothy S Farewell's blog

Happy Fall Y'all!

Available at GIPHY

Available at GIPHY

A big thanks to:





GitHub repo: | License: CC BY-SA 4.0

Thank you!

Connect with me here...

@ashirwad
@ashirwad1992
ashirwad.netlify.app

Welcome!

Paused

Help

Keyboard shortcuts

↑, ←, Pg Up, k Go to previous slide
↓, β†’, Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow