Statistical Methods with R

Reproducible Workflows

Unit A · Chapter 02 · Lecture 02a

Developed by Jeffrey M. Girard

Roadmap: Meeting R

  1. Projects

  2. Quarto

  3. Markdown

  4. Activity

Projects

File Management

  • Projects are special folders on your computer
    • They contain all files related to a task
    • They keep everything together and organized
  • Projects make it easy to find and use your files
    • No need to specify long, annoying file paths
    • No need to worry about working directories
  • Projects make it easy to switch between tasks
    • They will remember exactly where you left off
    • You can even open multiple projects at once

Projects Live Coding

# Create a new Project
- Open the "File" menu in RStudio
- Select the "New Project..." option
- Select the "New Directory" option
- Select the "New Project" option
- Name the directory "PSYC 399" or "PSYC 800" (or whatever)
- Browse to where to create your Project folder

# Create a new File
- Explore the Files tab in the Extras pane
- Create a New File (e.g., a script) as an example
- RStudio will automatically create it in your project folder
- Add some text to the example file (e.g., "# Hello World")
- Save the file and note that it defaults to your project folder
- Close the script with the "x" icon
- Reopen the script from the Files tab

# Close and Open Project
- Open the "File" menu in RStudio
- Select the "Close Project" option
- Notice that your work is now gone
- Open the "File" menu in RStudio
- Select the "Open Project" option
- Browse to your project folder
- Open the .Rproj file (e.g., PSYC 399.Rproj)
- Notice that your work is now back!

Quarto

Quarto

  • Quarto is a scientific/technical publishing system
  • Create dynamic content (e.g., code, output, and markdown) with R, Python, Julia, and Observable
  • Articles, reports, slideshows, websites, blogs, and books in HTML, PDF, MS Word, and ePub formats
  • Include equations, citations, crossrefs, figure panels, callouts, advanced layouts, etc.

Quarto Live Coding

# Create an Quarto Document
- Open the "File" menu in RStudio
- Select the "New File" option
- Select the "Quarto Document..." option
- Keep the defaults (HTML) and hit "Create Empty Document"
- Open the "File" menu
- Select the "Save" option
- Note that it defaults to the project folder
- Give it a name like "Lecture 02b" (or whatever)
- Note that the file extension is .qmd

# Add an R Chunk
- Option 1: Click the green box with C and a plus sign icon (top-right)
- Option 2: Ctrl+Alt+I (Win) or Cmd+Option+I (Mac)
- Option 3: Type out ```{r} then new line and ``` yourself

# Adding code to the chunk
- Anything you put inside the "fences" (i.e., ```) will be treated as R code
- The chunk operates like a mini console
- Try doing some calculations in the chunk and hit the green arrow
- The answer appears right below the chunk!
- If we save and render the document, it appears in the output too
- We can also add options to the chunk to control how it behaves

Markdown

Markdown

  • Markdown is a simple text-to-HTML conversion language used by Quarto, GitHub, Obsidian, etc.
  • It will allow us to add formatted text, images, links, lists, etc. to our Quarto documents
  • Quarto will treat all text that is not in a code chunk as markdown

Markdown Live Coding

# Formatting
- *italics* and **bold** and ***italics and bold***
- superscript^2^ and subscript~2~
- ~~strikethrough~~
- `verbatim code`

# Headings
- # Header 1
- ## Header 2
- ### Header 3

# Links
- <https://quarto.org>
- [Quarto](https://quarto.org)

# Images
- ![Caption](https://quarto.org/docs/authoring/elephant.png)

# Lists
- * Unordered list
-   + sub-item
-   + sub-item
- 1. Ordered list
- 2. Item 2