UtrechtUniversity / workshop-introduction-to-R-and-data

Material for the workshop 'Introduction to R & data'
https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/
Other
27 stars 13 forks source link

You can write to excel with openxlsx #1

Closed japhir closed 5 years ago

japhir commented 5 years ago

https://github.com/UtrechtUniversity/workshop-introduction-to-R-and-data/blob/512964245ca84cb9f6e43f37bbf828a189b2b6ec/modernR_solutions.Rmd#L115

Writing to excel is not possible within the tidyverse, but maybe encourage curious students to explore. It seems a little involved though, something like:

library(openxlsx)
dat <- tibble(x = 1:10, y = 11:20)
wb <- createWorkbook()
hs <- createStyle(textDecoration = "bold")
addWorksheet(wb, "data")
writeData(wb, "data", dat, headerStyle = hs)
saveWorkbook(wb, "path/to/file.xlsx")
J535D165 commented 5 years ago

Hello Ilja,

Thanks for your feedback. Indeed, there is no package in the tidyverse package to write Excel files, but there is a package named 'writexl' which implements Excel file writing in the tidyverse philosophy. We prefer this package.

One sheet

library(writexl)
write_xlsx(iris, "iris.xlsx")

Multiple sheets.

library(writexl)
write_xlsx(list(iris1 = iris, iris2 = iris), "iris.xlsx")

I will update the solutions manual.

Best, Jonathan