R读写xlsx文件

读取xlsx文件可以用xlsx 包,但是因为xlsx依赖java很容易出现各种问题,有时会爆内存而且速度较慢。因此可用其他包代替。 推荐readxl的read_excel,兼容.xls 和 .xlsx 基本用法:https://zhuanlan.zhihu.com/p/35608173

read_excel(path, sheet = NULL, range = NULL, col_names = TRUE, col_types = NULL, na = "")

install.packages("readxl")  library(readxl)  excel_sheets('file.xlsx')#展示里面的sheet
data <- read_excel('file.xlsx', sheet=1)#读入  data<-read_excel("data.xls",sheet = 1)

writexl包可以用来将数据框保存为Excel格式。 用法: write_xlsx(x,path = tempfile(fileext = ".xlsx"),col_names = TRUE,format_headers = TRUE)

library(writexl)  #需要将不同的sheet的数据写成list  sheets <- list("sheet1" = df1, "sheet2" = df2) #载入数据为data frame  write_xlsx(sheets, "file.xlsx")

此外,除了readxl和writexl扩展包, XLConnect, xlsx, tidyxl也可以进行与Excel文件或者Excel软件的交互。

欢迎关注微信公众号:生信编程日常

参考: https://zhuanlan.zhihu.com/p/32811035