Search code examples
rexcelr-markdownxlsx

Trying to export data to excel, works but the code is clunky-repeated per sheet


I am exporting some code to excel, and trying to do it to 1 workbook over 3 sheets. However to do this I have repeated each command thrice, this can't be a good way to do it? Perhaps someone can tell me where I'm going wrong?

library(r2excel)
#> Loading required package: xlsx

 sample_prime1 <- 1:4
 sample_prime2 <- 10:14
 sample_prime3 <- 22:26
# Create an Excel workbook. 
filename <- "validitycheck.xlsx" #The name of the file that will be saved
wb <- createWorkbook(type="xlsx") #Creating an excel workbook

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

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

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


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

Solution

  • The idea of @DavidMas is great (kudos and many thanks for teaching this to us). You can improve the function and generalize it to n data working with a list to store the data and a vector for the names. Then you can use a loop to print the data. Here the code:

    library(r2excel)
    #Data
    sample_prime1 <- 1:4
    sample_prime2 <- 10:14
    sample_prime3 <- 22:26
    #Store in a list
    List <- list(sample_prime1,sample_prime2,sample_prime3)
    #Function
    myfun <- function(wb,name,df) {
      # Create object
      sheet <- 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="darkblue", fontSize=14,
                    rowFill=c("white", "lightblue"),
                    startCol=2, 
                    row.names=FALSE, 
                    col.names = FALSE)
    }
    #Create workbook
    filename <- "validitycheck.xlsx" 
    wb <- createWorkbook(type="xlsx")
    #Loop
    #Vector for names
    vnames <- paste0('prime',1:length(List))
    #Code
    for(i in 1:length(List))
    {
      myfun(wb,vnames[i],List[i])
    }