Rで解析:docxやpptxファイルを作成するのに便利「officer」パッケージ

ワードやパワーポイントのdocxとpptxファイル作成に便利なパッケージの紹介です。紹介ではワードファイルのみに注目し紹介します。工夫次第では非常に有用なパッケージだと考えます。

パッケージバージョンは0.4.4。windows11のR version 4.2.2で確認しています。

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("officer")

コマンドの紹介

詳細はコマンド、各パッケージのヘルプを確認してください。

初めにワードファイルを操作するコマンドをいくつか紹介します。コマンドの使用例は下記の「TestDocx2での操作例」を確認してください。

#パッケージの読み込み
library("officer")

#ワードファイルの読み込みまたはひな形を作成:read_docxコマンド
#ファイル読み込み:pathオプション;指定が無ければひな形を作成
TestDocx <- read_docx()
#class&#30906;&#35469;
class(TestDocx)
[1] "docx"

#docx class&#12395;&#27573;&#33853;&#12434;&#36861;&#21152;:body_add_par&#12467;&#12510;&#12531;&#12489;
#&#29694;&#22312;&#12398;&#27573;&#33853;&#12434;&#22522;&#28310;&#12395;&#27573;&#33853;&#12434;&#36861;&#21152;&#12377;&#12427;&#20301;&#32622;&#12434;&#25351;&#23450;:
#pos&#12458;&#12503;&#12471;&#12519;&#12531;;on:&#19978;&#26360;&#12365;,before:&#21069;&#12395;&#36861;&#21152;,after:&#24460;&#12395;&#36861;&#21152;
body_add_par(x = TestDocx,
             value = "&#12390;&#12377;&#12392;&#12289;&#12486;&#12473;&#12488;, TEST, test on",
             style = "Normal",
             pos = "on")

#docx class&#12434;&#20445;&#23384;:print&#12467;&#12510;&#12531;&#12489;
#&#20445;&#23384;&#22580;&#25152;&#12434;&#25351;&#23450;:target&#12458;&#12503;&#12471;&#12519;&#12531;
#&#12501;&#12449;&#12452;&#12523;&#21517;&#12398;&#12415;&#12384;&#12392;&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;&#20445;&#23384;
print(TestDocx, target = "TEST.docx")

##########################################################
###&#20197;&#19979;&#12289;&#12467;&#12510;&#12531;&#12489;&#12398;&#32057;&#20171;&#12394;&#12398;&#12391;&#23455;&#34892;&#12375;&#12390;&#12418;&#12456;&#12521;&#12540;&#12364;&#20986;&#12414;&#12377;#####
###&#12467;&#12510;&#12531;&#12489;&#12398;&#20351;&#29992;&#20363;&#12399;&#19979;&#35352;&#12398;&#12300;TestDocx2&#12391;&#12398;&#25805;&#20316;&#20363;&#12301;&#12434;&#21442;&#29031;##
###x&#12458;&#12503;&#12471;&#12519;&#12531;&#12395;&#12399;&#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;&#12434;&#35373;&#23450;&#12375;&#12414;&#12377;##########
##########################################################
#&#27573;&#33853;&#12398;&#20301;&#32622;&#12434;&#25351;&#23450;&#12377;&#12427;:cursor_XXXX&#12467;&#12510;&#12531;&#12489;
#docx class&#12434;&#25351;&#23450;:x&#12458;&#12503;&#12471;&#12519;&#12531;
#&#20808;&#38957;:cursor_begin&#12467;&#12510;&#12531;&#12489;
cursor_begin(x = &#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;)
#&#26368;&#24460;:cursor_end&#12467;&#12510;&#12531;&#12489;
cursor_end(x = &#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;)
#&#12461;&#12540;&#12527;&#12540;&#12489;&#12391;&#25351;&#23450;:cursor_reach&#12467;&#12510;&#12531;&#12489;
cursor_reach(x = &#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;, keyword = &#12461;&#12540;&#12527;&#12540;&#12489;)
#&#29694;&#22312;&#20301;&#32622;&#12398;&#24460;&#12429;:cursor_backward&#12467;&#12510;&#12531;&#12489;
cursor_backward(x = &#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;)
#&#29694;&#22312;&#20301;&#32622;&#12398;&#21069;:cursor_forward&#12467;&#12510;&#12531;&#12489;
cursor_forward(x = &#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;)

#&#29694;&#22312;&#20301;&#32622;&#12398;&#27573;&#33853;&#12434;&#21066;&#38500;:body_remove&#12467;&#12510;&#12531;&#12489;
#docx class&#12434;&#25351;&#23450;:x&#12458;&#12503;&#12471;&#12519;&#12531;
body_remove(x = &#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;)

#&#29694;&#22312;&#20301;&#32622;&#12398;&#27573;&#33853;&#12395;&#30011;&#20687;&#12434;&#36861;&#21152;:body_add_img&#12467;&#12510;&#12531;&#12489;
#&#30011;&#20687;&#12501;&#12449;&#12452;&#12523;&#12497;&#12473;&#12434;&#25351;&#23450;:src&#12458;&#12503;&#12471;&#12519;&#12531;
body_add_img(x = &#12527;&#12540;&#12489;&#12489;&#12461;&#12517;&#12513;&#12531;&#12488;, src,
&#12288;&#12288;&#12288;&#12288;&#12288;&#12288; width = &#24133;, height = &#39640;&#12373;,
             style = "centered", pos = "after")

#&#29694;&#22312;&#20301;&#32622;&#12398;&#27573;&#33853;&#12395;&#12486;&#12540;&#12502;&#12523;&#12434;&#36861;&#21152;:body_add_table&#12467;&#12510;&#12531;&#12489;
#&#12487;&#12540;&#12479;&#12434;&#25351;&#23450;:value&#12458;&#12503;&#12471;&#12519;&#12531;
#&#12486;&#12540;&#12502;&#12523;&#12501;&#12457;&#12540;&#12510;&#12483;&#12488;&#12434;&#25351;&#23450;:style&#12458;&#12503;&#12471;&#12519;&#12531;;"Normal Table",
#"table_template","Light List Accent 2","Table Professional"&#12364;&#25351;&#23450;&#21487;&#33021;
body_add_table(x = TestDocx, value, style, pos = "after", first_row = TRUE,
               first_column = FALSE, last_row = FALSE, last_column = FALSE,
               no_hband = FALSE, no_vband = TRUE)

・TestDocx2での操作例

###&#12527;&#12540;&#12489;&#12501;&#12449;&#12452;&#12523;&#20316;&#25104;#####
TestDocx2 <- read_docx()
#&#27573;&#33853;&#12434;&#36861;&#21152;
body_add_par(x = TestDocx2,
             value = "&#27573;&#33853;1&#12289;&#12390;&#12377;&#12392;&#12289;&#12486;&#12473;&#12488;, TEST, test on",
             style = "Normal",
             pos = "on")
body_add_par(x = TestDocx2,
             value = paste(1, 3, 5, 99999, 8, collapse = " "),
             style = "Normal",
             pos = "after")
body_add_par(x = TestDocx2,
             value = paste(c(LETTERS[1:24], "AAA", letters[1:5]), collapse = " "),
             style = "Normal",
             pos = "after")
########

###cursor_reach&#12392;body_remove&#12467;&#12510;&#12531;&#12489;&#12398;&#32068;&#12415;&#21512;&#12431;&#12379;&#20363;#####
#TestDocx2&#12398;2&#27573;&#33853;&#30446;&#12434;&#21066;&#38500;
cursor_reach(x = TestDocx2, keyword = 99999)
body_remove(x = TestDocx2)
#&#20445;&#23384;
print(TestDocx2, target = "TEST2.docx")
########

###body_add_img&#12392;body_add_table&#12467;&#12510;&#12531;&#12489;&#12398;&#32068;&#12415;&#21512;&#12431;&#12379;&#20363;#####
#&#12487;&#12540;&#12479;&#20363;&#12398;&#20316;&#25104;
n <- 15
TestData <- data.frame("Group" = sample(paste0("Group", 1:5), n, replace = TRUE),
                          "x" = sample(c(1:100), n, replace = TRUE),
                          "y" = sample(c(1:200), n, replace = TRUE))

#temp&#12501;&#12457;&#12523;&#12480;&#12395;&#30011;&#20687;&#12434;&#20316;&#25104;
src <- tempfile(fileext = ".png")
png(filename = src, width = 5, height = 6, units = 'in', res = 300)
library("ggplot2")
ggplot(TestData, aes(x, y)) +
  geom_point()
dev.off()
#TestDocx2&#12398;TEST&#12434;&#21547;&#12416;&#27573;&#33853;&#12434;&#36984;&#25246;
cursor_reach(x = TestDocx2, keyword = "TEST")
#&#30011;&#20687;&#12434;&#24046;&#12375;&#36796;&#12416;
body_add_img(x = TestDocx2, src = src, width = 3, height = 3,
             style = "centered", pos = "after")
#&#12486;&#12540;&#12502;&#12523;&#12434;&#24046;&#12375;&#36796;&#12416;
body_add_table(x = TestDocx2, value = TestData,
               style = "Light List Accent 2", pos = "after", first_row = TRUE,
               first_column = FALSE, last_row = FALSE, last_column = FALSE,
               no_hband = FALSE, no_vband = TRUE)
#&#20445;&#23384;
print(TestDocx2, target = "TEST2.docx")
########

出力例


少しでも、あなたの解析が楽になりますように!!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました