Source Documents: ⇒ CSC303_Ch04_Practice.Rmd
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.
The above was a hyperlink. Another way to include hyperlink is like this: click here for tigertree help.
You can get boldface by surrounding a word or phrase with **, like this: here is some boldface.
You can get italics if you surround the word or phrase with *, like this: here is some italicized text. Another way is to surround with underscores, like this: here is some more italicized text.
You can also get subsection-headings:
There are sub-sub-section headings:
You can go as far as six #’s but I don’t recommend using more than four. You can also use just one #, but then the heading is as big as the title of the document: not a good idea!
You can make text look like computer code if you surround it with back-ticks (upper-right corner of your keyboard). Thus, the data table BabyNames is displayed as if in a Console. This is called “verbatim” mode.
You can set off computer code if you use three back-ticks before and after. If you add braces with r inside, then the computer will know to use r to execute the code.
Here is an example:
BabyNames %>%
filter(name == "Prince") %>%
head(size = 5)
## name sex count year
## 1 Prince M 16 1880
## 2 Prince M 17 1881
## 3 Prince M 18 1882
## 4 Prince M 18 1883
## 5 Prince M 17 1884
## 6 Prince M 21 1885
You can display text in “block quotes” if you use a >, like this:
To be or not to be, that is the question.
Here is an ordered list:
Here is an unordered list:
Notice the space between the * and the beginning of the text, in each item.
You can nest lists if you indent by four spaces:
When you press the Knit button the computer runs the R-code in the code chunks and shows the output. This “knits” your explanatory text together with the code and its result.
This process is called compilation. A .Rmd document can be complied to a pdf file, an HTML file, or to a Word document.
The r in the braces in the code chunk is a chunk option. There are many chunk options to choose from
echo = FALSEThis allows allows the code to execute, showing the results, but hides the code itself. This option is especially useful in reports, as many readers don’t want to see your R-code.
Example:
## name sex count year
## 1 Prince M 16 1880
## 2 Prince M 17 1881
## 3 Prince M 18 1882
## 4 Prince M 18 1883
## 5 Prince M 17 1884
## 6 Prince M 21 1885
By the way, if you want to data table in a very pretty way, you could use the kable() function from the knitr package:
BabyNames %>%
filter(name == "Prince") %>%
head(size = 5) %>%
knitr::kable()
| name | sex | count | year |
|---|---|---|---|
| Prince | M | 16 | 1880 |
| Prince | M | 17 | 1881 |
| Prince | M | 18 | 1882 |
| Prince | M | 18 | 1883 |
| Prince | M | 17 | 1884 |
| Prince | M | 21 | 1885 |
eval = FALSESometimes you want to show code, but don’t want it to be run when the document is compiled. Here is an example:
help("BabyNames")
we have already seen several options for code chunks that make a graph, including out-width, fig.cap and fig.align.
Here is one:
BabyNames %>%
filter(name == "Bobbie") %>%
ggplot(aes(x = year, y = count)) +
geom_line(aes(color = sex)) +
labs(x = "Year", y = "Number Born",
title = "Bobbie, by Sex")
The name Bobbie became more popular for both sexes in the 1920’s but has declined steadily since then.
Please note that except for the r option at the beginning, all chunk options must be separated by commas.
Sometimes you want to show the result of an R computation right in the middle of a sentence. Here is how you might accomplish that.
There are 1792091 cases in the BabyNames data table.
You can do some basic math inline. For example, you might want to say that \(2+2=4\). Or you might want to give the formula for the area of a circle: \(A = \pi r^2\).
You can also display formulas. for example, if
\[ax^2 + bx +c = 0\] then
\[x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}.\]
To do fancy math, you’ll have to learn some \(\LaTeX\). You won’t need it in this course, but it can be useful in other math classes. Google “latex tutorial” and you’ll find many useful resources.
You can include images, too:
Math nerds at play.