This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.
Let’s watch a short video introducing R Markdown.
The Title, Author and Output format choices will be used to create the YAML header and are used to customize your document. By default, the date is also included. A subtitle can be also be added.
Additional information that can be added to customize your HTML document include:
toc: a table of contents will be added when toc: truetheme: specifies the aesthetic theme for the document, including:
highlight: specifies syntax highlighting style, including:
code_folding: when set to hide (used in this document), the code is hidden by default, but can be displayed by the user. When set to show, code is shown, but can be hidden by the user.When you click the Knit button a document will be generated that includes both non-code content (text, links, images, plots, tables, etc.) as well as the output of any embedded R code chunks or in-line text within the document.
Reports created using Markdown include a combination of text and code output.
Section headings are creating using #, followed by the heading text. The largest heading uses a single #. Additional # can be included to denote lower level headings.
Unordered lists can be created using - or *. Lower level bullets can be created using + and indenting.
Ordered lists can be created using numbers followed by a period (.). Lower level bullets can be created using + and indenting.
Create a new R Markdown file. Choose HTML as the output type and name the document “In Class Activity”.
When a new markdown file is created, it includes example code. Knit the document and save the file to your computer as “ICA.Rmd”.
Remove the example code (lines 12-30). Add a Level 2 heading that says “My Activity Solutions”. Change the theme (not cerulean) in the YAML header.
To incorporate the output of running code into your report, you need to use R code chunks, which are code lines between a set of triple back ticks. To add a code chunk, you can use the icon and choose R.
Below, we run the summary() function on the built-in cars dataframe.
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
As you see, the default prefix of the result of running a line of code is ‘##’. We can remove this by setting comment = "".
dim(cars)
[1] 50 2
If you want to display the R code, but not run the code (or display code output), you would use eval = FALSE:
dim(cars)
If you do NOT want to display the R code, only the output of running the code, you would use echo = FALSE:
## [1] 50 2
To include code in-text, you can use a set of back ticks. For instance: To obtain the minimum of the first variable in the cars dataframe, we would use min(cars[,1]). To incorporate the results of running code in text, we include r after the first back tick. As an example, the mean of the speed variable in the cars dataset is 15.4 and the variance is 27.96. The minimum value of the dist variable in the cars dataset is 2 and the maximum value is 120.
echo = FALSE, the plot will display but not the code used in the code chunk to create the plot.
Figure 1
Options to customize the plot appearance include:
fig.align: “left”, “right” or “center”fig.cap: provide a figure captionfig.width: specify figure width (inches, default 7)fig.height: specify figure height (inches, default 5)To include images that are located in the same folder as your .Rmd file, you can reference the image file by name using .
LeBow College of Business
Note: In this example, I use a link to a shared image from google drive.
Using the built in dataset named attitude, use a code chunk to obtain summary information. Do not include the code in your document. Only include the output.
Using in-line code, complete the following sentence by filling in the missing information (rounded to 2 decimal places):
The average rating value is _________, with a variance of _________ and a standard deviation of _________. The minimum value of the complaints variable is ________ and the maximum value is ________.
complaints on the x-axis and rating on the y-axis. Give the plot a meaningful title, axis labels, and report caption.Default tables can be created using the print() function on a dataframe or matrix object or running a code chunk of the dataframe or matrix name. To avoid printing ## in our results, we set comment = "".
head(cars)
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
Tables can be included using the kable() function in the knitr package. These tables can be dataframes or the output from functions such as table() and aggregate(). To omit the code used to create the table, use echo = FALSE.
Note: Before using, you may need to install the package.
knitr::kable(x = head(cars),
caption = "Cars Dataset Preview")
| speed | dist |
|---|---|
| 4 | 2 |
| 4 | 10 |
| 7 | 4 |
| 7 | 22 |
| 8 | 16 |
| 9 | 10 |
Function arguments that can be used to customize the table include:
caption: A title for the tabledigits: the maximum number of digits to include in the table outputrow.names: logical (TRUE/FALSE) identifying if row names should be includedcol.names: a character vector of column names (if not provided, defaults to column names in dataframe)align: “l” (left, default for non-numeric variables), “r” (right, default for numeric variables), “c” (center)We will use the link that appears at the beginning of the document as an example. By using <link url>, the full link is displayed as a hyperlink (http://rmarkdown.rstudio.com). We usually want to use something more descriptive to call the link. You can do this using code formatted as [name of link](link url).
Here are some resources for Markdown in R:
In-text references can be used in the form of footnotes using [^n] where you want the footnote to appear (n is the number of the footnote).
Here is a sentence with a reference to a footnote1
complaints, raises and rating columns in the attitude dataframe. Include “Table 1: attitude” as the table caption, align the table center and do not include the code, only the output.Here is the text to appear in the footnote.↩