# It's also a good idea to load all of the libraries that you might ever need
library(MASS)
library(psych)
# You can echo this if you want for homework assignments, but NOT for professional reports
# BTW, you should not install packages in an R Markdown document
# as these packages should already be installed on your computer.
This is an example R Markdown document that is designed to give you a brief overview of how to write a document.
You can always read about this subject more online:
The RStudio Cheatsheat for R Markdown is found here:
https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf
This particular example is meant to be compiled to an HTML file.
While it would also compile to a PDF document using LaTeX
, there are some bonus settings and slight tricks that are unique only to LaTeX
documents. I will demonstrate this in another example file.
Headers start new chapters/sections and subsections
To insert a break between paragraphs, include a single completely blank line.
To force a line break, put two blank
spaces at the end of a line.
I can make a separating line by using ---
# If you know html we can make extra spaces using <br>
or
Note: this does not work for LaTeX
Code that needs italicized can be made using a single set of underscores or asterisks.
Code that needs to be bold uses two underscores or asterisks instead.
I’ve never used strikethrough personally, but it’s nice to know you can.
The > symbol makes a quote paragraph block.
Quoting verbatim computer code inline
is done by surrounding with left-single-quotes (a.k.a. “back-ticks”) (next to the 1 key). If you want to display multiple lines like this, start them with three back ticks in a row on a line by themselves, and end them the same way:
If you don’t skip a line, things look kinda crowded.
This looks much better.
R Markdown handles the numbering in what it renders automatically.
This line actually started with a 2, but R fixed it and put a 3. Clever R fixed this when we made the mistake.
There are times when you need to write some math, this is handles using LaTeX
notation.
Inline equations such as \(A = \pi * r^2\) are surrounded by a set of $ signs. Sometimes entire equation blocks are needed with more extensive math. In this case, surround your math by a set of $$
. \[
\int_{a}^{b} f(x) dx
\]
You can even do linear algebra
$$
\mathbf{A} = \begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
10 & 11 & 12
\end{bmatrix}
$$
\[ \mathbf{A} = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ 10 & 11 & 12 \end{bmatrix} \]
| Tables | Are | Cool |
| :------------ |:-------------:| -----:|
| col 1 is | left-aligned | -$5 |
| col 2 is | centered | $12 |
| col 3 is | right-aligned | $1600 |
| zebra stripes | are neat | $1 |
Tables | Are | Cool |
---|---|---|
col 1 is | left-aligned | -$5 |
col 2 is | centered | $12 |
col 3 is | right-aligned | $1600 |
zebra stripes | are neat | $1 |
There are many libraries that make nice looking tables automatically.
You can read about these libraries online for customization options. Overall kable()
is probably the easiest to use, but others are handy in particular situations.
Below is not an exhaustive list.
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Let’s see if we can format the output in a nicer looking table.
https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html
# A table using kable()
library(kableExtra)
kable( head(mtcars), caption = "Table using kable()") %>%
kable_styling(font_size = 10)
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
mpg | cyl | disp | hp | drat | wt | qsec | vs | |
---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.46 | 0 |
Mazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.61 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17.02 | 0 |
Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.22 | 1 |
am | gear | carb | |
---|---|---|---|
Mazda RX4 | 1 | 4 | 4 |
Mazda RX4 Wag | 1 | 4 | 4 |
Datsun 710 | 1 | 4 | 1 |
Hornet 4 Drive | 0 | 3 | 1 |
Hornet Sportabout | 0 | 3 | 2 |
Valiant | 0 | 3 | 1 |
# Usage

# If the image is on your computer

# If the image in the internet somewhere

A remote image
The problem with the above method, is that using R Markdown to render directly to HTML, there is no easy way to resize the image.
One good alternative is to use knitr::include_graphics()
which you can read about on your own.
Hyperlinks anchored by URLs are easy: Simply type the URL, e.g., https://www.stat.vt.edu/academics/graduate/daas-nva.html
Hyperlinks anchored to text have the [anchor in square brackets, then the link in parentheses](https://www.stat.vt.edu/academics/graduate/daas-nva.html)
Hyperlinks anchored by URLs are easy: Simply type the URL, e.g., https://www.stat.vt.edu/academics/graduate/daas-nva.html
Hyperlinks anchored to text have the anchor in square brackets, then the link in parentheses
# This chunk will echo the code AND run it.
# Here is some comments, below is the code
x <- runif(5, min = -5, max = 10)
print(x)
[1] 0.1109415 7.9101084 7.9611550 7.2324957 -4.3394611
Most of your write-up should be regular text, not comments within chunks. Everyone once in a while you can include the output from a chunk where the code is not necessarily important to be seen (as long as the context is clear), like the example below which has 5 uniform random numbers between -5 and 10.
[1] 3.3524274 1.7768364 -0.8009476 -2.6978338 5.4420078
# eval=FALSE
# This chunk will echo the code BUT NOT run it.
# Here is some comments, below is the code
x <- runif(5, min = -5, max = 10)
print(x)
# You are unlikely to use this for class reports, but it's useful for your later career.
# z was made in a chunk that had include=FALSE, tt's now showing the result
print(z)
[1] "This is message"
vars n mean sd median trimmed mad min max range skew
speed 1 50 15.40 5.29 15 15.47 5.93 4 25 21 -0.11
dist 2 50 42.98 25.77 36 40.88 23.72 2 120 118 0.76
kurtosis se
speed -0.67 0.75
dist 0.12 3.64
# You can make R output use more width by changing
# R.options=list(width=130) # 130 is character length
# in the R chunk options.
describe(cars)
vars n mean sd median trimmed mad min max range skew kurtosis se
speed 1 50 15.40 5.29 15 15.47 5.93 4 25 21 -0.11 -0.67 0.75
dist 2 50 42.98 25.77 36 40.88 23.72 2 120 118 0.76 0.12 3.64
In the setup chunk I specified a folder where all figures will be stored. This is helpful if you want to use those plots for some other purpose. Keep in mind, the plots will only have names if you give your chunks names like what I show below.
# The default is based upon what you set in the setup chunk
# In this case, plots are 7 x 7 inches and aligned in the center.
plot(cars)
# fig.width and fig.height can be used to specify dimensions
# In this case I used {r fig.width=5, fig.height=5}
plot(cars)
# fig.width and fig.height can be used to specify dimensions
# In this case I used {r fig.width=8, fig.height=5}
plot(cars)
# However there are times when out.width and out.height is a better looking option
# In th is case I used {r, out.width="50%"} we let it figure out the height on it's own.
plot(cars)
Figure 1: This is a plot
Comments
Comments in the general text work differently than in R code.
I commented a line out, but you could not see it in the .html file, here is how I did it.