{"id":202,"date":"2013-05-12T07:38:13","date_gmt":"2013-05-12T12:38:13","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=202"},"modified":"2013-05-12T07:38:13","modified_gmt":"2013-05-12T12:38:13","slug":"write-data-frame-to-excel-file-using-r-package-xlsx","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2013\/05\/12\/write-data-frame-to-excel-file-using-r-package-xlsx\/","title":{"rendered":"Write data (frame) to Excel file using R package xlsx"},"content":{"rendered":"<p>Writing to Excel files comes up rather often, especially if you\u2019re collaborating with non-OSS users. There are several options, but I like the\u00a0<a title=\"xmlx package homepage\" href=\"http:\/\/cran.r-project.org\/web\/packages\/xlsx\/index.html\" target=\"_blank\" rel=\"noopener\">xlsx package<\/a>\u00a0way of doing things. Authors use Java to write to Excel files, which are basically compressed XML files.<\/p>\n<p>Alright, let\u2019s get cracking.<\/p>\n<p>First, let\u2019s create some data.<\/p>\n<div>\n<div id=\"highlighter_741172\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>\n<div><code>sample.dataframe<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>If you don\u2019t have the file created yet, you can just write the data into a new file.<\/p>\n<div>\n<div id=\"highlighter_576635\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<\/td>\n<td>\n<div>\n<div><code>library(xlsx) <\/code><code>#load the package<\/code><\/div>\n<div><code>write.xlsx(x <\/code><code>=<\/code> <code>sample.dataframe, <\/code><code>file<\/code> <code>=<\/code> <code>\"test.excelfile.xlsx\"<\/code><code>,<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>sheetName <\/code><code>=<\/code> <code>\"TestSheet\"<\/code><code>, row.names <\/code><code>=<\/code> <code>FALSE)<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><a href=\"http:\/\/danganothererror.files.wordpress.com\/2012\/02\/sample-file.png\"><img decoding=\"async\" title=\"sample.file\" alt=\"\" src=\"http:\/\/danganothererror.files.wordpress.com\/2012\/02\/sample-file.png?w=595\" \/><\/a><\/p>\n<p>If you already have a file created, you can add data to a new sheet, or just add it to the existing one. Here\u2019s how you would add a data.frame to columns D and E (result not shown).<\/p>\n<div>\n<div id=\"highlighter_902433\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<\/td>\n<td>\n<div>\n<div><code>workbook.sheets workbook.test addDataFrame(x <\/code><code>=<\/code> <code>sample.dataframe, sheet <\/code><code>=<\/code> <code>workbook.test,<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0<\/code><code>row.names <\/code><code>=<\/code> <code>FALSE, startColumn <\/code><code>=<\/code> <code>4<\/code><code>) <\/code><code># write data to sheet starting on line 1, column 4<\/code><\/div>\n<div><code>saveWorkbook(workbook.sheets, <\/code><code>\"test.excelfile.xlsx\"<\/code><code>) <\/code><code># and of course you need to save it.<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>You can now open the file in your WYSIWYG editor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing to Excel files comes up rather often, especially if you\u2019re collaborating with non-OSS users. There are several options, but I like the\u00a0xlsx package\u00a0way of&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}