1.2 writexl

截止到2021年5月17日,writexl包功能比较简单,仅有输出Excel功能。快速、不依赖java和Excle是它绝对的优势,并且输出文件相比openxlsx包较小。

项目地址

1.2.1 用法

安装

install.packages("writexl")

参数

write_xlsx(
  x,
  path = tempfile(fileext = ".xlsx"),
  col_names = TRUE,
  format_headers = TRUE,
  use_zip64 = FALSE
)

输出Excel

library(writexl)
writexl::write_xlsx(iris,path = 'iris.xlsx')
write_xlsx(list(mysheet1 = iris,mysheet2 = iris),path = 'iris.xlsx')

效率比较

library(microbenchmark)
library(nycflights13)
microbenchmark(
  writexl = writexl::write_xlsx(flights, tempfile()),
  openxlsx = openxlsx::write.xlsx(flights, tempfile()),
  times = 2
)

文件大小比较

writexl::write_xlsx(flights, tmp1 <- tempfile())
file.info(tmp1)$size
openxlsx::write.xlsx(flights, tmp2 <- tempfile())
file.info(tmp2)$size

其它功能

df <- data.frame(
  name = c("UCLA", "Berkeley", "Jeroen"),
  founded = c(1919, 1868, 2030),
  website = xl_hyperlink(c("http://www.ucla.edu", "http://www.berkeley.edu", NA), "homepage")
)
df$age <- xl_formula('=(YEAR(TODAY()) - INDIRECT("B" & ROW()))')
write_xlsx(df, 'universities.xlsx')

# cleanup
unlink('universities.xlsx')