0% found this document useful (0 votes)
26 views10 pages

Intro To RMD

Uploaded by

red.book7748
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views10 pages

Intro To RMD

Uploaded by

red.book7748
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to R Markdown

Using R for Basic Reports

Dr. Christian Lucero ©

Fall 2024

Contents
1 Overview 2

2 Header Level 1 2
2.1 Header Level 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 General Text 2

4 Comments 3

5 Horizontal Lines 3

6 Spacing Items out 3

7 Boldface, Italics, Strikthrough & Quotations 3

8 Ordered and Unordered Lists 3


8.1 Unordered Bullet Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
8.2 Numbered Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

9 Math Equations 4

10 Tables 5
10.1 Tables by Hand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
10.2 Generating Tables Automatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

11 Images 6

12 Hyperlinks 6

13 Code Chunks 6

14 Customizing Plot Output 8

1
# 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.

1 Overview
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:
• https://rmarkdown.rstudio.com/
• https://bookdown.org/yihui/rmarkdown/

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 a pdf file.
Note, it is possible to keep the .tex file which allows you to do after the fact editing.

2 Header Level 1
Headers start new chapters/sections and subsections

2.1 Header Level 2


2.1.1 Header Level 3
2.1.1.1 Header Level 4

3 General Text
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 start new pages by using \newpage or \clearpage

2
4 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.
<!-- This has been commented out and will not show up in the rendered document. -->

5 Horizontal Lines
I can make a separating line by using --- or ***.

But \hrule works much better in this case.

6 Spacing Items out


I can space objects out vertically using \vspace{0.2in} replacing the 0.2 with the quantity, and the in with whatever units
I want, such as cm, em.

This was pushed down using \vspace{2cm}. While this was pushed right using \hspace{3cm}.

7 Boldface, Italics, Strikthrough & Quotations


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:
Note, LaTeX adds the ability to bold and italicize text alternative ways.

8 Ordered and Unordered Lists


8.1 Unordered Bullet Lists
• This is an unordered list marked by bullet points.
• Each item in the list should start with a * (asterisk) character, or a single dash (-).
• Each item should also be on a new line.
– Indent lines and begin them with + for sub-bullets.
∗ We can create sub-sub-bullets but you have to indent twice from the last indent.
• If you don’t skip a line, things look kinda crowded.
• This looks much better.
– If that is the look you want to go for.

3
8.2 Numbered Lists
1. Lines which begin with a numeral (0–9), followed by a period, will usually be interpreted as items in a numbered list.
a. We can make sub-lists of numbered lists using letters, but they can be tricky.
2. R Markdown handles the numbering in what it renders automatically.
3. This line actually started with a 2, but R fixed it and put a 3. Clever R fixed this when we made the mistake.

9 Math Equations
There are times when you need to write some math, this is handles using LaTeX notation.
Inline equations such as 𝐴 = 𝜋 ∗ 𝑟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 $$.
𝑏
∫ 𝑓(𝑥)𝑑𝑥
𝑎

You can even do linear algebra


$$
\mathbf{A} = \begin{bmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9 \\
10 & 11 & 12
\end{bmatrix}
$$

1 2 3
⎡4 5 6⎤
A=⎢ ⎥
⎢7 8 9⎥
⎣10 11 12⎦

4
10 Tables
10.1 Tables by Hand
The alignment of tables is dictated by where the : is. See below.
| 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

10.2 Generating Tables Automatically


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.
# The original console output
head(mtcars)
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.

10.2.1 kable()
https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html
# A table using kable()
library(kableExtra)
kable( head(mtcars), format = "latex", booktabs = T, caption = "Table using kable()") %>%
kable_styling(font_size = 10, latex_options = c("striped", "hold_position"))

Table 2: Table using kable()

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

5
10.2.2 pander()
# A table using pander()
library(pander)
pander( head(mtcars) )

Table 3: Table continues below

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

10.2.3 DT::datatable()
It’s probably best you don’t use DT’s library for PDF documents. It’s not imposible, but kable() works so much better.

11 Images
I recommend the use of knitr::include_graphics() for including graphics. You should read about this subject on your
own.

12 Hyperlinks
Hyperlinks anchored by URLs are easy: Simply type the URL, e.g.,
https://www.ais.science.vt.edu/programs/cmda.html

Hyperlinks anchored to text have the [anchor in square brackets, then the link in
parentheses](https://www.ais.science.vt.edu/programs/cmda.html)
Hyperlinks anchored by URLs are easy: Simply type the URL, e.g., https://www.ais.science.vt.edu/programs/cmda.html
Hyperlinks anchored to text have the anchor in square brackets, then the link in parentheses

13 Code Chunks

# 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)

6
[1] 5.9902708 0.9061391 1.7287773 -4.3253224 9.6566653

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] -1.261018 -1.824748 2.247307 6.041671 -2.800663

# 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"

# Notice that it doesn't use the full width of the page when I look at this
describe(cars)
vars n mean sd median trimmed mad min max range skew kurtosis
speed 1 50 15.40 5.29 15 15.47 5.93 4 25 21 -0.11 -0.67
dist 2 50 42.98 25.77 36 40.88 23.72 2 120 118 0.76 0.12
se
speed 0.75
dist 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

13.0.1 Note: You can run R scripts from within chunks


# You can call R scripts from R Chunks if you specify the path
source("/home/username/.../Lecture_03/my_scripts/awesome_script.R")

# If you have set the current working directory to be in Lecture_03


# then you can specify the relative path
source("my_scripts/awesome_script.R")

7
14 Customizing Plot Output
# 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)
120
100
80
dist

60
40
20
0

5 10 15 20 25

speed

# 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)

8
120
100
80
dist

60
40
20
0

5 10 15 20 25

speed

# 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)
120
100
80
dist

60
40
20
0

5 10 15 20 25

speed

9
# 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)

120
100
80
dist

60
40
20
0

5 10 15 20 25

speed

# In this case I used {r, out.width="65%", fig.align='left'}


plot(cars)
120
100
80
dist

60
40
20
0

5 10 15 20 25

speed

10

You might also like