R Markdown Demo in RStudio

Sep 15, 2024

R Markdown in RStudio Demo

Overview

  • Quick demonstration of using R Markdown in RStudio
  • Focus on creating and processing R Markdown documents

Getting Started

  • Open a new R Markdown file in RStudio:
    • Go to menu and select R Markdown
    • Default output format is usually HTML (recommended)
    • Options for PDF or Microsoft Word may require additional tools
  • Input document title and author (optional)

Creating a Document

  • After creating the document, a pre-populated template appears
  • Knit HTML Button: Click to generate HTML document from Markdown
    • Code is displayed in gray boxes
    • Outputs, including plots, are rendered in the HTML document

Basic Document Editing

  • Remove boilerplate text after the preamble
  • Save the document (e.g. as Markdown.demo.rmd)
  • Begin writing custom text and code
  • Example introductory text: "This is my first breakdown document."

Loading and Summarizing Data

  • Use code block to write R code:
    • Open code block with three backticks (```) and specify r for R code
    • Load air quality dataset using data(airquality)
    • Summarize the dataset with summary(airquality)
  • Knit the document to view results

Exploratory Data Analysis

  • Visualizing the Data: Create a pairs plot (scatter plot matrix)
    • Code: pairs(airquality)
  • Knit the document again to see the visualization

Modeling Data

  • Fit a regression model:
    • Predict ozone based on solar radiation, wind, and temperature
    • Use code block with linear model:
      lm_ozone <- lm(Ozone ~ Solar.R + Wind + Temp, data = airquality)
      summary(lm_ozone)
      
  • Knit the document to view model summary and coefficients

Additional Features in R Markdown

  • Create unordered lists using asterisks or other delimiters
  • Create ordered lists using numbers
  • R Markdown integrates text with R code and outputs, facilitating data analysis reporting

Conclusion

  • Encouragement to create and experiment with R Markdown in RStudio
  • Combine text, code, and outputs in a single document for effective communication of analysis.