"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?

728x90

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

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.

4 months ago