こちらを参照した。 taehoonh.me
使用するのはflextableパッケージ www.rdocumentation.org
データの呼び込み
library(tidyverse)
dat <- mtcars[, 1:6] %>%
mutate(model = rownames(mtcars)) %>%
select(ncol(.), 1:(ncol(.)-1))
mtcarsの頭部分を使用する。
サンプルデータ
model mpg cyl disp hp drat wt Mazda RX4 Mazda RX4 21.0 6 160 110 3.90 2.620 Mazda RX4 Wag Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 Datsun 710 Datsun 710 22.8 4 108 93 3.85 2.320 Hornet 4 Drive Hornet 4 Drive 21.4 6 258 110 3.08 3.215 Hornet Sportabout Hornet Sportabout 18.7 8 360 175 3.15 3.440 Valiant Valiant 18.1 6 225 105 2.76 3.460
出力の方法
print(object, preview = "docx")
こちらはワードに出力する場合。 "html", "pptx", "docx", "log"と4種類存在している。主に使うのはPowerPoint形式である"pptx"とワード形式である"docx"ではないかと思う。
とりあえずワードに出力
library(flextable) t01<- flextable(head(dat)) print(t01, preview = "docx")

少し整形する
t02 <-
head(dat) %>%
regulartable() %>%
autofit()
print(t02, preview = "docx")

きれいに揃った。
フッターをつける
基本的にはこういう構造でフッターをつける。
add_footer([your dataframe], [column name] = “text”)
t03 <-
head(dat) %>%
regulartable() %>%
autofit() %>%
add_footer(., model = "* The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).")
print(t03, preview = "docx")

フッターはついたが、一列目についたので、いまいちきれいじゃない。
t04 <-
head(dat) %>%
regulartable() %>%
autofit() %>%
add_footer(., model = "* The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).")%>%
merge_at(., i=1, j = 1:ncol(dat), part = "footer")
print(t04, preview = "docx")
merge_at()コマンドを利用する。

フッターを2つつける。
t05 <-
head(dat) %>%
regulartable() %>%
autofit() %>%
add_footer(., model = "* The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).") %>%
merge_at(., i = 1, j = 1:ncol(dat), part = "footer") %>%
add_footer(., model = "** This is a data frame with 32 observations on 11 (numeric) variables. The source is from Henderson and Velleman (1981), Building multiple regression models interactively. Biometrics, 37, 391–411.", top = F) %>%
merge_at(., i=2, j = 1:ncol(dat), part = "footer")
print(t05, preview = "docx")
パイプでつなげればいいようだ。

ヘッダー・タイトルをつける
t06 <-
head(dat) %>%
regulartable() %>%
autofit() %>%
add_footer(., model = "* The data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).") %>%
merge_at(., i = 1, j = 1:ncol(dat), part = "footer") %>%
add_footer(., model = "** This is a data frame with 32 observations on 11 (numeric) variables. The source is from Henderson and Velleman (1981), Building multiple regression models interactively. Biometrics, 37, 391–411.", top = F) %>%
merge_at(., i=2, j = 1:ncol(dat), part = "footer") %>%
add_header(., model = "Table1. First six car models in the mtcars dataset", top = T) %>%
merge_at(., i = 1, j = 1:ncol(dat), part = "header") %>%
align(., i = 1, j = 1:ncol(dat), align = "center", part = "header")
print(t06, preview = "docx")
パイプで接続するadd_header()を書くとヘッダー・タイトルになる。align = "center"を指定するとセンタリングされる。この辺りはhtml的である。

参照
日本語で解説したべージも発見。