class: title-slide, left, middle background-image: url("images/light-bulb-icon.jpg") background-position: right background-size: contain background-color: #7899d4ff .pull-left[ # 20 random tips and tricks for working with R, Rmarkdown, and RStudio ## ISU Graphics Group Meeting ### Ashirwad Barnwal ### 2021-09-09 ] --- class: middle, center, inverse background-image: url("images/isu-campanile.jpg") background-position: right background-size: contain .pull-left[ ## .big-text[Welcome!] ] --- class: middle, center, inverse ### Ashirwad Barnwal <img style="border-radius: 50%;" src="https://ashirwad.netlify.app/authors/ashirwad/avatar.jpg" width="150px"/> ### Ph.D. Candidate (Civil) | M.S. Student (Statistics) ### Institute for Transportation (InTrans) [
@ashirwad](https://github.com/ashirwad) [
@ashirwad1992](https://www.linkedin.com/in/ashirwad1992/) [
ashirwad.netlify.app](https://ashirwad.netlify.app) --- class: top, left .frame[ # 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." > > .center[[R4DS](https://r4ds.had.co.nz/r-markdown.html#chunk-name)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/DxuT_qOev-I" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # Tip #02: If you don't like the default arguments in an R function, change it! ] .pull-left[ <img src="https://media0.giphy.com/media/Ssltx68WIeX1wA0Mg8/giphy-downsized.gif" width="65%" /> .footnote[Available at [*GIPHY*](https://gph.is/g/4zgwK8L)] ] .pull-right[ <blockquote class="twitter-tweet"><p lang="en" dir="ltr">Roses are red<br>Violets are blue <br>In R versions after 4.0.0<br>stringsAsFactors does not default to TRUE</p>— Kiegan Rice (@kieganer) <a href="https://twitter.com/kieganer/status/1361057477115203587?ref_src=twsrc%5Etfw">February 14, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/rgxkgv_bgHQ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # Tip #03: Don't import the whole namespace if all you need is a couple of functions ] <center> <blockquote class="twitter-tweet"><p lang="en" dir="ltr">FWIW you can accomplish something similar with base R tools now: e.g. `library(pkg, include.only = c("f1", "f2"))` will load <pkg> but only put functions f1 and f2 on the search path. Moral equivalent of "from pkg import f1, f2"?</p>— Kevin Ushey (@kevin_ushey) <a href="https://twitter.com/kevin_ushey/status/1281085273154048000?ref_src=twsrc%5Etfw">July 9, 2020</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> </center> --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/w0qWm9aHb9o" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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**." > > .center[[Hadley Wickham](https://conflicted.r-lib.org/)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/5vXWCBveUn4" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # Tip #05: Use `here::here()` for generating file paths ] .center[ > "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](https://www.tidyverse.org/blog/2017/12/workflow-vs-script/) ] --- class: middle, inverse background-image: url("images/here-function-drake-meme.png") background-position: right background-size: contain ## Thanks [@krlmlr](https://twitter.com/krlmlr?s=20)! <img style="border-radius: 50%;" src="https://unavatar.vercel.app/twitter/krlmlr" width="150px"/> --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/gock2RtsppY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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.**" > > .center[[Jim Hester](https://fs.r-lib.org/index.html)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/fdrfAv9QC7c" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # Tip #07: Use `fs::file_show()` to open files or directories ] > `file_show(path = ".", browser = getOption("browser"))` > > .center[[function reference](https://fs.r-lib.org/reference/file_show.html)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/_s7zlOAJuhI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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)." > > .center[[rstudio.github.io](https://rstudio.github.io/visual-markdown-editing/?_ga=2.27003637.974706172.1630717819-1281065507.1590280001)] --- class: middle, center ![visual-markdown](https://blog.rstudio.com/2020/09/30/rstudio-v1-4-preview-visual-markdown-editing/images/visualmode-demo.gif) .footnote[[Source](https://blog.rstudio.com/2020/09/30/rstudio-v1-4-preview-visual-markdown-editing/images/visualmode-demo.gif)] --- class: top, left .frame[ # 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." > > .center[[mathpix.com](https://mathpix.com/)] --- class: middle, center <iframe src="https://mathpix.com/videos/hero-video-snip.mp4" width="100%" height="600px"></iframe> .footnote[[Source](https://mathpix.com/)] --- class: top, left .frame[ # Tip #10: Use TablesGenerator for generating markdown tables ] > "Quickly and easily generate markup for tables in LaTeX, HTML, plain text, Markdown and MediaWiki formats." > > .center[[onethingwell.org](https://onethingwell.org/post/75483091484/tablesgenerator)] --- class: middle, center <iframe src="https://www.tablesgenerator.com/markdown_tables" width="100%" height="600px"></iframe> --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/t94A96fQnLU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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**." > > .center[[Christopher Chizinski](https://chrischizinski.github.io/rstats/using_geom_blank/)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/0UQbJLptZUo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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." > > .center[[Stefano Meschiari](https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/6YU8pm_X6qI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # Tip #13: Use `scales::show_col()` function to preview colors in RStudio viewer ] .pull-left[ <blockquote class="twitter-tweet"><p lang="en" dir="ltr">scales::show_col() <a href="https://t.co/El8891gLgV">https://t.co/El8891gLgV</a></p>— tj mahr 🍍🍕 (@tjmahr) <a href="https://twitter.com/tjmahr/status/1374423244028014602?ref_src=twsrc%5Etfw">March 23, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ] .pull-right[ <blockquote class="twitter-tweet"><p lang="en" dir="ltr">I think scales::show_col() is what you're looking for<a href="https://t.co/D3GwydYAKD">https://t.co/D3GwydYAKD</a></p>— Jake Rothschild (@jake_rothschild) <a href="https://twitter.com/jake_rothschild/status/1420546678034247686?ref_src=twsrc%5Etfw">July 29, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/vvW8zAR92X8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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." > > .center[[Charlie Sharpsteen](https://daqana.github.io/tikzDevice/index.html)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/RlDCQYuF_GI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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**." > > .center[[Barry Rowlingson](https://github.com/yihui/knitr/issues/238#issue-4608247)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/2PHxvxuQrH8" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # Tip #16: Use crop and optipng knitr chunk options for cropping and optimizing png plots ] .pull-left[ <blockquote class="twitter-tweet"><p lang="en" dir="ltr"><a href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw">#rstats</a> question:<br><br>How do crop unneeded white space from a ggplot? Plot is going into an RMarkdown PDF and has a lot of white space above and below it.</p>— Patrick (@pjames122) <a href="https://twitter.com/pjames122/status/1031992769127100418?ref_src=twsrc%5Etfw">August 21, 2018</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ] .pull-right[ <blockquote class="twitter-tweet"><p lang="en" dir="ltr">Reduce <a href="https://twitter.com/hashtag/knitr?src=hash&ref_src=twsrc%5Etfw">#knitr</a> output file size by automatically optimizing images: <a href="https://t.co/wCArERuhoF">https://t.co/wCArERuhoF</a> <a href="https://twitter.com/hashtag/Rmarkdown?src=hash&ref_src=twsrc%5Etfw">#Rmarkdown</a> <a href="https://twitter.com/hashtag/Rstats?src=hash&ref_src=twsrc%5Etfw">#Rstats</a> <a href="https://twitter.com/hashtag/pngquant?src=hash&ref_src=twsrc%5Etfw">#pngquant</a> <a href="https://twitter.com/hashtag/optipng?src=hash&ref_src=twsrc%5Etfw">#optipng</a></p>— Kamil Slowikowski (@slowkow) <a href="https://twitter.com/slowkow/status/798614606377783296?ref_src=twsrc%5Etfw">November 15, 2016</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/XeYx4sUu7Xw" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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." > > .center[[Miles McBain](https://milesmcbain.github.io/datapasta/)] --- class: middle, center <img src="https://raw.githubusercontent.com/milesmcbain/datapasta/master/inst/media/tribble_paste.gif" width="85%" /> .footnote[[Source](https://raw.githubusercontent.com/milesmcbain/datapasta/master/inst/media/tribble_paste.gif)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/MWcsq72RZtE" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # Tip #18: Use rio package for data import and export ] > "A Swiss-Army Knife for Data I/O" > > .center[[Chung-hong Chan](https://cloud.r-project.org/web/packages/rio/vignettes/rio.html)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/nfKi_O6hJbc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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)**." > > .center[[From SO question by donodarazao](https://stackoverflow.com/q/5869539/10789048)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/c3mx-62uc70" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, left .frame[ # 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." > > .center[[From Dr Timothy S Farewell's blog](https://timfarewell.co.uk/my-r-script-header-template/#:~:text=Open%20RStudio,of%20the%20snippets%20code%20block.)] --- class: middle, center <iframe width="1120" height="630" src="https://www.youtube.com/embed/ywbgYS6Z_Hc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> --- class: top, center .frame[ # Happy Fall Y'all! ] .pull-left[ <img src="https://media4.giphy.com/media/ONxw4niC96zwk/giphy.gif" width="100%" /> .left[.footnote[Available at [*GIPHY*](https://media.giphy.com/media/ONxw4niC96zwk/giphy.gif?cid=790b76118d57c7a8946002fb40b5af348500a765f5bbb75d&rid=giphy.gif&ct=g)]] ] .pull-right[ <img src="https://media0.giphy.com/media/4TTqqym8oj0Q0/giphy.gif" width="100%" /> .left[.footnote[Available at [*GIPHY*](https://media.giphy.com/media/4TTqqym8oj0Q0/giphy.gif?cid=790b761190c3536ca9003bb4ce7ca3ec12008603a5494af8&rid=giphy.gif&ct=g)]] ] --- class: bottom background-color: #fad662 .pull-left[ ### A big thanks to: + Yihui Xie for [xaringan](https://github.com/yihui/xaringan), + Alison Hill for [slide layout](https://github.com/apreshill/talks/tree/master/ares-kind-tools), + [Ganesh Krishnan](https://www.linkedin.com/in/ganeshkrishnann/) and [ISU Graphics Group](https://isu-graphics.rbind.io/) for the talk invitation <br> <br> <br> <br> ### GitHub repo: [
](https://github.com/ashirwad/isu-graphics-rtips) | License: [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) ] .pull-right[ .right[ <img style="border-radius: 50%;" src="https://ashirwad.netlify.app/authors/ashirwad/avatar.jpg" width="150px"/> # Thank you! ### Connect with me here... [
@ashirwad](https://github.com/ashirwad) [
@ashirwad1992](https://www.linkedin.com/in/ashirwad1992/) [
ashirwad.netlify.app](https://ashirwad.netlify.app) ] ]