R Markdown Basics {#rmd-basics}

Here is a brief introduction into using R Markdown. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. R Markdown provides the flexibility of Markdown with the implementation of R input and output. For more details on using R Markdown see https://rmarkdown.rstudio.com.

Be careful with your spacing in Markdown documents. While whitespace largely is ignored, it does at times give Markdown signals as to how to proceed. As a habit, try to keep everything left aligned whenever possible, especially as you type a new paragraph. In other words, there is no need to indent basic text in the Rmd document (in fact, it might cause your text to do funny things if you do).

Lists

It's easy to create a list. It can be unordered like

or it can be ordered like

  1. Item 1
  2. Item 2

Notice that I intentionally mislabeled Item 2 as number 4. Markdown automatically figures this out! You can put any numbers in the list and it will create the list. Check it out below.

To create a sublist, just indent the values a bit (at least four spaces or a tab). (Here's one case where indentation is key!)

  1. Item 1
  2. Item 2
  3. Item 3
    • Item 3a
    • Item 3b

Line breaks

Make sure to add white space between lines if you'd like to start a new paragraph. Look at what happens below in the outputted document if you don't: Here is the first sentence. Here is another sentence. Here is the last sentence to end the paragraph. This should be a new paragraph.

Now for the correct way:

Here is the first sentence. Here is another sentence. Here is the last sentence to end the paragraph.

This should be a new paragraph.

R chunks

When you click the Knit button above 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 (mtcars is a built-in R dataset):

summary(mtcars)

Inline code

If you'd like to put the results of your analysis directly into your discussion, add inline code like this:

The cos of $2 \pi$ is r cos(2*pi).

Another example would be the direct calculation of the standard deviation:

The standard deviation of speed in cars is r sd(cars$speed).

One last neat feature is the use of the ifelse conditional statement which can be used to output text depending on the result of an R calculation:

r ifelse(sd(cars$speed) < 6, "The standard deviation is less than 6.", "The standard deviation is equal to or greater than 6.")

Note the use of > here, which signifies a quotation environment that will be indented.

As you see with $2 \pi$ above, mathematics can be added by surrounding the mathematical text with dollar signs. More examples of this are in [Mathematical equations].

Plots

Varsity blues already solves all the packages in order to insert plots right away from your code.

#| fig-width: 5
#| fig-height: 3
#| fig-cap: "An elementary plot"

library(ggplot2)

ggplot(mtcars) +
  geom_point(aes(x = cyl, y = wt, color = am))

Tables

As for the case of plots, this package already solves all the dependencies in order to use different types of tables in \LaTeX.

Simple table

kable(xtabs(~ am, mtcars))

Complex table (regression table)

#| results: 'asis'

library(stargazer)

model1 <- lm(mpg ~ cyl, mtcars)
model2 <- lm(mpg ~ cyl + am, mtcars)
model3 <- lm(mpg ~ cyl + am + wt, mtcars)

stargazer(model1, model2, model3, header = F, title = "Regression table")

\newpage

Mathematical equations

Consider a function $f: U \to \mathbb{R}$, defined on an open set $U\subset\mathbb{R}$, is said to be differentiable at $a\in U$ if the derivative $f'(a) = \lim_{h \to 0}\frac{f(a+h)-f(a)}{h}$ exists. In general, $f$ is of class $\mathcal{C}^k$ if its first $k$ derivatives $f^{\prime}(x), f^{\prime\prime}(x), \ldots, f^{(k)}(x)$ exist and are continuous.



pachadotdev/varsityblues documentation built on March 25, 2024, 1:57 p.m.