R Codes (Week 1)

Click here to download the Rmd file: week1-intro-RMarkdown.Rmd

RStudio Intro

print("This is Thursday.")
[1] "This is Thursday."

Tools --> Global Options --> 
- Uncheck "Restore .RData into workspace at startup".
- Set "Save workspace to .RData on exit" to "Never".  
- (Optional) R Markdown --> Set "Show output preview in" to "Viewer Pane".

Use Project

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Note: R is case sensitive. So Cars and cars are different.

summary(cars[-(1:4), ])

YAML options

Chunk options

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Install packages

tweetrmd::include_tweet("https://twitter.com/visnut/status/1248087845589274624")

Check out https://rladiessydney.org/courses/ryouwithme/01-basicbasics-2/

# Install the tidyverse "meta" package
# install.packages("tidyverse")
# Install the lme4 package
# install.packages("lme4")

Load a package

Tip 1: Try the (PC) Ctrl + Alt + I/(Mac) Cmd + Option + I shortcut for a new code chunk

Tip 2: Use Tab for code completeion

Tip 3: Use Ctrl + Enter/(Mac) Cmd + Return for running a line of R code

Tip 4: Set message = FALSE to suppress messages in loading packages

# Load tidyverse

# Load lme4

Data Frame

# Load SleepStudy

# Extract one column

# Extract column by index (same as last line)

# Extract two rows

# Compute the mean and sd, and chain them together

# Correlation matrix with psych::pairs.panel()

# Find out what a function does (use `?function_name`, e.g., `?pairs.panel`)

Basic Markdown Elements

From RStudio, click Help –> Markdown Quick Reference

Italic and bolded texts

This is italic

Lists (Ordered and Unordered)

Equations (LaTeX)

Inline: The correlation between \(a = b + c + \tau\)

Display:

\[a = b + c + \tau\]

Cheatsheet

More detailed cheatsheet: https://rmarkdown.rstudio.com/lesson-15.HTML


Exercise

  1. Download the Rmd file for the exercise on Blackboard
  2. Insert your name on line 3.
  3. Complete the following in this R Markdown document:
    1. Copy the following LaTex equation to below: A_1 = \pi r^2. How does this say about writing Greek letters and subscripts/superscripts? \[[Insert equation here]\]

    2. Install and then load the modelsummary package, and run the following. You’ll need to remove eval = FALSE so that it runs. Find out what this code chunk does.

      # Install and load the modelsummary package first; otherwise it won't run
       library(modelsummary)
       fm1 <- lm(dist ~ speed, data = cars)
       fm2 <- lm(dist ~ poly(speed, 2), data = cars)
       fm3 <- lm(log(dist) ~ log(speed), data = cars)
       msummary(list(fm1, fm2, fm3))
       
    3. Run the following. You’ll need to remove eval = FALSE so that it runs. Find out what this code chunk does.

      ggplot(cars, aes(x = log(speed), y = log(dist))) +
         geom_point() +
         geom_smooth()
       
    4. Add a code chunk below to show the output of running sessionInfo(), which prints out the session information of your computer. Make the code chunk to show only the output, but not the code.

    5. Knit the document to HTML, PDF, and Word. If you run into an error when knitting to any one of the formats, record the error message. Which format do you prefer?

    6. Go to the top of this Rmd file, and change the line inside YAML

        html_document: default

      to

        html_document: 
            toc: TRUE

      Knit the document again. What does it do?

    7. Submit the knitted document to Blackboard in your preferred format (HTML, PDF, or WORD)