2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rmarkdownからdocxファイルをknitする際にテーブルをきれいに生成する

Posted at

Rmarkdownで書類を作成する際に一番困るのがdocxファイルに変換するとき。すべてpdfで扱えれば一番いいのだが、どうしてもワードが必要なときにしょっちゅう出くわす。rmarkdownでknit to wordとする際にkableのテーブルが出力されないので、画像としてきれいに一旦保存して、それを埋め戻すことにする。

なお今回はRstudio Cloudにmagickパッケージがインストールできなかったのでベタ打ちのみ。すべてRstudioのRmdファイル上でKnit to wordボタンを実行。

##ライブラリの読み込み

library(knitr)
library(kableExtra)
library(magick)

##テストデータのインポート


dt <- mtcars[1:5, 1:4]
##https://github.com/haozhu233/kableExtra

kableにてテーブルを作成

###Latex

#from an official example
path <-  kable(dt, format = "latex", booktabs = T, caption = "Demo Table") %>%
  kable_styling(latex_options = c("striped", "hold_position"),
                full_width = F) %>%
  add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
  add_footnote(c("table footnote")) %>% 
  kable_as_image(keep_pdf = T)
    
  include_graphics(path[1])
Untitled.jpeg
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?