{"id":2643,"date":"2022-09-10T15:00:33","date_gmt":"2022-09-10T15:00:33","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=2643"},"modified":"2022-09-10T15:03:55","modified_gmt":"2022-09-10T15:03:55","slug":"r-tips-tricks-write-data-to-separate-excel-sheet-tabs","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/r-tips-tricks-write-data-to-separate-excel-sheet-tabs\/","title":{"rendered":"R Tips &#038; Tricks: Write Data to Separate Excel Sheet Tabs"},"content":{"rendered":"<p>When working with colleagues and managers, sometimes they prefer to have data or analysis written out to an excel workbook so that they can look at the information and sort or filter it, however they see fit. Rather than saving each output to a single csv file and then spending time copying and pasting them all into a single excel sheet (different data outputs on different tabs), let&#8217;s see how we can write everything out to a multi-tab excel sheet from R in one shot using the <strong>{openxlsx} <\/strong>package.<\/p>\n<p><strong>Data<\/strong><\/p>\n<p>We will simulate some sports data. In this example, we have two data sources:<\/p>\n<ol>\n<li>Data about teams<\/li>\n<li>Data about players<\/li>\n<\/ol>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n### Libraries -----------------------\r\nlibrary(tidyverse)\r\nlibrary(openxlsx)\r\n\r\n\r\n### Create Data -----------------------\r\n\r\nteams &lt;- data.frame(\r\n  team = c(&quot;Bats&quot;, &quot;Sharks&quot;, &quot;Tigers&quot;, &quot;Bisons&quot;),\r\n  stat1 = round(rnorm(n = 4, mean = 100, sd = 20), 1),\r\n  stat2 = round(rnorm(n = 4, mean = 250, sd = 70), 1),\r\n  stat3 = round(rnorm(n = 4, mean = 0, sd = 3), 2)\r\n)\r\n\r\nplayers &lt;- data.frame(\r\n  player = c(&quot;Tom&quot;, &quot;Joe&quot;, &quot;Karl&quot;, &quot;Bob&quot;, &quot;Ben&quot;, &quot;Albert&quot;, &quot;Simon&quot;, &quot;Harold&quot;, &quot;Ken&quot;, &quot;Cal&quot;),\r\n  cool_stat1 = round(rnorm(n = 10, mean = 300, sd = 10), 1),\r\n  cool_stat1 = round(rnorm(n = 10, mean = 20, sd = 5), 1),\r\n  cool_stat1 = round(rnorm(n = 10, mean = 500, sd = 50), 1),\r\n  cool_stat1 = round(rnorm(n = 10, mean = 10, sd = 15), 1)\r\n)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.40.36-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2644\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.40.36-AM.png\" alt=\"\" width=\"699\" height=\"535\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.40.36-AM.png 699w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.40.36-AM-300x230.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.40.36-AM-624x478.png 624w\" sizes=\"auto, (max-width: 699px) 100vw, 699px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Store the data frames in a named list<\/strong><\/p>\n<p>Next, we need to store each of the data frames we want in our excel output into a named list. Naming of the elements in the list is important as these will be the names given to the excel tabs.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Store each data frame as a named list -----------------------\r\nlist_of_dataframes &lt;- list(teams = teams,\r\n                   players = players)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.44.50-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2645\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.44.50-AM.png\" alt=\"\" width=\"417\" height=\"362\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.44.50-AM.png 464w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.44.50-AM-300x261.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Write the Data to an Excel Workbook<\/strong><\/p>\n<p>Once we have the data sets we are interested in structured in a named list we are ready to use the <strong>{openxlsx}<\/strong> package to write the data to a multi-tab excel workbook.<\/p>\n<p>First, we create an empty excel workbook object to store our results.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Create an empty excel workbook object -----------------------\r\nblank_excel &lt;- createWorkbook()\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next, we loop over our list of data sets and store each data set on its own named tab.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## loop over the list storing each data set on its own tab in the blank excel workbook -----------\r\nMap(function(df, tab_name){     \r\n  \r\n  addWorksheet(blank_excel, tab_name)\r\n  writeData(blank_excel, tab_name, df)\r\n  }, \r\n  \r\n  list_of_dataframes, names(list_of_dataframes)\r\n)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you&#8217;ve done it correctly, in your R console you should see an output indicating each of the data sets that were placed into the workbook.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.56.33-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2646\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.56.33-AM.png\" alt=\"\" width=\"68\" height=\"93\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Finally, we want to save the results into an excel workbook that we can email out to our co-workers. We use the <strong>saveWorkbook()<\/strong> function to save the results to our working directory.<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n## Save the now populated excel workbook --------------------------------------------\r\nsaveWorkbook(blank_excel, file = &quot;league_data.xlsx&quot;, overwrite = TRUE)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>When we open the excel\u00a0 workbook we see that we have both tabs, properly named for the data they contain.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.23-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2647\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.23-AM.png\" alt=\"\" width=\"646\" height=\"387\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.23-AM.png 646w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.23-AM-300x180.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.23-AM-624x374.png 624w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/a> <a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.34-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2648\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.34-AM.png\" alt=\"\" width=\"625\" height=\"413\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.34-AM.png 653w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.34-AM-300x198.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/09\/Screen-Shot-2022-09-10-at-7.59.34-AM-624x412.png 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with colleagues and managers, sometimes they prefer to have data or analysis written out to an excel workbook so that they can look at the information and sort or filter it, however they see fit. Rather than saving each output to a single csv file and then spending time copying and pasting them [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-2643","post","type-post","status-publish","format-standard","hentry","category-r-tips-tricks"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/comments?post=2643"}],"version-history":[{"count":3,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2643\/revisions"}],"predecessor-version":[{"id":2651,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2643\/revisions\/2651"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=2643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=2643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=2643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}