Skip to content

Instantly share code, notes, and snippets.

@allenaven
Created June 10, 2016 01:37
Show Gist options
  • Save allenaven/51496be122e14eaa54ba71b0bbaf7f68 to your computer and use it in GitHub Desktop.
Save allenaven/51496be122e14eaa54ba71b0bbaf7f68 to your computer and use it in GitHub Desktop.
Read and write Excel from R using the openxlsx package. No infuriating Java dependencies. Easy to work with. Best Excel translator I know of.
# Example use of the openxlsx package
require(openxlsx) # For writing output in excel for collaborators
df1 <- data.frame(aa = letters[3:12], bb = 21:30, stringsAsFactors = FALSE)
df2 <- data.frame(cc = rnorm(20, 5, 1), dd = runif(20, 1, 5))
## Write the data to a new excel file
wb <- openxlsx::createWorkbook()
addWorksheet(wb, 'Wksheet name')
addWorksheet(wb, 'Another worksheet name')
class(df1$aa) <- 'text' # Ususally not necessary, the function does a good job of auto-recognizing
writeDataTable(wb = wb, sheet = 'Wksheet name', x = df1)
writeDataTable(wb = wb, sheet = 'Another worksheet name', x = df2)
saveWorkbook(wb = wb, file = './blahblah.xlsx', overwrite = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment