必要なパッケージを読み込み

library(officer) library(tidyverse) library(flextable) library(dplyr)

サンプルデータを作成

data1 <- data.frame( 名前 = c(“山田”, “佐藤”, “田中”), 年齢 = c(28, 34, 23), 部署 = c(“営業”, “経理”, “開発”) )

data2 <- data.frame( 商品 = c(“A”, “B”, “C”, “D”, “E”), 売上 = c(120, 140, 100, 160, 180), 利益 = c(30, 40, 20, 50, 60), 在庫 = c(10, 15, 5, 20, 25) )

flextableオブジェクトを作成

ft1 <- flextable(data1) ft2 <- flextable(data2)

ft1とft2のスタイルを適用する関数

customize_tables <- function(ft1, ft2) { # ft1のスタイルを適用 ft1 <- bg(ft1, bg = “lightblue”, part = “header”) ft1 <- bold(ft1, bold = TRUE, part = “header”) ft1 <- autofit(ft1)

# ft2のカラム数と行数を取得 num_cols <- ncol_keys(ft2) num_rows <- nrow_part(ft2, part = “body”)

ft2 <- bg(ft2, bg = “lightgreen”, part = “header”) ft2 <- bold(ft2, bold = TRUE, part = “header”)

# カラムごとのスタイル設定 for (col in 1:num_cols) { ft2 <- bg(ft2, j = col, bg = ifelse(col %% 2 == 0, “lightgray”, “white”)) }

# 行ごとのスタイル設定 for (row in 1:num_rows) { ft2 <- bold(ft2, i = row, bold = ifelse(row %% 2 == 0, TRUE, FALSE)) }

ft2 <- autofit(ft2)

return(list(ft1 = ft1, ft2 = ft2)) }

関数を使ってft1とft2にスタイルを適用

result <- customize_tables(ft1, ft2) ft1 <- result\(ft1 ft2 <- result\)ft2

行数を揃えるために空白の行を追加

num_rows_ft1 <- nrow_part(ft1, part = “body”) num_rows_ft2 <- nrow_part(ft2, part = “body”)

if (num_rows_ft1 < num_rows_ft2) { for (i in 1:(num_rows_ft2 - num_rows_ft1)) { ft1 <- add_row(ft1, values = list(名前 = ““, 年齢 =”“, 部署 =”“)) } } else if (num_rows_ft2 < num_rows_ft1) { for (i in 1:(num_rows_ft1 - num_rows_ft2)) { ft2 <- add_row(ft2, values = list(商品 =”“, 売上 =”“, 利益 =”“, 在庫 =”“)) } }

空白の列を作成

empty_col <- data.frame(空白 = rep(““, max(num_rows_ft1, num_rows_ft2)))

データフレームを結合

combined_data <- bind_cols(ft1\(body\)dataset, empty_col, ft2\(body\)dataset)

新しいflextableオブジェクトを作成

ft_combined <- flextable(combined_data)

結合後のスタイルを設定する関数

set_combined_style <- function(ft_combined, ft1, ft2) { num_cols_ft1 <- ncol_keys(ft1) num_rows_ft1 <- nrow_part(ft1, part = “body”) num_cols_ft2 <- ncol_keys(ft2) num_rows_ft2 <- nrow_part(ft2, part = “body”)

# ヘッダーのスタイル設定 ft_combined <- bg(ft_combined, bg = “lightblue”, part = “header”) ft_combined <- bold(ft_combined, bold = TRUE, part = “header”) ft_combined <- fontsize(ft_combined, size = 12, part = “header”)

# ft1 部分のカラムごとのスタイル設定 for (col in 1:num_cols_ft1) { ft_combined <- bg(ft_combined, j = col, bg = ifelse(col %% 2 == 0, “lightgray”, “white”)) ft_combined <- border(ft_combined, j = col, border = fp_border(color = “black”, width = 2)) }

# ft2 部分のカラムごとのスタイル設定 for (col in (num_cols_ft1 + 2):(num_cols_ft1 + 2 + num_cols_ft2 - 1)) { ft_combined <- bg(ft_combined, j = col, bg = ifelse(col %% 2 == 0, “lightgray”, “white”)) ft_combined <- border(ft_combined, j = col, border = fp_border(color = “black”, width = 2)) }

# 行ごとのスタイル設定 for (row in 1:nrow(ft_combined\(body\)dataset)) { ft_combined <- bold(ft_combined, i = row, bold = ifelse(row %% 2 == 0, TRUE, FALSE)) ft_combined <- padding(ft_combined, i = row, padding = 6) }

# フォントスタイルの変更 ft_combined <- font(ft_combined, fontname = “Arial”, part = “all”)

# 全体のサイズを調整 ft_combined <- autofit(ft_combined)

return(ft_combined) }

結合後のスタイルを適用

ft_combined <- set_combined_style(ft_combined, ft1, ft2)

Wordドキュメントを作成

doc <- read_docx()

結合したテーブルを追加

doc <- doc %>% body_add_par(“2つの表を横に並べた表示”, style = “heading 1”) %>% body_add_flextable(ft_combined) %>% body_add_par(” “, style =”Normal”) # 改行を追加

Wordドキュメントを保存

print(doc, target = “two_tables_side_by_side.docx”)


## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:


``` r
summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.