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

Creating a Project

  • File > New Project…
    • Choose New Directory, then New Project
    • Name the directory after the course or the task
    • Browse to where the folder should live
  • RStudio makes the folder and puts an .Rproj file inside it
    • That file is the project; opening it opens the project

Working Inside a Project

  • The Files tab (Extras pane) shows the project folder
  • Make a new script and RStudio puts it in the project folder
    • Add a line of text, such as # Hello World
    • Save it; the dialog already points at the project folder
  • Close the script with the x icon, then reopen it from Files

Closing and Reopening a Project

  • File > Close Project
    • The Source Editor empties and the Console resets
    • It looks like your work is gone
  • File > Open Project, then pick the .Rproj file in your folder
  • Everything comes back exactly where you left it
    • Open files, working directory, and history are all restored

Quarto

Quarto

  • Quarto is a technical publishing system
    • One document holds the prose, the code, and its output
  • Create dynamic content with R, Python, Julia, and Observable
    • Results are computed at render time, never pasted in
  • Articles, reports, slideshows, websites, blogs, and books in HTML, PDF, MS Word, and ePub formats
    • These slides are a Quarto document, and so is this whole site
  • Include equations, citations, crossrefs, panels, callouts, layouts, etc.

Creating a Quarto Document

  • File > New File > Quarto Document…
    • Keep the defaults, then Create Empty Document
  • File > Save
    • The dialog defaults to the project folder
    • Give it a name and note the .qmd extension

Adding an R Chunk

  • A chunk is a block of R code sitting inside the document
  • Any one of these will insert one:
    • Click the green box with a C and a plus sign (top-right)
    • Ctrl+Alt+I (Win) or Cmd+Option+I (Mac)
    • Type the fences yourself: three backticks and {r}, then a closing line of three backticks

Running Code in a Chunk

  • A chunk works like a mini console
    • Do a calculation inside it, then click the green arrow
    • The answer appears right below the chunk
  • Save and render the document and the answer appears in the output too
  • Chunk options control how each chunk behaves; more later

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 treats text that is not in a code chunk as markdown

Formatting Text

Type this Get this
*italics* italics
**bold** bold
***italics and bold*** italics and bold
superscript^2^ superscript2
subscript~2~ subscript2
~~strikethrough~~ strikethrough
`verbatim code` verbatim code

Headings and Links

  • A heading is one or more # at the start of a line
    • # Header 1, then ## Header 2, then ### Header 3
    • More hashes means a smaller, deeper heading
  • Links come in two forms:
    • <https://quarto.org> shows the bare URL
    • [Quarto](https://quarto.org) shows text you pick

Images and Lists

  • An image is a link with a ! in front of it
    • ![Caption](elephant.png)
  • Lists are * or - for unordered, numbers for ordered
    • Indent by four spaces to make a sub-item