Rで解析:エクセルファイルの作成が大変便利!!「openxlsx」パッケージ

Rの解析に役に立つ記事
スポンサーリンク

エクセルファイルの作成に大変便利な「openxlsx」パッケージのコマンドを紹介です。パッケージでは「createWorkbook」コマンドで「Workbook object」を作成、データの追加、セルの体裁を条件付き書式や罫線で整える、「ggplot2」パッケージなどの出力を追加してエクセルファイルの作成が可能です。

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

連続でコマンドを使用したい方は「openxlsx2」パッケージがおすすめです。

スポンサーリンク

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

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

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

実行コマンドの前に

実行コマンドの前に下記コマンドを実行し、各種パッケージを読み込んでください。

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

#tidyverseパッケージがなければインストール
#ggplot2パッケージで図を作成するのに使用します
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

#tkgetOpenFileコマンド読み込みのため実行
library("tcltk")

実行コマンド

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

xlsxファイルの読込み:「read.xlsx」コマンド

#xlsxファイルの読込み:read.xlsxコマンド
#xlsxファイルを選択
XLPath <- paste0(as.character(
  tkgetOpenFile(title = "xlsx&#12501;&#12449;&#12452;&#12523;&#12434;&#36984;&#25246;",
                filetypes = '{"xlsx&#12501;&#12449;&#12452;&#12523;" {".xlsx"}}',
                initialfile = c("*.xlsx"))), collapse = " ")

#xlsx&#12501;&#12449;&#12452;&#12523;&#12398;&#35501;&#12415;&#36796;&#12415;:read.xlsx&#12467;&#12510;&#12531;&#12489;
#&#35501;&#12415;&#36796;&#12415;xlsx&#12501;&#12449;&#12452;&#12523;&#12434;&#25351;&#23450;:xlsxFile&#12458;&#12503;&#12471;&#12519;&#12531;
#&#35501;&#12415;&#36796;&#12416;&#12471;&#12540;&#12488;&#12434;&#25351;&#23450;:sheet&#12458;&#12503;&#12471;&#12519;&#12531;
#&#35501;&#12415;&#36796;&#12415;&#38283;&#22987;&#34892;&#12434;&#25351;&#23450;:startRow&#12458;&#12503;&#12471;&#12519;&#12531;
#&#21015;&#21517;&#12434;&#35373;&#23450;:colNames&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE:&#26368;&#21021;&#12398;&#34892;&#12364;&#21015;&#21517;/FALSE:&#12394;&#12375;
#&#34892;&#21517;&#12434;&#35373;&#23450;:rowNames&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE:&#26368;&#21021;&#12398;&#21015;&#12364;&#34892;&#21517;/FALSE:&#12394;&#12375;
#&#26085;&#20184;&#12398;&#26908;&#20986;&#12392;&#22793;&#25563;&#35373;&#23450;:detectDates&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE
#&#12487;&#12540;&#12479;&#12414;&#12391;&#12398;&#31354;&#21015;&#12398;&#20966;&#29702;:skipEmptyCols&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE:&#31354;&#34892;&#12434;&#12473;&#12461;&#12483;&#12503;/FALSE:&#31354;&#12434;NA
#&#12487;&#12540;&#12479;&#12414;&#12391;&#12398;&#31354;&#34892;&#12398;&#20966;&#29702;:skipEmptyRows&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE:&#31354;&#34892;&#12434;&#12473;&#12461;&#12483;&#12503;/FALSE:&#31354;&#12434;NA
#&#12487;&#12540;&#12479;&#12434;&#35501;&#12415;&#36796;&#12416;&#34892;&#31684;&#22258;:rows&#12458;&#12503;&#12471;&#12519;&#12531;;NULL&#12391;&#20840;&#12390;,&#21336;&#19968;/c(5, 7),c(2:12)&#12394;&#12393;&#12392;&#25351;&#23450;
#&#12487;&#12540;&#12479;&#12434;&#35501;&#12415;&#36796;&#12416;&#21015;&#31684;&#22258;:cols&#12458;&#12503;&#12471;&#12519;&#12531;;NULL&#12391;&#20840;&#12390;,&#21336;&#19968;/c(5, 7),c(2:12)&#12394;&#12393;&#12392;&#25351;&#23450;
#&#21015;&#21517;&#12398;&#37325;&#35079;&#12434;&#12481;&#12455;&#12483;&#12463;:check.names&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE:&#37325;&#35079;&#12375;&#12383;&#22580;&#21512;[.No]&#12434;&#20184;&#19982;/FALSE
#&#21015;&#21517;&#12398;&#31354;&#30333;&#37096;&#20998;&#12395;&#25407;&#20837;:sep.names&#12458;&#12503;&#12471;&#12519;&#12531;
#&#25351;&#23450;&#12375;&#12383;&#21517;&#21069;&#20184;&#12365;&#38936;&#22495;&#12434;&#21462;&#24471;:namedRegion&#12458;&#12503;&#12471;&#12519;&#12531;;NULL/"&#38936;&#22495;&#21517;"&#12434;&#35373;&#23450;
#NA&#20516;&#12392;&#35469;&#35672;&#12377;&#12427;&#25991;&#23383;&#12398;&#35373;&#23450;:na.strings&#12458;&#12503;&#12471;&#12519;&#12531;
#&#12300;&#12475;&#12523;&#12398;&#32080;&#21512;&#12301;&#12395;&#23550;&#12377;&#12427;&#20966;&#29702;:fillMergedCells&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE:&#12475;&#12523;&#32080;&#21512;&#31684;&#22258;&#12395;&#21516;&#12376;&#20516;
read.xlsx(xlsxFile = XLPath, sheet = 1,
          startRow = 1, colNames = TRUE,
          rowNames = FALSE, detectDates = FALSE,
          skipEmptyCols = FALSE, #skipEmptyRows = TRUE, 
          rows = NULL, cols = NULL,
          check.names = FALSE, sep.names = ".",
          namedRegion = NULL, na.strings = "&#12390;&#12377;&#12392;",
          fillMergedCells = FALSE)

xlsxファイルを保存:「write.xlsx」コマンド

#データを指定:xオプション;list clsassに複数データを格納して一括保存も可能
#ファイル名を設定:fileオプション;ファイルパスがなければ作業フォルダに保存
#上書き設定:overwriteオプション;TRUE/FALSE
###セルの体裁はcreateStyleコマンドで別に作成し指定が可能###
###その他パラメータオプションも紹介しています###
###[体裁を整えて保存:write.xlsxコマンド]を参照###
write.xlsx(x = list("Iris" = iris, "Cars" = cars),
           file = "Test.xlsx",
           overwrite = TRUE)

セルの体裁を作成:「createStyle」コマンド

「write.xlsx」コマンドだけでなく、「createWorkbook」コマンドと組み合わせて使用する「addStyle」コマンド、「conditionalFormatting」コマンドでもセルの体裁を設定するのに必要な「Style Object」を作成するコマンドです。後者の方が細かい調整が可能です。

#&#12501;&#12457;&#12531;&#12488;:fontName&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:"Calibri"
#&#12501;&#12457;&#12531;&#12488;&#12469;&#12452;&#12474;:fontSize&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:11
#&#12501;&#12457;&#12531;&#12488;&#33394;:fontColour&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:"black"
#&#12475;&#12523;&#12398;&#26360;&#24335;:numFmt&#12458;&#12503;&#12471;&#12519;&#12531;;"GENERAL","NUMBER","CURRENCY",
##"ACCOUNTING","DATA","LONGDATE","TIME","PERCENTAGE","FRACTION","SCIENTIFIC",
##"TEXT","COMMA",&#12518;&#12540;&#12470;&#12540;&#23450;&#32681;&#12398;'m"&#26376;"d"&#26085;"','0.00'&#12394;&#12393;&#12364;&#21487;&#33021;
#&#32619;&#32218;&#12398;&#20301;&#32622;:border&#12458;&#12503;&#12471;&#12519;&#12531;;"NULL","top","bottom","left","right","TopBottom",
##"LeftRight","TopLeftRight","TopBottomLeftRight"&#12364;&#21487;&#33021;
#&#32619;&#32218;&#12398;&#33394;:borderColour&#12458;&#12503;&#12471;&#12519;&#12531;;border&#12458;&#12503;&#12471;&#12519;&#12531;"NULL"&#20197;&#22806;&#12391;&#36969;&#24540;
#&#32619;&#32218;&#12398;&#12473;&#12479;&#12452;&#12523;:BorderStyle&#12458;&#12503;&#12471;&#12519;&#12531;;border&#12458;&#12503;&#12471;&#12519;&#12531;"NULL"&#20197;&#22806;&#12391;&#36969;&#24540;,
##"none","thin","medium","dashed","dotted","thick","double","hair","mediumDashed",
##"dashDot","mediumDashDot","mediumDashDotDot","slantDashDot"&#12364;&#21487;&#33021;
#&#26465;&#20214;&#20184;&#12365;&#12475;&#12523;&#22615;&#12426;&#33394;:bgFill&#12458;&#12503;&#12471;&#12519;&#12531;;&#26465;&#20214;&#20184;&#12365;&#26360;&#24335;&#12398;&#12415;&#36969;&#24540;
#&#12475;&#12523;&#22615;&#12426;&#33394;:fgFill&#12458;&#12503;&#12471;&#12519;&#12531;;&#36890;&#24120;&#12398;&#32972;&#26223;&#33394;&#12398;&#35373;&#23450;
#&#25991;&#23383;&#27178;&#20301;&#32622;:halign&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL,"left","right","center","justify"&#12364;&#21487;&#33021;
#&#25991;&#23383;&#32294;&#20301;&#32622;:valign&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL,"top","center","buttom"&#12364;&#21487;&#33021;
#&#25991;&#23383;&#12473;&#12479;&#12452;&#12523;:textDecoration&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL,"bold","strikout","italic",
##"underline","underline2","accounting","accounting2"&#12364;&#21487;&#33021;
#&#25991;&#23383;&#12434;&#25240;&#12426;&#36820;&#12375;&#12390;&#20840;&#20307;&#12434;&#34920;&#31034;&#12377;&#12427;:wrapText&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE
#&#25991;&#23383;&#12398;&#26041;&#21521;:textRotation&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL,0:255
#&#25991;&#23383;&#12398;&#12452;&#12531;&#12487;&#12531;&#12488;:indent&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL
#&#20445;&#35703;&#12398;&#12525;&#12483;&#12463;&#35373;&#23450;:locked&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL,TRUE/FALSE
#&#20445;&#35703;&#12398;&#34920;&#31034;&#35373;&#23450;:hidden&#12458;&#12503;&#12471;&#12519;&#12531;:NULL,TRUE/FALSE
CellStyle <- createStyle(fontName = "Arial",
                         fontSize = 12,
                         fontColour = "black",
                         numFmt = 'm"&#26376;"d"&#26085;"',
                         border = "top",
                         borderColour = "black",
                         borderStyle = "none",
                         bgFill = NULL,
                         fgFill = "green",
                         halign = "center",
                         valign = NULL,
                         textDecoration = "accounting2",
                         wrapText = FALSE,
                         textRotation = 0,
                         indent = 1,
                         locked = TRUE,
                         hidden = FALSE)

体裁を整えて保存:「write.xlsx」コマンド

#NA&#12391;&#32622;&#25563;
iris[1, 2:3] <- NA
#&#20445;&#23384;
write.xlsx(x = list("Iris" = iris, "Cars" = cars),
           file = "Test.xlsx",
           overwrite = TRUE,
           #&#12471;&#12540;&#12488;&#21517;&#12434;&#35373;&#23450;:sheetName&#12458;&#12503;&#12471;&#12519;&#12531;
           #&#21336;&#19968;/&#12505;&#12463;&#12488;&#12523;&#12391;&#25351;&#23450;&#12364;&#21487;&#33021;
           sheetName = c("Iris", "&#12367;&#12427;&#12414;"),
           #&#30446;&#30427;&#12426;&#32218;&#12398;&#34920;&#31034;&#35373;&#23450;:gridLines&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE:&#34920;&#31034;/&#38750;&#34920;&#31034;
           #&#21336;&#19968;/&#12505;&#12463;&#12488;&#12523;&#12391;&#25351;&#23450;&#12364;&#21487;&#33021;
           gridLines = c(FALSE,TRUE),
           #&#12479;&#12502;&#12398;&#33394;&#12434;&#35373;&#23450;:tabColour&#12458;&#12503;&#12471;&#12519;&#12531;
           tabColour = c("red", "green"),
           #&#12471;&#12540;&#12488;&#12398;&#25313;&#22823;&#35373;&#23450;:zoom&#12458;&#12503;&#12471;&#12519;&#12531;;10:400&#12398;&#31684;&#22258;
           zoom = c(80, 120),
           #&#26360;&#12365;&#36796;&#12415;&#38283;&#22987;&#21015;&#12434;&#35373;&#23450;:startCol&#12458;&#12503;&#12471;&#12519;&#12531;;c(sheet1, sheet2, ...)
           startCol = c(3, 5),
           #&#26360;&#12365;&#36796;&#12415;&#38283;&#22987;&#34892;&#12434;&#35373;&#23450;:startRow&#12458;&#12503;&#12471;&#12519;&#12531;;c(sheet1, sheet2, ...)/&#19968;&#25324;
           startRow = 2,
           #&#26360;&#12365;&#36796;&#12415;&#38283;&#22987;&#21015;&#34892;&#12398;&#19968;&#25324;&#35373;&#23450;:xy&#12458;&#12503;&#12471;&#12519;&#12531;;c(startCol, startRow)
           #xy = c("A2", "c1"),
           #&#21015;&#21517;&#12398;&#20184;&#19982;;colnames&#12458;&#12503;&#12471;&#12519;&#12531;:TRUE/FALSE
           colNames = TRUE,
           #&#34892;&#21517;&#12398;&#20184;&#19982;:rowNames&#12458;&#12503;&#12471;&#12519;&#12531;:TRUE/FALSE
           rowNames = FALSE,
           #&#34892;&#21517;&#12398;&#20307;&#35009;&#35519;&#25972;:headerStyle&#12458;&#12503;&#12471;&#12519;&#12531;
           #createStyle&#12467;&#12510;&#12531;&#12489;&#12391;&#20316;&#25104;&#12375;&#12383;Style&#12458;&#12502;&#12472;&#12455;&#12463;&#12488;&#12434;&#25351;&#23450;
           headerStyle = CellStyle,
           #&#12487;&#12540;&#12479;&#12434;&#22258;&#12358;&#12424;&#12358;&#12395;&#32619;&#32218;&#12434;&#24341;&#12367;:borders&#12458;&#12503;&#12471;&#12519;&#12531;;
           #NULL,"surrounding","columns","rows"&#12364;&#21487;&#33021;
           borders = "surrounding",
           #borders&#12398;&#33394;:borderColour
           borderColour = "red",
           #border&#12398;&#32218;&#31278;:borderStyle&#12458;&#12503;&#12471;&#12519;&#12531;,
           #createStyle&#12467;&#12510;&#12531;&#12489;&#12300;borderStyle&#12301;&#12458;&#12503;&#12471;&#12519;&#12531;&#21516;&#27096;
           borderStyle = "medium",
           #NA&#12434;&#31354;&#30333;&#12391;&#12394;&#12367;#N/A&#12391;&#20445;&#23384;;TRUE/FALSE
           keepNA = TRUE,
           #&#12454;&#12451;&#12531;&#12489;&#12454;&#26528;&#12398;&#34892;&#22266;&#23450;:firstActiveRow&#12458;&#12503;&#12471;&#12519;&#12531;
           firstActiveRow = 3,
           #&#12454;&#12451;&#12531;&#12489;&#12454;&#26528;&#12398;&#21015;&#22266;&#23450;:firstActiveCol&#12458;&#12503;&#12471;&#12519;&#12531;
           firstActiveCol = 4,
           #&#21015;&#24133;&#12398;&#25351;&#23450;:colWidths&#12458;&#12503;&#12471;&#12519;&#12531;;"auto"/&#25968;&#20516;&#12391;&#25351;&#23450;
           #colWidths = c("auto", "auto"),
           #&#12501;&#12451;&#12523;&#12479;&#12540;&#12434;&#35373;&#23450;:withFilter&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE
           withFilter = c(TRUE, FALSE)
           )

「Workbook object」を細かく体裁を整える

「createWorkbook」コマンドで「Workbook object」を作成し、データの書き込み、各種体裁を整えてエクセルファイルを作成します。実行コマンドではggplot2パッケージの出力をシートに追加する例も紹介します。

上から順番に実行することで出力例のエクセルを作成することが出来ます。

#Workbook object&#12398;&#20316;&#25104;:createWorkbook&#12467;&#12510;&#12531;&#12489;
#&#20316;&#25104;&#32773;&#12398;&#35373;&#23450;:creator&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:&#12525;&#12464;&#12452;&#12531;&#12518;&#12540;&#12470;&#12540;&#21517;
#&#12479;&#12452;&#12488;&#12523;&#12503;&#12525;&#12497;&#12486;&#12451;&#12398;&#35373;&#23450;:title&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL
#&#20214;&#21517;&#12398;&#35373;&#23450;:subject&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL
#&#20998;&#39006;&#12503;&#12525;&#12497;&#12486;&#12451;&#12398;&#35373;&#23450;:category&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL
TestWb <- createWorkbook(creator = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;",
                         title = "KARADA",
                         subject = "GOOD",
                         category = "R&#12486;&#12473;&#12488;")

#&#12527;&#12540;&#12463;&#12471;&#12540;&#12488;&#12398;&#36861;&#21152;:addWorksheet&#12467;&#12510;&#12531;&#12489;
#Workbook object&#12434;&#25351;&#23450;:wb&#12458;&#12503;&#12471;&#12519;&#12531;
#&#20316;&#25104;&#12471;&#12540;&#12488;&#21517;:sheetName&#12458;&#12503;&#12471;&#12519;&#12531;
#&#30446;&#30427;&#12426;&#32218;&#12398;&#34920;&#31034;:gridLines&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE
#&#12479;&#12502;&#33394;:tabColour&#12458;&#12503;&#12471;&#12519;&#12531;;&#21021;&#26399;&#20516;:NULL
#&#12474;&#12540;&#12512;&#35373;&#23450;:zoom&#12458;&#12503;&#12471;&#12519;&#12531;;10:400&#12398;&#31684;&#22258;
###&#19979;&#35352;&#12504;&#12483;&#12480;&#12540;,&#12501;&#12483;&#12479;&#12540;&#12391;&#20351;&#29992;&#21487;&#33021;&#12394;&#12471;&#12519;&#12540;&#12488;&#12467;&#12540;&#12489;###
#&[Page]:&#12506;&#12540;&#12472;&#30058;&#21495;,&[Pages]:&#12506;&#12540;&#12472;&#25968;,
#&[Date]:&#29694;&#22312;&#12398;&#26085;&#20184;,&[Time]:&#29694;&#22312;&#12398;&#26178;&#21051;,
#&[Path]:&#12501;&#12449;&#12452;&#12523;&#12398;&#12497;&#12473;,&[File]:&#12501;&#12449;&#12452;&#12523;&#21517;,
#&[Tab]:&#12471;&#12540;&#12488;&#21517;
#&#12504;&#12483;&#12480;&#12540;&#35373;&#23450;:header&#12458;&#12503;&#12471;&#12519;&#12531;;c(&#24038;&#20596;,&#20013;&#22830;&#37096;,&#21491;&#20596;)
#&#12501;&#12483;&#12479;&#12540;&#35373;&#23450;:footer&#12458;&#12503;&#12471;&#12519;&#12531;;c(&#24038;&#20596;,&#20013;&#22830;&#37096;,&#21491;&#20596;)
#&#20598;&#25968;&#12506;&#12540;&#12472;&#12398;&#12504;&#12483;&#12480;&#12540;&#35373;&#23450;:evenHeader&#12458;&#12503;&#12471;&#12519;&#12531;;c(&#24038;&#20596;,&#20013;&#22830;&#37096;,&#21491;&#20596;)
#&#20598;&#25968;&#12506;&#12540;&#12472;&#12398;&#12501;&#12483;&#12479;&#12540;&#35373;&#23450;:evenFooter&#12458;&#12503;&#12471;&#12519;&#12531;;c(&#24038;&#20596;,&#20013;&#22830;&#37096;,&#21491;&#20596;)
#&#20808;&#38957;&#12410;&#12540;&#12472;&#12504;&#12483;&#12480;&#12540;&#35373;&#23450;:firstHeader&#12458;&#12503;&#12471;&#12519;&#12531;;c(&#24038;&#20596;,&#20013;&#22830;&#37096;,&#21491;&#20596;)
#&#20808;&#38957;&#12410;&#12540;&#12472;&#12501;&#12483;&#12479;&#12540;&#35373;&#23450;:firstFooter&#12458;&#12503;&#12471;&#12519;&#12531;;c(&#24038;&#20596;,&#20013;&#22830;&#37096;,&#21491;&#20596;)
#&#34920;&#31034;&#35373;&#23450;:visible&#12458;&#12503;&#12471;&#12519;&#12531;;TRUE/FALSE:&#12471;&#12540;&#12488;&#12364;&#19968;&#12388;&#12398;&#26178;&#12399;FALSE&#19981;&#21487;
#&#21360;&#21047;&#12469;&#12452;&#12474;:paperSize&#12458;&#12503;&#12471;&#12519;&#12531;;8:A3,9:A4,11:A5,12:B4,13:B5,
#67:A3&#27178;,55:A4&#27178;,61:A5&#27178;,&#12381;&#12398;&#20182;&#12469;&#12452;&#12474;&#12399;?pageSetup&#12391;&#30906;&#35469;
#&#21360;&#21047;&#12398;&#32294;&#27178;&#26041;&#21521;:orientation&#12458;&#12503;&#12471;&#12519;&#12531;;"portrait"/"landscape"
addWorksheet(wb = TestWb,
             sheetName = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;",
             gridLines = FALSE,
             tabColour = "blue",
             zoom = 80,
             header = c("&#12363;&#12425;&#12384;&#12395;", "&#12356;&#12356;&#12418;&#12398;", "R&#24773;&#22577;"),
             footer = c("KARADA", "GOOD", "R INFO &[Page],&[Pages]"),
             evenHeader = c("&#20598;&#25968;&#12363;&#12425;&#12384;&#12395;&[Date],&[Time],", "&#20598;&#25968;&#12356;&#12356;&#12418;&#12398;", NA),
             evenFooter = c("EVEN KARADA", "EVEN GOOD", "EVEN R INFO"),
             firstHeader = c("&#20808;&#38957;&#12363;&#12425;&#12384;&#12395;", "&[Path],&[File],&[Tab]",
                             "&#20808;&#38957;R&#24773;&#22577;"),
             firstFooter = c("First KARADA", NA, "First R INFO"),
             visible = TRUE,
             paperSize = 9,
             orientation = "landscape")

#&#12471;&#12540;&#12488;&#12395;&#12487;&#12540;&#12479;&#12434;&#26360;&#12365;&#36796;&#12416;:writeData&#12467;&#12510;&#12531;&#12489;
#Workbook object&#12434;&#25351;&#23450;:wb&#12458;&#12503;&#12471;&#12519;&#12531;
#&#26360;&#12365;&#36796;&#12415;&#23550;&#35937;&#12471;&#12540;&#12488;&#21517;:sheet&#12458;&#12503;&#12471;&#12519;&#12531;;&#21517;&#21069;/&#12452;&#12531;&#12487;&#12483;&#12463;&#12473;&#30058;&#21495;
#&#26360;&#12365;&#36796;&#12415;&#12487;&#12540;&#12479;:x&#12458;&#12503;&#12471;&#12519;&#12531;
#&#35501;&#12415;&#36796;&#12415;&#38283;&#22987;&#34892;&#12434;&#25351;&#23450;:startRow&#12458;&#12503;&#12471;&#12519;&#12531;
#&#35501;&#12415;&#36796;&#12415;&#38283;&#22987;&#21015;&#12434;&#25351;&#23450;:startcol&#12458;&#12503;&#12471;&#12519;&#12531;
#&#12487;&#12540;&#12479;&#31684;&#22258;&#12395;&#21517;&#21069;&#12434;&#20184;&#12369;&#12427;:name&#12458;&#12503;&#12471;&#12519;&#12531;
#&#20182;&#12399;&#12300;&#20307;&#35009;&#12434;&#25972;&#12360;&#12390;&#20445;&#23384;:write.xlsx&#12467;&#12510;&#12531;&#12489;&#12301;&#12434;&#21442;&#29031;
writeData(wb = TestWb,
          sheet = 1, x = iris,
          startRow = 3, startCol = 2,
          array = FALSE,
          colNames = TRUE, rowNames = FALSE,
          name = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;",
          headerStyle = CellStyle,
          borders = "columns",
          borderColour = "blue",
          borderStyle = "double",
          withFilter = TRUE,
          keepNA = TRUE)

#&#12454;&#12451;&#12531;&#12489;&#12454;&#26528;&#12398;&#22266;&#23450;:freezePane&#12467;&#12510;&#12531;&#12489;
#&#12300;&#20307;&#35009;&#12434;&#25972;&#12360;&#12390;&#20445;&#23384;:write.xlsx&#12467;&#12510;&#12531;&#12489;&#12301;&#12434;&#21442;&#29031;
freezePane(wb = TestWb,
           sheet = 1,
           firstActiveRow = 3,
           firstActiveCol = 4)

#&#21015;&#12398;&#24133;&#12434;&#25351;&#23450;:setColWidths&#12467;&#12510;&#12531;&#12489;
#&#24133;&#12434;&#25351;&#23450;:widths&#12458;&#12503;&#12471;&#12519;&#12531;;"auto"/&#25968;&#20516;
setColWidths(wb = TestWb, sheet = 1,
             cols = 2:6, widths = "auto")

#Style Object&#12434;&#25351;&#23450;&#31684;&#22258;&#12395;&#36969;&#24540;:addStyle&#12467;&#12510;&#12531;&#12489;
#&#26032;&#35215;Style&#12434;&#20316;&#25104;
CellStyle2 <- createStyle(fgFill = "lightblue",
                          textDecoration = "underline2")
#Style Object&#12434;&#36969;&#24540;
#cols,rows&#12398;&#31684;&#22258;&#12364;&#21516;&#12376;&#12391;&#12394;&#12356;&#22580;&#21512;:gridExpand&#12458;&#12503;&#12471;&#12519;&#12531;:TRUE&#12395;&#35373;&#23450;
addStyle(wb = TestWb, sheet = 1, 
         style = CellStyle2,
         rows = 8:10, cols = 2:6,
         gridExpand = TRUE)

#Style Object&#12434;&#26360;&#24335;&#12523;&#12540;&#12523;&#12391;&#36969;&#24540;:conditionalFormatting&#12467;&#12510;&#12531;&#12489;
#&#26032;&#35215;Style&#12434;&#20316;&#25104;
#&#12509;&#12452;&#12531;&#12488;::bgFill&#12458;&#12503;&#12471;&#12519;&#12531;&#12434;&#20351;&#29992;&#12377;&#12427;
CellStyle3 <- createStyle(bgFill = "red",
                          textDecoration = "underline2")
CellStyle4 <- createStyle(bgFill = "gold",
                          textDecoration = "underline2")
CellStyle5 <- createStyle(bgFill = "green",
                          textDecoration = "underline2")
CellStyle6 <- createStyle(bgFill = "black",
                          textDecoration = "underline2")

#&#25351;&#23450;&#26465;&#20214;&#20869;&#23481;:type&#12458;&#12503;&#12471;&#12519;&#12531;
#rule&#12364;"<","<=",">",">=","==","!=":"expression"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle3,
                      cols = 2, rows = 4:30,
                      type = "expression",
                      rule = ">=5")

#rule&#12364;2&#12414;&#12383;&#12399;3&#12398;&#25968;&#20516;&#31684;&#22258;&#12391;&#12464;&#12521;&#12487;&#12540;&#12471;&#12519;&#12531;:"colourScale"
#rule&#12364;2&#12414;&#12383;&#12399;3&#12398;&#25968;&#20516;&#31684;&#22258;&#12391;&#12487;&#12540;&#12479;&#12496;&#12540;:"databar"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = c("pink", "yellow"),
                      cols = 3, rows = 4:30,
                      type = "colourScale",
                      rule = c(3, 5))

#rule&#12364;&#37325;&#35079;&#20516;:"duplicates"
#rule&#12364;&#25351;&#23450;&#12398;&#20516;&#12434;&#21547;&#12416;:"contains"
#rule&#12364;&#25351;&#23450;&#12375;&#12383;&#31684;&#22258;&#12398;&#20516;&#12434;&#21547;&#12416;:"between"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle4,
                      cols = 4, rows = 4:30,
                      type = "duplicates")

#rule&#12364;&#12354;&#12427;&#25991;&#23383;&#12391;&#22987;&#12414;&#12427;:"beginsWith"
#rule&#12364;&#12354;&#12427;&#25991;&#23383;&#12391;&#32066;&#12431;&#12427;:"endsWith"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle5,
                      cols = 6, rows = 4:30,
                      type = "endsWith",
                      rule = "a")

#rule&#12364;&#19978;&#20301;&#12398;&#20516;:"topN"
#rule&#12364;&#19979;&#20301;&#12398;&#20516;:"bottomN"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle6,
                      cols = 5, rows = 4:30,
                      type = "topN",
                      rank = 20, percent = TRUE)
######################

#&#21015;&#12398;&#24133;&#12434;&#25351;&#23450;:setColWidths&#12467;&#12510;&#12531;&#12489;
#&#21015;&#24133;:widths&#12458;&#12503;&#12471;&#12519;&#12531;;"auto"/&#25968;&#20516;
setColWidths(wb = TestWb, sheet = 1,
             cols = 2:5, widths = "18.3")

#plot&#12420;ggplot2&#12497;&#12483;&#12465;&#12540;&#12472;&#12398;&#20986;&#21147;&#12434;&#25407;&#20837;:insertPlot&#12467;&#12510;&#12531;&#12489;
#BoxPlot&#12434;&#20316;&#25104;
BoxPlot <- ggplot(cars, aes(x = factor(speed),
                            y = dist, fill = factor(speed))) +
  geom_boxplot()

#BoxPlot&#12434;&#36861;&#21152;:insertPlot&#12467;&#12510;&#12531;&#12489;
#&#26368;&#24460;&#12395;print&#12375;&#12383;&#22259;&#12364;&#25407;&#20837;&#12373;&#12428;&#12414;&#12377;
#&#12469;&#12452;&#12474;&#12434;&#25351;&#23450;:width,height&#12458;&#12503;&#12471;&#12519;&#12531;;&#21336;&#20301;&#12399;&#12452;&#12531;&#12481;
#&#21336;&#20301;&#12434;&#22793;&#26356;&#12377;&#12427;&#22580;&#21512;&#12399;units&#12458;&#12503;&#12471;&#12519;&#12531;&#12434;&#20351;&#29992;
print(BoxPlot)
insertPlot(wb = TestWb, sheet = 1,
           width = 15, height = 15,
           units = "cm", xy= c("H", 7),
           fileType = "png")

#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;xlsx&#12501;&#12449;&#12452;&#12523;&#12434;&#20445;&#23384;:saveWorkbook&#12467;&#12510;&#12531;&#12489;
saveWorkbook(wb = TestWb,
             file = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;.xlsx",
             overwrite = TRUE)

xlsxファイルやシートの保護、複製、削除など

#xlsxファイルにパスワードを設定する:protectWorkbookコマンド
#パスワードを指定:passwordオプション
protectWorkbook(wb = TestWb,
                protect = TRUE,
                password = "KARADAGOOD",
                lockStructure = FALSE,
                lockWindows = FALSE)

#ワークシートにパスワードやセルの保護などを設定する:protectWorksheetコマンド
#パスワードを指定:passwordオプション
#セルの保護:lockSelectingLockedCellsオプション
#その他保護設定は?protectWorksheet
protectWorksheet(wb = TestWb,
                 sheet = 1,
                 protect = TRUE,
                 password = "KARADAGOOD",
                 lockSelectingLockedCells = TRUE)

#シートの複製:cloneWorksheetコマンド
#コピー後のシート名:sheetNameオプション
cloneWorksheet(wb = TestWb,
               sheetName = "からだにいいもの_2",
               clonedSheet = 1)

#シート名の変更:renameWorksheetコマンド
#対象シート:sheetオプション
#変更後の名前:newNameオプション
renameWorksheet(wb = TestWb,
                sheet = "からだにいいもの_2",
                newName = "からだにいいもの_3")

#シートの削除:removeWorksheetコマンド
#対象シート:sheetNameオプション
removeWorksheet(wb = TestWb,
                sheetName = "からだにいいもの_2")

その他

・ハイパーリンクの設定など

#&#12495;&#12452;&#12497;&#12540;&#12522;&#12531;&#12463;&#12398;&#20316;&#25104;:getNamedRegions&#12467;&#12510;&#12531;&#12489;
#URL&#12420;Path&#12434;&#21547;&#12416;&#12487;&#12540;&#12479;&#12364;&#23550;&#35937;
#&#26360;&#12365;&#36796;&#12415;&#12487;&#12540;&#12479;&#12434;"hyperlink class"&#12395;&#35373;&#23450;&#12375;&#12390;&#12363;&#12425;&#26360;&#12365;&#36796;&#12416;
class(&#23550;&#35937;&#12487;&#12540;&#12479;) <- "hyperlink"
#&#12418;&#12375;&#12367;&#12399;&#25991;&#23383;&#21015;&#12434;HYPERLINK&#38306;&#25968;&#12392;&#12375;&#12390;&#28310;&#20633;&#12377;&#12427;
#windows&#12398;&#22580;&#21512;&#12399;&#12300;&#65509;&#12301;&#12399;&#12300;\\&#12301;&#12392;&#20837;&#21147;&#12377;&#12427;
paste0('HYPERLINK(&#12522;&#12531;&#12463;&#20808;, &#34920;&#31034;&#21517;)')

#&#20316;&#25104;&#32773;&#12398;&#21462;&#24471;:getCreators&#12467;&#12510;&#12531;&#12489;
getCreators(wb = TestWb)

#&#12471;&#12540;&#12488;&#21517;&#12398;&#21462;&#24471;:getSheetNames&#12467;&#12510;&#12531;&#12489;
#xlsx/xlsm&#12501;&#12449;&#12452;&#12523;&#12434;&#25351;&#23450;:file&#12458;&#12503;&#12471;&#12519;&#12531;
getSheetNames(file)

・罫線のスタイルと位置:出力例を参照

#&#12456;&#12463;&#12475;&#12523;&#12501;&#12449;&#12452;&#12523;&#12434;&#26032;&#35215;&#20316;&#25104;
TestWb <- createWorkbook()
#&#12471;&#12540;&#12488;&#12434;&#36861;&#21152;
addWorksheet(wb = TestWb,
             sheetName = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;",
             gridLines = FALSE,
             tabColour = "blue",
             zoom = 80)

###&#32619;&#32218;&#12398;&#12473;&#12479;&#12452;&#12523;#####
StyleName <- c("thin", "medium", "dashed", "dotted", "thick", "double",
               "hair", "mediumDashed", "dashDot", "mediumDashDot",
               "mediumDashDotDot", "slantDashDot")

#&#25991;&#23383;&#12434;&#26360;&#12365;&#36796;&#12415;
writeData(wb = TestWb, sheet = 1, x = "&#32619;&#32218;&#12398;&#12473;&#12479;&#12452;&#12523;",
          startRow = 2, startCol = 3)

#&#32619;&#32218;&#12434;&#26360;&#12365;&#36796;&#12416;
for(i in seq(StyleName)){
  ifelse(i <= 6, Sc <- 3, Sc <- 5)
  ifelse(i <= 6, Sr <- 2 + i, Sr <- i - 4)
  #&#26360;&#12365;&#36796;&#12415;
  writeData(wb = TestWb, sheet = 1, x = StyleName[i],
            startRow = Sr, startCol = Sc)
  #&#12475;&#12523;&#12398;&#12473;&#12479;&#12452;&#12523;
  CellStyle <- createStyle(fontSize = 12,
                           fontColour = "black",
                           border = "bottom",
                           borderColour = "red",
                           borderStyle = StyleName[i])
  #&#12473;&#12479;&#12452;&#12523;&#12434;&#36969;&#24540;
  addStyle(wb = TestWb, sheet = 1, 
           style = CellStyle,
           rows = Sr, cols = Sc,
           gridExpand = TRUE)
}
########

###&#32619;&#32218;&#12398;&#20301;&#32622;#####
AllBorder <- c("top","bottom","left","right","TopBottom")
SrNo <- c(2, 3, 4, 5, 6)

#&#25991;&#23383;&#12434;&#26360;&#12365;&#36796;&#12415;
writeData(wb = TestWb, sheet = 1, x = "&#32619;&#32218;&#12398;&#20301;&#32622;",
          startRow = 2, startCol = 7)

#&#32619;&#32218;&#12434;&#26360;&#12365;&#36796;&#12416;
for(i in seq(AllBorder)){
  Sr <- SrNo[i] + i
  #&#26360;&#12365;&#36796;&#12415;
  writeData(wb = TestWb, sheet = 1, x = AllBorder[i],
            startRow = Sr, startCol = 7)
  #&#12475;&#12523;&#12398;&#12473;&#12479;&#12452;&#12523;
  CellStyle <- createStyle(fontSize = 12,
                           fontColour = "black",
                           border = AllBorder[i],
                           borderColour = "blue",
                           borderStyle = "medium")
  #&#12473;&#12479;&#12452;&#12523;&#12434;&#36969;&#24540;
  addStyle(wb = TestWb, sheet = 1, 
           style = CellStyle,
           rows = Sr, cols = 7,
           gridExpand = TRUE)

}
########

#&#21015;&#12398;&#24133;&#12434;&#25351;&#23450;
setColWidths(wb = TestWb, sheet = 1,
             cols = c(3, 4, 5, 6, 7),
             widths = c(23, 2.86, 23, 2.86, 23))

#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;xlsx&#12501;&#12449;&#12452;&#12523;&#12434;&#20445;&#23384;:saveWorkbook&#12467;&#12510;&#12531;&#12489;
saveWorkbook(wb = TestWb,
             file = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;.xlsx",
             overwrite = TRUE)

・条件付き書式:出力例を参照

#&#12456;&#12463;&#12475;&#12523;&#12501;&#12449;&#12452;&#12523;&#12434;&#26032;&#35215;&#20316;&#25104;
TestWb <- createWorkbook()
#&#12471;&#12540;&#12488;&#12434;&#36861;&#21152;
addWorksheet(wb = TestWb,
             sheetName = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;",
             gridLines = FALSE,
             tabColour = "blue",
             zoom = 80)

###&#26465;&#20214;&#20184;&#12365;&#26360;&#24335;#####
#&#26465;&#20214;&#20184;&#12365;&#26360;&#24335;&#21517;
RuleName <- c("expression", "colourScale", "databar", "duplicates",
              "contains", "between", "beginsWith", "endsWith",
              "topN", "bottomN")
#&#12475;&#12523;&#12398;&#12473;&#12479;&#12452;&#12523;
CellStyle <- createStyle(bgFill = "red")
CellStyle_BW <- createStyle(bgFill = c("pink", "blue"))

#&#12487;&#12540;&#12479;&#20301;&#32622;
Sr <- rep(seq(3, 11, by = 2), 2)
LSc <- rep(c(2, 12), each = 5)
Sc <- rep(c(3, 13), each = 5)

#&#12487;&#12540;&#12479;&#26360;&#12365;&#36796;&#12415;
for(i in seq(RuleName)){
  #&#26465;&#20214;&#20184;&#12365;&#26360;&#24335;&#21517;&#12434;&#26360;&#12365;&#36796;&#12415;
  writeData(wb = TestWb, sheet = 1, x = RuleName[i],
            startRow = LSc[i], startCol = Sr[i])
  #&#12487;&#12540;&#12479;&#26360;&#12365;&#36796;&#12415;
  if(RuleName[i] %in% c("contains", "beginsWith", "endsWith")){
    writeData(wb = TestWb, sheet = 1,
              x = c("karada", "&#12363;&#12425;&#12384;&#12395;", "Good", "&#12356;&#12356;&#12418;&#12398;"),
              startRow = Sc[i], startCol = Sr[i])
  }else{
    writeData(wb = TestWb, sheet = 1,
              x = c(-1, -1, 2, 3, 3, 4, 5, 6),
              startRow = Sc[i], startCol = Sr[i])}
}

###&#26360;&#24335;&#12523;&#12540;&#12523;&#12434;&#36969;&#24540;#####
#"expression"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 3, rows = 3:10,
                      type = "expression",
                      rule = ">2")
#"colourScale"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = c("pink", "blue", "yellow"),
                      cols = 5, rows = 3:10,
                      type = "colourScale",
                      rule = c(1, 3, 6))
#"databar"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = c("pink", "blue"),
                      cols = 7, rows = 3:10,
                      type = "databar")
#"duplicates"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 9, rows = 3:10,
                      type = "duplicates")
#"contains"/"notcontainsText"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 11, rows = 3:10,
                      type = "contains",
                      rule = "oo")
#"between"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 3, rows = 13:20,
                      type = "between",
                      rule = c(2, 4))
#"beginsWith"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 5, rows = 13:20,
                      type = "beginsWith",
                      rule = "&#12363;")
#"endsWith"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 7, rows = 13:20,
                      type = "endsWith",
                      rule = "d")
#"topN"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 9, rows = 13:20,
                      type = "topN",
                      rank = 3)
#"bottomN"
conditionalFormatting(wb = TestWb, sheet = 1,
                      style = CellStyle,
                      cols = 11, rows = 13:20,
                      type = "bottomN",
                      rank = 3)

#&#21015;&#12398;&#24133;&#12434;&#25351;&#23450;
setColWidths(wb = TestWb, sheet = 1,
             cols = c(3:11),
             widths = rep(c(23, 2.86), length = 9))

#&#20316;&#26989;&#12501;&#12457;&#12523;&#12480;&#12395;xlsx&#12501;&#12449;&#12452;&#12523;&#12434;&#20445;&#23384;:saveWorkbook&#12467;&#12510;&#12531;&#12489;
saveWorkbook(wb = TestWb,
             file = "&#12363;&#12425;&#12384;&#12395;&#12356;&#12356;&#12418;&#12398;.xlsx",
             overwrite = TRUE)

出力例

・罫線のスタイルと位置

・条件付き書式


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

タイトルとURLをコピーしました