Skip to content

Instantly share code, notes, and snippets.

@anfederico
Created June 20, 2019 14:08
Show Gist options
  • Save anfederico/211bdd2dbf38b9d7df9f32cff0e1c30f to your computer and use it in GitHub Desktop.
Save anfederico/211bdd2dbf38b9d7df9f32cff0e1c30f to your computer and use it in GitHub Desktop.
Write vectors of unequal sizes to excel
library(openxlsx)
library(magrittr)
library(plyr)
wb.init <- function() {
wb <- openxlsx::createWorkbook()
return(wb)
}
wb.add.sheet <- function(wb, sheet="1", signatures) {
df <- lapply(names(signatures), function(x) {
df <- data.frame(x=signatures[[x]])
colnames(df) <- c(x)
return(df)
}) %>%
do.call(plyr::rbind.fill, .) %>%
apply(2, sort, na.last=TRUE) %>%
data.frame()
openxlsx::addWorksheet(wb, sheetName=sheet)
openxlsx::writeData(wb, sheet=sheet, x=df, colNames=TRUE, rowNames=FALSE)
}
# Signatures of unequal length
signature.alpha <- list("A"=c("G1", "G2", "G3", "G4", "G5"),
"B"=c("G1", "G2", "G3"),
"C"=c("G1"))
# Write to excel
wb <- wb.init()
wb.add.sheet(wb, sheet="Alpha", signature.alpha)
openxlsx::saveWorkbook(wb, file="example.xlsx", overwrite=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment