简体   繁体   中英

R Error in as.vector(x, “character”) when using openxlsx to write excel

I want to export the several dataframes into different sheets in one excel and my code is as follows:

aa<-data.frame(1:5,2:6)
ab<-data.frame(2:6,3:7)

wb <- createWorkbook()

sheet1 <- createSheet(wb, "Sheet1")
addDataFrame(aa,sheet1)

sheet2 <- createSheet(wb, "Sheet2")
addDataFrame(ab,sheet2)

saveWorkbook(wb,file="D:/aa.xlsx")

And then the Rstudio reports that:

Error in as.vector(x, "character") : cannot coerce type 'environment' to vector of type 'character'

Any thoughts would be helpful. Thanks.

aa<-data.frame(1:5,2:6) 
ab<-data.frame(2:6,3:7)

wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, 'Sheet1')
openxlsx::writeData(wb, 'Sheet1', aa)

openxlsx::addWorksheet(wb, 'Sheet2')
openxlsx::writeData(wb, 'Sheet2', ab)

openxlsx::saveWorkbook(wb,file="D:/aa.xlsx") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM