Writing an excel doc with a subset of my data in R. Currently the code is excessively long I'm fairly new to R. I have used the 'r2excel' package to create an excel workbook from a subset of my R data. This should have 3 sheets for the three different subsets. What I have written works fine, but its a lot of code and is very clunky, just re written three times.
If there is a better way of framing my question/best practice tips, I'd love to learn.


# Create an Excel workbook. 
filename &lt;- &quot;validitycheck.xlsx&quot; #The name of the file that will be saved
wb &lt;- createWorkbook(type=&quot;xlsx&quot;) #Creating an excel workbook

# Create a sheet in that workbook to contain the data table
sheet1 &lt;- createSheet(wb, sheetName = &quot;prime1&quot;)
sheet2 &lt;- createSheet(wb, sheetName = &quot;prime2&quot;)
sheet3 &lt;- createSheet(wb, sheetName = &quot;prime3&quot;)
 

# Add header for sheet 1
xlsx.addHeader(wb, sheet1, value=&quot;Prime 1&quot;,level=1, 
               color=&quot;black&quot;, underline=1)
xlsx.addLineBreak(sheet1, 1)

# Add header for sheet 2
xlsx.addHeader(wb, sheet2, value=&quot;Prime 2&quot;,level=1, 
               color=&quot;black&quot;, underline=1)
xlsx.addLineBreak(sheet2, 1)

# Add header for sheet 3
xlsx.addHeader(wb, sheet3, value=&quot;Prime 3&quot;,level=1, 
               color=&quot;black&quot;, underline=1)
xlsx.addLineBreak(sheet3, 1)

# Add a paragraph : Author
author=paste(&quot;Author : Gabriella. \n&quot;,
             &quot;20% randomly chosen validity check.&quot;,
             &quot;\n Source: filename&quot;, sep=&quot;&quot;)

#Add author for sheet 1
xlsx.addParagraph(wb, sheet1,value=author, isItalic=TRUE, colSpan=5, 
                  rowSpan=4, fontColor=&quot;darkgray&quot;, fontSize=14)

#Add author para for sheet 2
xlsx.addParagraph(wb, sheet2,value=author, isItalic=TRUE, colSpan=5, 
                  rowSpan=4, fontColor=&quot;darkgray&quot;, fontSize=14)

#Add author para for sheet 3
xlsx.addParagraph(wb, sheet3,value=author, isItalic=TRUE, colSpan=5, 
                  rowSpan=4, fontColor=&quot;darkgray&quot;, fontSize=14)

xlsx.addLineBreak(sheet1, 3)
xlsx.addLineBreak(sheet2, 3)
xlsx.addLineBreak(sheet3, 3)


# Add table : add a data frame for sheet 1
xlsx.addHeader(wb, sheet1, value=&quot;Validty for prime 1&quot;)
xlsx.addLineBreak(sheet1, 1)
xlsx.addTable(wb, sheet1, sample_prime1,
               fontColor=&quot;darkblue&quot;, fontSize=14,
              rowFill=c(&quot;white&quot;, &quot;lightblue&quot;),
              startCol=2, 
              row.names=FALSE, 
              col.names = TRUE)


# Add table : add a data frame for sheet 2
xlsx.addHeader(wb, sheet2, value=&quot;Validty for prime 2&quot;)
xlsx.addLineBreak(sheet2, 1)
xlsx.addTable(wb, sheet2, sample_prime2,
               fontColor=&quot;darkblue&quot;, fontSize=14,
              rowFill=c(&quot;white&quot;, &quot;lightblue&quot;),
              startCol=2, 
              row.names=FALSE, 
              col.names = TRUE)


# Add table : add a data frame for sheet 3
xlsx.addHeader(wb, sheet3, value=&quot;Validty for prime 3&quot;)
xlsx.addLineBreak(sheet3, 1)
xlsx.addTable(wb, sheet3, sample_prime3,
               fontColor=&quot;darkblue&quot;, fontSize=14,
              rowFill=c(&quot;white&quot;, &quot;lightblue&quot;),
              startCol=2, 
              row.names=FALSE, 
              col.names = TRUE)



# save the workbook to an Excel file
saveWorkbook(wb, filename, password = &quot;secret&quot;)
xlsx.openFile(filename)# View the file
```
@Duck from Stack overflow provided the answer. Pasting it here. Its is essentially writing a function:
library(r2excel)
#Data
sample_prime1 &lt;- 1:4
sample_prime2 &lt;- 10:14
sample_prime3 &lt;- 22:26
#Store in a list
List &lt;- list(sample_prime1,sample_prime2,sample_prime3)
#Function
myfun &lt;- function(wb,name,df) {
  # Create object
  sheet &lt;- createSheet(wb, sheetName = name)
  # Add headers
  xlsx.addHeader(wb, sheet, value=paste0('Validty for prime ',name))
  xlsx.addLineBreak(sheet, 1)
  xlsx.addTable(wb, sheet, data.frame(df),
                fontColor=&quot;darkblue&quot;, fontSize=14,
                rowFill=c(&quot;white&quot;, &quot;lightblue&quot;),
                startCol=2, 
                row.names=FALSE, 
                col.names = FALSE)
}
#Create workbook
filename &lt;- &quot;validitycheck.xlsx&quot; 
wb &lt;- createWorkbook(type=&quot;xlsx&quot;)
#Loop
#Vector for names
vnames &lt;- paste0('prime',1:length(List))
#Code
for(i in 1:length(List))
{
  myfun(wb,vnames[i],List[i])
}