class: title-slide, inverse, bottom background-image: url(https://images.unsplash.com/photo-1613117943693-9864648f7cb5?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1500&q=80) background-size: cover # Filling your bag of workflow tricks ## R-Ladies Dammam ### María Paula Caldas ### 2021-06-09 .right[ Photo by [Didssph](https://unsplash.com/photos/Q9E4de1OxFk) ] --- class: about-me, right, bottom <img style="border-radius: 50%;" src="https://github.com/mpaulacaldas.png" width="150px"/> ## María Paula Caldas [
mpaulacaldas.com](https://mpaulacaldas.com)<br/> [
mapaulacaldas](https://twitter.com/mapaulacaldas)<br/> [
mpaulacaldas](https://github.com/mpaulacaldas)<br/> --- class: left, middle # Links ### Slides: <https://mpaulacaldas.github.io/r-ladies-workflow> ### Github repo: <https://github.com/mpaulacaldas/r-ladies-workflow> --- class: inverse, left, middle # 👩💻 What are workflows? --- class: middle A workflow is about the _**way**_ I work in projects, either by myself or with others. --- class: middle ## It can be about the way I... -- - Organise my folders -- - Name my files -- - Share my work -- - Handle repetitive tasks -- ## Ultimately, R is also a __workflow tool__\* -- .small[ \* Credit to [David Keyes](https://rfortherestofus.com/2019/09/r-workflow-tool/) ] --- class: inverse, left, middle # Workflows are a popular topic in the R universe --- ## About topics that we all thought we understood... .pull-left[ The **three fundamental rules of file names**: 1. Machine readable 1. Human readable 1. Play well with default ordering ] .pull-right[ <script async class="speakerdeck-embed" data-slide="1" data-id="5e4b07f0d9a94f8e9a29b902bad6ed0b" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"></script> ] --- ## About things that in retrospect sound obvious... .pull-left[ - Set-ups that encourage reproducibility. - Project-oriented workflows. - Safe paths. <br/> 🎥 Keynote materials of [Zen and the art of workflow maintenance ](https://github.com/jennybc/zen-art-workflow) 📚 Section **A holistic workflow** from [What they forgot to teach you about R](https://rstats.wtf/save-source.html) ] .pull-right[ <script async class="speakerdeck-embed" data-slide="1" data-id="b3fffa871213414491f868936df0633b" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"></script> ] --- background-image: url(https://github.com/allisonhorst/stats-illustrations/blob/master/rstats-artwork/usethis.png?raw=true) background-position: 500px 150px background-size: 700px ## Many tools to improve specific workflows... .pull-left[ [**usethis**](https://usethis.r-lib.org/) to automate repetitive tasks during project setup and development [**golem**](https://thinkr-open.github.io/golem/) for Shiny apps [**targets**](https://github.com/ropensci/targets) for reproducible pipelines ] .footnote[ Artwork by [@allison_horst](https://twitter.com/allison_horst/) ] --- class: inverse, bottom, left # ✨ Trick #1 ## Customise how R works for you --- ## Customise R Startup .pull-left[ ### `.Renviron` Contains environment variables to be set in R sessions. - API keys - R-specific environment variables ```r usethis::edit_r_environ() ``` ] .pull-right[ ### `.Rprofile` Contains R code run at the start of every session. - Packages that you use interactively - Global options ```r usethis::edit_r_profile() ``` ] .footnote[ 📖 [What they forgot to teach you about R](https://rstats.wtf/r-startup.html), by Jenny Brian and Jim Hester ] --- ### Extract from my `.Rprofile` ```r if (interactive()) { # packages I use interactively suppressMessages(require(devtools)) suppressMessages(require(testthat)) # to know the name of my branch if using git prompt::set_prompt(prompt::prompt_git) # because I use it all the time suppressMessages(require(datapasta)) } # see ?shiny::`shiny-options` options( shiny.launch.browser = FALSE, shiny.port = 1234 ) ``` .footnote[ 📦 [`rprofile`](https://github.com/csgillespie/rprofile), by Colin Guillespie ] --- class: inverse, bottom, left # ✨ Trick #2 ## Customise the RStudio defaults... repeatedly --- background-image: url(screenshots/rdata.png) background-position: 550px 50px background-size: 600px .pull-left[ ## Customise the RStudio defaults <br/> Relying on .RData files hinders **reproducibility** Avoid pain when R crashes ] --- background-image: url(screenshots/utf.png) background-position: 550px 50px background-size: 600px .pull-left[ ## Customise the RStudio defaults <br/> Save scripts with UTF-8 encoding ] --- ## Customise the RStudio defaults .pull-left[ As of **RStudio 1.3**, RStudio preferences are stored in: ``` ~/.config/rstudio/rstudio-prefs.json ``` These can then be saved in **dotfiles** that you can use to set-up other computers. ] .pull-right[ ```json { "save_workspace": "never", "load_workspace": false, "initial_working_directory": "~/Desktop", "posix_terminal_shell": "bash", "restore_last_project": false, "restore_source_documents": false, "default_project_location": "~/Desktop", "spelling_dictionary_language": "en_GB", "document_author": "María Paula", "rmd_viewer_type": "pane", "default_encoding": "UTF-8" } ``` ] .footnote[ 📝 https://blog.rstudio.com/2020/02/18/rstudio-1-3-preview-configuration ] --- class: inverse, bottom, left # ✨ Trick #3 ## Use the shortcuts! --- .pull-left[ <br/> <br/> <br/> As of **RStudio 1.4** `Ctrl+Shift+P` to launch the command palette .footnote[_This functionality also exists in other IDEs!_] ] .pull-right[ <blockquote class="twitter-tweet tw-center-align" data-conversation="none"><p lang="en" dir="ltr">That's easy, Cmd/Control + Shift + P for the command palette! (And then you don't have to remember any more shortcuts!)<br><br>[gif: Demo opening Command Palette in RStudio] <a href="https://t.co/gYA8N1TpI2">pic.twitter.com/gYA8N1TpI2</a></p>— Garrick Aden-Buie (@grrrck) <a href="https://twitter.com/grrrck/status/1378799812183793670?ref_src=twsrc%5Etfw">April 4, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ] --- ## Map your own shortcuts R addins can be mapped to keyboard shortcuts. Dean Attali has a [curated list](https://github.com/daattali/addinslist) of packages with addins. My favourites include: - [styler](https://github.com/r-lib/styler) - [xaringan](https://github.com/yihui/xaringan) - [remedy](https://github.com/ThinkR-open/remedy) - [WrapRmd](https://github.com/tjmahr/WrapRmd) Under *Tools > Modify keyboard shortcuts* .footnote[ 📝 https://support.rstudio.com/hc/en-us/articles/206382178-Customizing-Keyboard-Shortcuts ] --- class: inverse, bottom, left # ✨ Trick #3 ## Use snippets --- ## Use snippets... or create your own Under *RStudio > Preferences > Code > Snippets > Edit snippets* .pull-left[ ### R ``` snippet postgis library(DBI) library(RPostgres) con <- DBI::dbConnect( RPostgres::Postgres(), dbnmae = "my_db", host = "myhost.org" ) ``` ] .pull-right[ ### Markdown ``` snippet dets <details><summary> ${1:summary} </summary> ${2:contents} </details> ``` ] .footnote[ 📝 [Code Snippets in the RStudio IDE](https://support.rstudio.com/hc/en-us/articles/204463668-Code-Snippets) ] --- class: inverse, bottom, left # ✨ Trick #5 ## Automate project set-up --- ## Automate project set-up with `usethis` and `fs` .pull-left[ - `usethis` can be useful in all kinds of projects, beyond R packages. - For package development, see [The whole game](https://r-pkgs.org/whole-game.html) chapter in the **R packages** book. - For Shiny apps, see the [golem](https://thinkr-open.github.io/golem/articles/a_start.html) framework. - `fs` is helps create, delete and modify files. It is robust and has some good defaults. ] .pull-right[ ```r # Creates and switches you to project create_project("myproject") # Add documentation use_readme_md() # Set up git use_git() use_github() # Create directories fs::dir_create(c("data", "data-raw", "docs")) # Create and open up a file file.edit("data-raw/README.md") ``` ] --- class: inverse, bottom, left # ✨ Trick #6 ## Get the little things done from R (My current favourites) --- ### Navigate to websites or directories ```r browseURL("https://twitter.com/rladiesdammam") ``` ### Download files ```r usethis::use_zip("bit.ly/usethis-shortlink-example") # alternatively, usethis::use_course() is chattier ``` ### Lookup the source code ```r lookup::lookup(mean) ``` --- class: inverse, middle, left background-image: url(https://images.unsplash.com/photo-1613117943693-9864648f7cb5?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1500&q=80) background-size: cover # Thank you! ## Any questions?