R Markdown do html

Arnošt Komárek

https://msekce.karlin.mff.cuni.cz/~komarek

Basics

Path to the working directory

ROOT <- "/home/komarek/teach/mff_2023/nmst547_AdvRko/Tutorial12/Rmd/"

Load data being previously saved in the R format

load(paste(ROOT, "../Data/auta2004.RData", sep = ""))

Mean consumption

mean(auta$spotreba, na.rm = TRUE)
## [1] 10.70616

The result can also be shown without showing the code.

## [1] 10.70616

It is also possible to show code only while not showing the result.

mean(auta$spotreba, na.rm = TRUE)

It is also possible to calculate something while showing neither the code nor the result.

The result can also be used inside a sentence within a text. Mean consumption of cars in the dataset is 10.71 l/100 km.

Let \(y_1,\ldots,y_n\) denote consumption values in data. The above mentioned sample mean is given by $$ \overline{y} = \frac{1}{n}\sum_{i=1}^n y_i, $$ where \(\sum_{i=1}^n y_i = 4432.35\) and \(n = 414\).

Text can be easily formatted as italic or in bold.


Figures

Boxplot and histogram

Boxplot is a useful plot which graphically shows the most important descriptive statistics. Histogram provides more information on the distribution (it is a basic density estimator).

par(mfrow = c(1, 2))
boxplot(auta$spotreba, ylab = "Consumption [l/100 km]", col = "cadetblue3")
hist(auta$spotreba, prob = TRUE, xlab = "Consumption [l/100 km]", ylab = "Density", 
     col = "cadetblue3", main = "")
Krabičkový graf spotřeby.

Krabičkový graf spotřeby.


Tables using the xtable package

Descriptive statistics of consumption (spotreba) given drive of the car (fnahon) saved in a form of the data.frame.

spotr <- data.frame(
   Mean   = with(auta, tapply(spotreba, fnahon, mean, na.rm = TRUE)),
   SD     = with(auta, tapply(spotreba, fnahon, sd, na.rm = TRUE)),
   Median = with(auta, tapply(spotreba, fnahon, median, na.rm = TRUE)),
   Q1     = with(auta, tapply(spotreba, fnahon, quantile, prob = 0.25, na.rm = TRUE)),
   Q3     = with(auta, tapply(spotreba, fnahon, quantile, prob = 0.75, na.rm = TRUE)),
   n      = with(auta, tapply(!is.na(spotreba), fnahon, sum)),
   NAs    = with(auta, tapply(is.na(spotreba),  fnahon, sum)))
print(spotr)
##             Mean       SD Median    Q1    Q3   n NAs
## predni  9.674306 1.888841  9.800  8.45 10.70 216  10
## zadni  11.293981 1.293581 11.250 10.55 11.85 108   2
## 4x4    12.477222 2.339009 11.725 10.70 14.05  90   2

Change names of rows and columnsZměna názvu řádků a sloupců

colnames(spotr) <- c("Mean", "Std. Dev.", "Median", "Q1", "Q3", "n", "Missing")
rownames(spotr) <- c("Front", "Rear", "4x4")
print(spotr)
##            Mean Std. Dev. Median    Q1    Q3   n Missing
## Front  9.674306  1.888841  9.800  8.45 10.70 216      10
## Rear  11.293981  1.293581 11.250 10.55 11.85 108       2
## 4x4   12.477222  2.339009 11.725 10.70 14.05  90       2

Package xtable

Package xtable provides possibilities to format rectangular structures (matrix, data.frame) into a table written in either the LaTeX or html code.

library("xtable")

Format the table

tspotr <- xtable(spotr, align   = c("l", rep("c", 7)), 
                        digits  = c(0, rep(2, 5), 0, 0),
                        caption = "Table of descriptive statistics.", 
                        label   = "tab:popis")
print(tspotr, type = "html")
## <!-- html table generated in R 4.3.2 by xtable 1.8-4 package -->
## <!-- Thu Dec  7 14:53:12 2023 -->
## <table border=1>
## <caption align="bottom"> Table of descriptive statistics. </caption>
## <tr> <th>  </th> <th> Mean </th> <th> Std. Dev. </th> <th> Median </th> <th> Q1 </th> <th> Q3 </th> <th> n </th> <th> Missing </th>  </tr>
##   <tr> <td> Front </td> <td align="center"> 9.67 </td> <td align="center"> 1.89 </td> <td align="center"> 9.80 </td> <td align="center"> 8.45 </td> <td align="center"> 10.70 </td> <td align="center"> 216 </td> <td align="center"> 10 </td> </tr>
##   <tr> <td> Rear </td> <td align="center"> 11.29 </td> <td align="center"> 1.29 </td> <td align="center"> 11.25 </td> <td align="center"> 10.55 </td> <td align="center"> 11.85 </td> <td align="center"> 108 </td> <td align="center"> 2 </td> </tr>
##   <tr> <td> 4x4 </td> <td align="center"> 12.48 </td> <td align="center"> 2.34 </td> <td align="center"> 11.72 </td> <td align="center"> 10.70 </td> <td align="center"> 14.05 </td> <td align="center"> 90 </td> <td align="center"> 2 </td> </tr>
##    <a name=tab:popis></a>
## </table>

Table in the output

Table of descriptive statistics.
Mean Std. Dev. Median Q1 Q3 n Missing
Front 9.67 1.89 9.80 8.45 10.70 216 10
Rear 11.29 1.29 11.25 10.55 11.85 108 2
4x4 12.48 2.34 11.72 10.70 14.05 90 2

Additional possible adjustment, see help(print.xtable).

print(tspotr, type = "html", 
              caption.placement = "top")
Table of descriptive statistics.
Mean Std. Dev. Median Q1 Q3 n Missing
Front 9.67 1.89 9.80 8.45 10.70 216 10
Rear 11.29 1.29 11.25 10.55 11.85 108 2
4x4 12.48 2.34 11.72 10.70 14.05 90 2

Automatized documents preparation.

More on R Markdown

R Markdown and R package knitr

Sweave

Sweave can be used to integrate LaTeX and R code/output. For more see https://stat.ethz.ch/R-manual/R-devel/library/utils/doc/Sweave.pdf


Shiny

Shiny can be used to create html documents with interactive items (user influences what should be displayed), see http://shiny.rstudio.com/