layout: true <!-- this adds the link footer to all slides, depends on footer-small class in css--> <div class="footer-small"><span>https://github.com/mjfrigaard/csuc-data-journalism</div> --- name: title-slide class: title-slide, center, middle, inverse # The RStudio IDE #.fancy[using RStudio to manage code, data, and files] <br> .large[by Martin Frigaard] Written: September 30 2021 Updated: November 30 2021 .footer-large[.right[.fira[ <br><br><br><br><br>[Created using the "λέξις" theme](https://jhelvy.github.io/lexis/index.html#what-does-%CE%BB%CE%AD%CE%BE%CE%B9%CF%82-mean) ]]] --- class: center, middle # The RStudio IDE ### Managing your code, data, and files with RStudio --- class: left, top ## Change RStudio's Default Settings .leftcol[ #### Click on **Tools** > **Global Options...** #### - We want to uncheck "*Restore .RData into work space at start up*" #### - We also want to make sure we change "*Save work space to .Rdata on exit*" to "*Never*" ] .rightcol[ <img src="img/settings.png" width="200%" height="200%" style="display: block; margin: auto;" /> ] --- class: inverse, center, middle # Customize RStudio .leftcol[ ### Code ### Console ### Appearance ### Pane Layout ### R Markdown ] .rightcol[ <img src="img/setting-options.png" width="40%" height="40%" style="display: block; margin: auto;" /> ] --- class: left, top # Code Editing .leftcol[ ## - Auto indent? ## - Continue comment lines? ## - Save R scripts before sourcing? ] .rightcol[.border[ <img src="img/code-settings.png" width="64%" height="64%" style="display: block; margin: auto;" /> ] ] --- class: left, top # Code Display .leftcol[ ## - Margins? ## - Scrolling? ## - Rainbow parentheses? ] .rightcol[.border[ <img src="img/code-display.png" width="70%" height="70%" style="display: block; margin: auto;" /> ] ] --- class: left, top # Code Saving .leftcol[ ## - Cursor position? ## - Line endings? ## - Text encoding? ] .rightcol[.border[ <img src="img/code-savings.png" width="80%" height="80%" style="display: block; margin: auto;" /> ]] --- class: left, top # Code Completion .leftcol[ ## - Insert parentheses? ## - Insert spaces? ## - Completion delay setting? ] .rightcol[.border[ <img src="img/code-completion.png" width="70%" height="70%" style="display: block; margin: auto;" /> ]] --- class: left, top # Code Diagnostics .leftcol[ ## - Check your R Code? ## - Check other languages? ## - How long? ] .rightcol[.border[ <img src="img/code-diagnostics.png" width="70%" height="70%" style="display: block; margin: auto;" /> ]] --- class: left, top # Console .leftcol[ ## - Display? ## - Debugging? ## - Other? ] .rightcol[.border[ <img src="img/console.png" width="70%" height="70%" style="display: block; margin: auto;" /> ]] --- class: left, top # Appearance .leftcol[ #### - RStudio theme? #### - Zoom? ##### - Also hold `⌘` and press `+` on macOS ##### - Also hold `ctrl` and press `+` on Windows #### - Font? #### - Editor theme? ] .rightcol[.border[ <img src="img/appearance.png" width="70%" height="70%" style="display: block; margin: auto;" /> ]] --- class: left, top ## Pane layout .leftcol[ #### - Source? #### - Console? #### Combining pane elements? ##### - Plots, Connections, Build, VCS, Presentation ##### - Files, Packages, Help, Tutorial, Viewer ] .rightcol[.border[ <img src="img/default-layout.png" width="75%" height="75%" /> ]] --- # Pane layout view Standard layout options .border[ <img src="img/default-layout-view.png" width="65%" height="65%" style="display: block; margin: auto;" /> ] --- class: left, top # Pane layout: add column #### Two screens? -- .small[ *add a Source column and rearrange the panes* ] -- .leftcol[.border[ <img src="img/add-pane-column.png" width="60%" height="60%" style="display: block; margin: auto;" /> ]] .rightcol[.border[ <img src="img/add-column-switch-panes.png" width="60%" height="60%" style="display: block; margin: auto;" /> ]] --- class: left, top # Pane layout: add column view Now you see **Source**, **Tutorial**, and **Console** panes on a single screen! .border[ <img src="img/three-column-layout.png" width="65%" height="65%" style="display: block; margin: auto;" /> ] --- class: inverse, center, middle # RStudio Projects --- class: left, top # Why RStudio Projects? ### Keep track of all your files with RStudio project files (`.Rproj`). -- #### Self contained > Using R projects keeps track or your current working directory! -- #### Project orientated > .Rproj files make bundling and shipping files and folders easier! --- class: left, top # Why RStudio Projects? ### Keep track of all your files with RStudio project files (`.Rproj`). -- #### Avoid removing all the files -- **Don't do this!** ```r rm(list = ls()) ``` -- > `.Rproj` files keep all the files associated with a project together — input data, R scripts, analytic results, figures. --- class: left, top # Creating RStudio project in existing folder #### Click on *'Project: (None)'* > *'New Project'* -- .border[ <img src="img/proj-existing-dir.png" width="50%" height="50%" style="display: block; margin: auto;" /> ] --- class: left, top # Creating RStudio project in existing folder #### Click on *'Browse* > *'Create Project'* -- .border[ <img src="img/proj-existing-dir-create.png" width="50%" height="50%" style="display: block; margin: auto;" /> ] --- class: left, top # Creating RStudio projects in new folder -- .leftcol[ #### Click on .blue[Project: (None)] > .blue[New Project] .border[ <img src="img/proj-new-dir.png" width="80%" height="80%" style="display: block; margin: auto;" /> ]] -- .rightcol[ #### Select project type .border[ <img src="img/proj-new-project.png" width="80%" height="80%" style="display: block; margin: auto;" /> ]] --- class: left, top # Creating RStudio projects in new folder -- .leftcol[ #### Create new folder name .border[ <img src="img/proj-new-dir-name.png" width="90%" height="90%" style="display: block; margin: auto;" /> ]] -- .rightcol[ #### Choose parent folder .border[ <img src="img/proj-new-parent-dir-name.png" width="90%" height="90%" style="display: block; margin: auto;" /> ]] --- class: left, top # Creating RStudio projects in new folder #### If you have Git installed, select *'Create a git repository'* .border[ <img src="img/proj-new-dir-version-crtl.png" width="50%" height="50%" style="display: block; margin: auto;" /> ] --- class: left, top # Creating RStudio projects in new folder #### Check for new project name & *Git* pane .border[ <img src="img/proj-new-dir-view.png" width="60%" height="60%" style="display: block; margin: auto;" /> ] --- class: inverse, left, middle # Folder Structure - separate raw and cleaned data - keep documents and code separate - keep figures separate - name files appropriately (preferably 2 digit prefix) - structure is reusable and easy to understand --- class: left, top .leftcol[ .small[ ``` project-name/ |-- CITATION |-- project-name.Rproj |-- README.md |-- LICENSE |-- requirements.txt |--data/ |--raw/ |--raw-birds-data.csv |--processed/ |--processed-birds-data.csv |--doc/ |-- notebook.Rmd |-- manuscript.Rmd |-- changelog.txt |--results/ |-- summarized-results.csv |--code/ |-- 01-sightings-import.R |-- 02-sightings-wrangle.R |-- 03-sightings-model.R |-- runall.R ``` ] ] .rightcol[ # Folder structure #### Adapted from from '[Good enough practices in scientific computing](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1005510)' ] --- class: inverse, center, middle # Naming things --- class: left, top # Naming files ### File names should be: .leftcol[ #### 1. human readable -> (makes sense) #### 2. machine readable -> (regex) #### 3. sort/order well -> (ISO 8601 date) ] -- .rightcol[ ##### 2020-10-12-270-301-**central-lab-metrics**.csv ##### 2020-10-12-**270-301**-central-lab-metrics.csv ##### **2020-10-12**-270-301-central-lab-metrics.csv ] --- class: left, top # Naming files* We can perform regular expression searches for files like this: *Find 270-301 files* ```r grepl(pattern = "270-301", x = "2020-10-12-270-301-central-lab-metrics.csv") ``` ``` [1] TRUE ``` -- .small[ > *Adapted from [Jenny Byran's slides](https://speakerdeck.com/jennybc/how-to-name-files) ] --- class: left, top # Naming files* ### Also acceptable: Logical order and underscores `_` -- ```r files ``` ``` [1] "01.0_import_lab-data.R" "02.0_wrangle_lab-data.R" [3] "03.0_eda_lab-data.R" ``` -- ```r stringr::str_split_fixed(string = files, pattern = "_", 3) ``` ``` [,1] [,2] [,3] [1,] "01.0" "import" "lab-data.R" [2,] "02.0" "wrangle" "lab-data.R" [3,] "03.0" "eda" "lab-data.R" ``` .small[ > *Adapted from [Jenny Byran's slides](https://speakerdeck.com/jennybc/how-to-name-files) ] --- class: inverse, center, middle # File paths --- class: left, top # Use relative rather than absolute file paths -- ### **Absolute paths** are specific to a system `/project-name/data` -> absolute path in macOS `\\project-name\\data` -> absolute path in Windows -- ### **Relative paths** are specific to a folder `project-name/data` -> relative path in macOS `project-name\\data` -> relative path in Windows --- class: left, top # Or use the `here` package ### The `here::set_here()` function solves a lot of file path problems (*especially if you're not using R projects*) ```r library(here) here::set_here(".") list.files(all.files = TRUE, pattern = "here") ``` ``` [1] ".here" ``` -- This creates a `.here` file (similar to `.Rproj` files) As long as the `.here` file stays in the referenced folder, you can include simply include `here::here()` in the top of your code files. --- class: left, top # Terminal pane .leftcol[ #### Learn a handful of command-line tools to make life easier `cd`, `pwd`, `mkdir`, `rm`, `ls`, etc. #### RStudio comes with a Terminal pane for quick access to the command-line ] .rightcol[.border[ <img src="img/terminal-pane.png" width="100%" height="100%" style="display: block; margin: auto;" /> ]] --- class: left, top # Getting help .leftcol[ R comes with a *ton* of accessible help files ```r ?read.csv ``` ] .rightcol[.border[ <img src="img/help-pane.png" width="100%" height="100%" style="display: block; margin: auto;" /> ]] --- class: left, top ## Getting help online R also help an incredible community! Click on the links below to see some of the common places for Q & A. ### 1) [Dedicated forum on RStudio Community](https://community.rstudio.com/) ### 2) [Questions tagged R on StackOverflow](https://stackoverflow.com/questions/tagged/r) ### 3) [Twitter topics with #rstats hashtag](https://twitter.com/hashtag/rstats) --- class: left, top ## Asking good questions (reproducible examples) You'll get better results if you ask a question with a reproducible example. The [`reprex` package](https://github.com/tidyverse/reprex) was designed to help you create one! ```r install.packages("reprex") library(reprex) ``` ### *Use the RStudio Addin to create a reproducible example from code you've copied onto your clipboard!* --- class: left, top # Reprex Addin 1 .leftcol40[ 1. Copy code 2. Select .blue[Addin] > .blue[Render selection] ] .rightcol60[.border[ <img src="img/copy-render-reprex.png" width="100%" height="100%" style="display: block; margin: auto;" /> ]] --- class: left, top # Reprex Addin 2 .leftcol40[ 1. Copy code 2. Select .blue[Addin] > .blue[Render selection] 3. Wait for console 4. Paste reprex ] .rightcol60[.border[ <img src="img/paste-reprex.png" width="100%" height="100%" style="display: block; margin: auto;" /> ]] --- class: left, top # Reprex + datapasta ### To copy + paste actual data in a reproducible example, try `datapasta`! https://reprex.tidyverse.org/articles/articles/datapasta-reprex.html <img src="https://raw.githubusercontent.com/tidyverse/reprex/main/img/datapasta_w_reprex_sheet_to_tribble.gif" width="50%" height="50%" style="display: block; margin: auto;" /> --- class: left, top Learn more about R best practices: 1. [R for Data Science](https://r4ds.had.co.nz/) 2. [Tidyverse](https://www.tidyverse.org/) 3. [RViews Community Blog](https://rviews.rstudio.com/) --- class: center, top # THANK YOU! ## Feedback @mjfrigaard on Twitter and Github mjfrigaard@gmail.com