Basic Skills in R Markdown
The HTML file (October, 2021)
1 Introduction
This document provides basic tools to produce a html file using R markdown. The best way to use this document is to run the file in R studio and then read the .Rmd file to see how the output was created. The file can be used to produce a very basic html document an you can add later more components to you document.
2 Sections and subsections
This is a an example of a R markdown file that produces htnl output. This is a section in the document.
2.1 Subsection
This text apears in a subsection
2.1.1 Subsubsection
This text is a part of a subsection.
3 Including R code
3.1 Print R code and output
This is an example how to include R code and output in the document. We use the airquality data as an example.
x<-na.omit(airquality$Ozone)
print(x)
## [1] 41 36 12 18 28 23 19 8 7 16 11 14 18 14 34 6 30 11 1 11 4 32 23
## [24] 45 115 37 29 71 39 23 21 37 20 12 13 135 49 32 64 40 77 97 97 85 10 27
## [47] 7 48 35 61 79 63 16 80 108 20 52 82 50 64 59 39 9 16 78 35 66 122 89
## [70] 110 44 28 65 22 59 23 31 44 21 9 45 168 73 76 118 84 85 96 78 73 91 47
## [93] 32 20 23 21 24 44 21 28 9 13 46 18 13 24 16 13 23 36 7 14 30 14 18
## [116] 20
## attr(,"na.action")
## [1] 5 10 25 26 27 32 33 34 35 36 37 39 42 43 45 46 52 53 54 55 56 57 58 59
## [25] 60 61 65 72 75 83 84 102 103 107 115 119 150
## attr(,"class")
## [1] "omit"
3.2 Do not print the R code but print the output
If we do not want to print the R code, but we want to see the output use the option echo=FALSE:
## [1] 41 36 12 18 28 23 19 8 7 16 11 14 18 14 34 6 30 11 1 11 4 32 23
## [24] 45 115 37 29 71 39 23 21 37 20 12 13 135 49 32 64 40 77 97 97 85 10 27
## [47] 7 48 35 61 79 63 16 80 108 20 52 82 50 64 59 39 9 16 78 35 66 122 89
## [70] 110 44 28 65 22 59 23 31 44 21 9 45 168 73 76 118 84 85 96 78 73 91 47
## [93] 32 20 23 21 24 44 21 28 9 13 46 18 13 24 16 13 23 36 7 14 30 14 18
## [116] 20
## attr(,"na.action")
## [1] 5 10 25 26 27 32 33 34 35 36 37 39 42 43 45 46 52 53 54 55 56 57 58 59
## [25] 60 61 65 72 75 83 84 102 103 107 115 119 150
## attr(,"class")
## [1] "omit"
4 Items
This is a text that contains items:
- Item 1
- Item 2
- Item 3
- Item 4
5 Use R as a part of your text
5.1 Example: summary statistics
The mean Ozone level is
x<-na.omit(airquality$Ozone)
mean(x)
## [1] 42.12931
with variance is equal to
var(x)
## [1] 1088.201
5.2 Graphical displays in the document
A histogram for the Ozone level can be produced using the function qplot with the option geom = “histogram”:
Ozone.R<-data.frame(x)
qplot(x, data = Ozone.R, geom = "histogram", binwidth = 0.1)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
To add a caption to the figure we use {r figchp1,fig.cap=“Sepal length (III)”}.
Ozone.R<-data.frame(x)
qplot(x, data = Ozone.R, geom = "histogram", binwidth = 0.1)
Figure 5.1: Sepal length (III)
We can refer to the figure from the text in the document. For example, Figure 5.2 presents a histogram that was produced using the function qplot() function.
Ozone.R<-data.frame(x)
qplot(x, data = Ozone.R, geom = "histogram", binwidth = 0.1)
Figure 5.2: Sepal length (III)
6 How to add a link to your document
Materials about R markdown are widely available online.
- For a YouTube tutorial about R markdown by Jalayer Academy see YTRmd1.
- For a second YouTube tutorial about R markdown by Roger Peng see YTRmd2.
A usefull link to a R Markdown:
- For a free online book about R markdown see see RMDbook.
7 How to create a math formula
To create a math formula, for example a linear regression model of the form
\[y_{i}=\alpha+\beta \times x_{i} + \varepsilon_{i},\] we need to use \(LaTeX\) syntax.
8 Just do it
8.1 Analysis of the cars data
Fit a linear regression model to the cars datasets in which the response is the stopping distance and the predictor is the car speed.
8.2 Expected outout
Write a short report with the following structure
- Introduction
- The cars data (including a scapter plot of the data)
- Modeling (formulate the model for the cars data)
- Application to the data: present the results including the R object with the regression output. Plot os the data
and fitted model and isgnostic plots.
In your report, include the R code as a part of the text. Produce both html and pdf outputs.