わさびさんやくろきちさんと、
定期的なもくもく会あたりから始めます
Word/パワポで凝った表組みするなら
flextable
パッケージが唯一の選択肢
ただし使いやすくはない
↓
つ install.packages('ftExtra')
Package | HTML | Docx | Pptx | |
knitr | o | o | o | o |
xtable | o | o | x | x |
rstudio/gt | o | o | x | x |
flextable | o | x | o | o |
x
は消えるkableExtra
パッケージがあるrstudio/gt
は使いやすいがCRANに来る気配がないマークダウン♡
tibble::tibble(
x = '***ft^Extra^***', y = 'is *Cool*'
) %>%
ftExtra::as_flextable() %>%
ftExtra::colformat_md()
x | y |
ftExtra | is Cool |
tibble::tibble(x = 'ftExtra', y = 'is Cool') %>%
flextable::flextable() %>%
flextable::compose(
1, 'x',
flextable::as_paragraph(
'ft', flextable::as_sup('Extra')
)
) %>%
flextable::bold(1, 'x') %>%
flextable::italic(j = c('x', 'y'))
x | y |
ftExtra | is Cool |
tibble::tibble(酸化物 = c('SiO2', 'Al2O3'),
紅柱石 = c(37.08, 69.92)) %>%
dplyr::mutate_at(
'酸化物', stringr::str_replace_all,
'([:number:]+)', '~\\1~'
) %>%
ftExtra::as_flextable() %>%
ftExtra::colformat_md()
酸化物 | 紅柱石 |
SiO2 | 37.08 |
Al2O3 | 69.92 |
前準備
Petal.Length | Petal.Width |
Species: setosa | |
1.4 | 0.2 |
1.4 | 0.2 |
Species: versicolor | |
4.7 | 1.4 |
4.5 | 1.5 |
Species | Petal.Length | Petal.Width |
setosa | 1.4 | 0.2 |
1.4 | 0.2 | |
versicolor | 4.7 | 1.4 |
4.5 | 1.5 |
できるだけtidyverseを使いたいところ
Petal.Length | Petal.Width |
Species: setosa | |
1.4 | 0.2 |
1.4 | 0.2 |
Species: versicolor | |
4.7 | 1.4 |
4.5 | 1.5 |
iris[c(1:2, 51:52), 3:5] %>%
dplyr::select(Species, everything()) %>%
flextable::as_flextable() %>%
flextable::merge_v('Species') %>%
flextable::theme_vanilla() %>% # テーマの再設定
flextable::fix_border_issues() # 枠線の修復
Species | Petal.Length | Petal.Width |
setosa | 1.4 | 0.2 |
1.4 | 0.2 | |
versicolor | 4.7 | 1.4 |
4.5 | 1.5 |
separate_header
Sepal | Sepal | Petal | Petal | Species |
Length | Width | Length | Width | |
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 |
span_header
Sepal | Petal | Species | ||
Length | Width | Length | Width | |
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 |
# 見出しの階層性を表現するデータフレームを容易
header <- tibble::tibble(key = names(iris)) %>%
tidyr::separate('key', into = c('level1', 'level2'), remove = FALSE)
flextable::flextable(iris[1:3, ]) %>%
flextable::set_header_df(
# 事前に準備した見出しをマッピング
mapping = header, key = 'key'
) %>%
flextable::theme_booktabs() %>%
flextable::fix_border_issues()
Sepal | Sepal | Petal | Petal | Species |
Length | Width | Length | Width | |
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 |