Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Error in addWorksheet(wb, "sheet1") : First argument must be a Workbook"

I am using ‘openxlsx’ package in R. ٰI want to add some data in xlsx file. I have used following code to create the workbook and add worksheet in it.

 wb=createWorkbook()
 addWorksheet(wb,"sheet 1")
 writeData(wb,sheet = 1,"From",startCol = 1,startRow = 1)
 writeData(wb,sheet = 1,"To",startCol = 2,startRow = 1)
 writeData(wb,sheet = 1,"From",startCol = 1,startRow = 2)
 writeData(wb,sheet = 1,"From",startCol = 1,startRow = 2)
 saveWorkbook(wb,"file.xlsx",overwrite = TRUE)

This code was working well for a long time, But recently, I am facing this error

Error in addWorksheet(wb, "sheet 1") : First argument must be a Workbook.

How this error will be resolved?

like image 370
Alvi Avatar asked Mar 05 '23 02:03

Alvi


2 Answers

I had the same issue with this. I did the followings and it fixed the issue. Maybe it can solve yours.

  • Close R or RStudio.
  • Make sure that your current working directory does not have any other file or folder. In other words, the path in which you would like to save the xlsx is empty prior to running createWorkbook(). If you have already saved any file in there, just copy and paste it somewhere else.
  • Run your code again from the beginning.
like image 196
R User Scientist Avatar answered Apr 07 '23 04:04

R User Scientist


I had the same problem and I just unloaded the package, reinstalled it and reloaded it, and it worked (without having to close R studio) :

detach("package:openxlsx", unload=TRUE)

install.packages("openxlsx")

library(openxlsx)
like image 36
Emmanuelle Chevallier Avatar answered Apr 07 '23 02:04

Emmanuelle Chevallier