Abstract
Rmarkdownの書き方を調べたその都度に、追記していく。htmlドキュメントを想定している。title: <span style="font-style:italic">Rmarkdown</span>の書き方 部分的にitalicにする場合はこのように
date: “` r Sys.Date() `” 現在の日時が書き出される。
ページに使用するBootstrapテーマを指定 theme: cerulean
default, cerulean, journal, flatly, readable, spacelab, united, cosmo, lumen, paper, sandstone, yeti
スタイルシート指定css:style.css
シンタックスハイライトのスタイルを指定highlight: pygments
default, tango, pygments, kate, monochrome, espresso, zenburn, haddock, textmate, nullから選ぶ
toc: yes
toc_float: yes
collapsed: truesmooth_scroll: true目次を生成する階層指定toc_depth: 2
項目ごとに番号を振るnumber_sections: true
コードの折りたたみcode_folding: hide
---
title: <span style="font-style:italic">Rmarkdown</span>の書き方
author: "S.Konishi"
date: "2020-02-26"
abstract: "Rmarkdownの書き方を調べたその都度に、追記していく。htmlドキュメントを想定している。"
output:
html_document:
css: style.css
highlight: pygments
theme: cerulean
df_print: kable
toc: true
toc_depth: 2
toc_float:
collapsed: true
smooth_scroll: true
number_section: true
code_folding: hide
---
body{ /* Normal */
font-size: 13px;
}
td { /* Table */
font-size: 12px;
}
h1.title {
font-size: 26px;
}
h1 {
font-size: 22px;
}
h2 {
font-size: 18px;
color: dimgray;
}
h3 {
font-size: 16px;
color: dimgray;
}
{r setup} スタートアップ{r warning=F, message=F} 警告, メッセージの抑制{r eval=F} コードを実行するかしないか{r include=F} chunk・実行結果を出力に入れるか否か。(コードは実行される){r resluts="hold"}
{r fig.align = "right", fig.width=7, fig.height=3, dev='jpeg'}
* item 1
+ sub 1
* item 2
+ sub 1
- sub-sub 1
- sub-sub 2
* item 3
- item1
- sub1
- sub-sub1
- sub-sub2
- item2
- sub2
1. ordered
2. ordered
i) sub 1
ii) sub 2
A. sub-sub-item 1
B. sub-sub-item 2
(@) ordered 1
(@) ordered 2
* 太字は**アスタリスク2つ**で囲む
* 訂正線は~~波線2つ~~で囲む。
* イタリックはアスタリスク1つで囲む。*Cricetulus griseus*
* 下付き文字は波線1つで囲むH~2~O~2~
* 上付き文字はキャレット(^)で囲む O~2~^.-^
* htmlリンク[Rpubs](http://rpubs.com/)
* Horizontal Rule / Page Break 3つ以上のアスタリスクまたはダッシュ
***
---
[Rpubs](http://rpubs.com/) ` r date() `
# 項目をタブで分割 {.tabset}
## A
(tab content)
## B
(tab content)
# 相関のある2つの母集団
vcor <- function(n, sd, rho){
x <- rnorm(n = n, mean = 0, sd = 3)
e1 <- rnorm(n = n, mean = 0, sd = sd)
e2 <- rnorm(n = n, mean = 0, sd = sd)
if (rho < 0) {
y1 <- sqrt(abs(rho)) * x + sqrt(1 - abs(rho)) * e1
y2 <- (sqrt(abs(rho)) * x + sqrt(1 - abs(rho)) * e2) * (-1)
} else {
y1 <- sqrt(abs(rho)) * x + sqrt(1 - abs(rho)) * e1
y2 <- sqrt(abs(rho)) * x + sqrt(1 - abs(rho)) * e2
}
return(list(y1, y2))
}format “latex”, “html”, “markdown”, “pandoc”, “rst”,align “l” (left), “c” (center), “r” (right)results="asis"を指定、そのまま出力。paddingknitr::kable(head(iris), format = "latex", align = "c", caption = "latex")
knitr::kable(head(iris), format = "html", align = "c", caption = "html")
knitr::kable(head(iris), format = "markdown", align = "l", caption = "markdown")
knitr::kable(head(iris), format = "pandoc", align = "r", caption = "pandoc", padding = 0)| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
kableExtraを用いる場合kable_stylingで体裁を変える。bootstrap_options “striped”, “bordered”, “hover”, “condensed” and “responsive.”position="float_left" とposition="right"と、それぞれすることでside-by-sideで並べることができる。library(kableExtra); library(dplyr)
dt1 <- head(InsectSprays); dt2 <- mtcars[1:6,1:3]; dt3 <- swiss[1:6,1:3]
kableExtra::kable(dt1, format = "html", align = 'c', caption = 'striped') %>%
kableExtra::kable_styling(bootstrap_options = "striped", full_width = F, position = "float_left")
kableExtra::kable(dt2, format = "html", align = 'c', caption = 'bordered') %>%
kableExtra::kable_styling(bootstrap_options = "bordered", full_width = F, position = "float_left")
kableExtra::kable(dt3, format = "html", align = 'c', caption = 'hover') %>%
kableExtra::kable_styling(bootstrap_options = "hover", full_width = F, position = "right")| count | spray |
|---|---|
| 10 | A |
| 7 | A |
| 20 | A |
| 14 | A |
| 14 | A |
| 12 | A |
| mpg | cyl | disp | |
|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160 |
| Mazda RX4 Wag | 21.0 | 6 | 160 |
| Datsun 710 | 22.8 | 4 | 108 |
| Hornet 4 Drive | 21.4 | 6 | 258 |
| Hornet Sportabout | 18.7 | 8 | 360 |
| Valiant | 18.1 | 6 | 225 |
| Fertility | Agriculture | Examination | |
|---|---|---|---|
| Courtelary | 80.2 | 17.0 | 15 |
| Delemont | 83.1 | 45.1 | 6 |
| Franches-Mnt | 92.5 | 39.7 | 5 |
| Moutier | 85.8 | 36.5 | 12 |
| Neuveville | 76.9 | 43.5 | 17 |
| Porrentruy | 76.1 | 35.3 | 9 |
formattable テーブルの体裁はlistで設定color_bar 変数の範囲でカラーバー表示color_tile 変数の範囲で指定した色で塗りつぶす(テーブルをheatmapにする)formatterpacman::p_load(formattable, dplyr)
pal <- c( "#41B6C4", "#78C679", "#FB6A4A", "#F768A1", "#969696")
iris.mean <- iris %>%
group_by(Species) %>%
summarise_at(1:4, mean)
formattable(iris.mean, caption="irisデータ(平均)",
list(Sepal.Length = color_bar(pal[1]),
Sepal.Width = color_bar(pal[2]),
Petal.Length = color_bar(pal[3]),
Petal.Width = color_bar(pal[4])
))| Species | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width |
|---|---|---|---|---|
| setosa | 5.006 | 3.428 | 1.462 | 0.246 |
| versicolor | 5.936 | 2.770 | 4.260 | 1.326 |
| virginica | 6.588 | 2.974 | 5.552 | 2.026 |
DT::datatablemdtable <- function(dat, add_row = FALSE){
# function of convert a data.frame to table of markdown format output
convertmd <- function(d){
d <- data.frame(lapply(d, as.character), stringsAsFactors=F, check.names = F)
cat(paste0("|", paste(names(d), collapse = " | "), " |","\n")) # header
cat(paste0(paste(rep("| --- ", ncol(d)), collapse = ""), "|", "\n")) #
invisible(sapply(1:nrow(d), function(i){cat(paste0("| ",paste(d[i,], collapse = " | "), "|", "\n"))}))
}
# add row
if (add_row == T){
dat <- tibble::rownames_to_column(dat)
names(dat)[1] <- " "
}
# argument check and convertmd
if(any(class(dat)=="data.frame")){
convertmd(d = dat)
} else if (any(class(dat)=="matrix")){
dat <- as.data.frame(dat)
convertmd(d = dat)
} else {
stop("d must be a data.frame, tbl, or matrix.")
}
}
mdtable(head(iris))## |Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
## | --- | --- | --- | --- | --- |
## | 5.1 | 3.5 | 1.4 | 0.2 | setosa|
## | 4.9 | 3 | 1.4 | 0.2 | setosa|
## | 4.7 | 3.2 | 1.3 | 0.2 | setosa|
## | 4.6 | 3.1 | 1.5 | 0.2 | setosa|
## | 5 | 3.6 | 1.4 | 0.2 | setosa|
## | 5.4 | 3.9 | 1.7 | 0.4 | setosa|
reticulateを使って、pythonコードチャンクで定義された変数xを以降のrチャンク内でも、呼び出すことができる```{python}
x = 10
```
```{r}
py$x
```
```{r}
py$y = 1:5
```
```{python}
print(py$y )
```
## R version 3.5.2 (2018-12-20)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13.6
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
##
## locale:
## [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] reticulate_1.13 DT_0.8 formattable_0.2.0.1
## [4] dplyr_0.8.3 kableExtra_1.1.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.3 later_0.8.0 pillar_1.4.2
## [4] compiler_3.5.2 highr_0.8 tools_3.5.2
## [7] zeallot_0.1.0 digest_0.6.23 lattice_0.20-38
## [10] jsonlite_1.6 evaluate_0.14 tibble_2.1.3
## [13] lifecycle_0.1.0 viridisLite_0.3.0 pkgconfig_2.0.3
## [16] rlang_0.4.2 Matrix_1.2-17 shiny_1.3.2
## [19] rstudioapi_0.10 crosstalk_1.0.0 yaml_2.2.0
## [22] xfun_0.9 stringr_1.4.0 httr_1.4.1
## [25] knitr_1.24 xml2_1.2.2 htmlwidgets_1.3
## [28] vctrs_0.2.1 hms_0.5.1 grid_3.5.2
## [31] webshot_0.5.1 tidyselect_0.2.5 glue_1.3.1
## [34] R6_2.4.1 pacman_0.5.1 rmarkdown_1.15
## [37] purrr_0.3.3 readr_1.3.1 magrittr_1.5
## [40] promises_1.0.1 backports_1.1.5 scales_1.1.0
## [43] htmltools_0.3.6 assertthat_0.2.1 rvest_0.3.4
## [46] xtable_1.8-4 mime_0.7 colorspace_1.4-1
## [49] httpuv_1.5.1 stringi_1.4.3 munsell_0.5.0
## [52] crayon_1.3.4