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

6

6

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?

Rabblement answered 9/11, 2018 at 6:49 Comment(4)
The code you have shared doesn't throw any error in my case. The error message you have shared indicates that the workbook wb was not created. Maybe you skipped executing line 1 of the code.Fowling
I have not skipped that line. Actually, this was working well but recently failed. but don't know why?Rabblement
What does str(wb) show?Ulberto
Formal class 'jobjRef' [package "rJava"] with 2 slots ..@ jobj :<externalptr> ..@ jclass: chr "org/apache/poi/xssf/usermodel/XSSFWorkbook"Rabblement
S
2

I was able to fix this error by disabling XLSX package

detach("package:xlsx", unload = TRUE)
Saloop answered 12/11, 2021 at 1:57 Comment(1)
A related solution to this is to make sure that you're using the openxlsx package by calling openxlsx::createWorkbook()Composure
B
6

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)
Bainite answered 23/10, 2019 at 14:17 Comment(1)
Worked for me without reinstallingTombaugh
L
4

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.
Lagrange answered 9/11, 2018 at 21:57 Comment(1)
This worked for me, but it would be great to know whats causing the issueSzabadka
S
2

I was able to fix this error by disabling XLSX package

detach("package:xlsx", unload = TRUE)
Saloop answered 12/11, 2021 at 1:57 Comment(1)
A related solution to this is to make sure that you're using the openxlsx package by calling openxlsx::createWorkbook()Composure
I
1

Works for me unload it and re-install (or uninstall) library rio

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

install.packages("openxlsx", "rio")

library(openxlsx)
Intonation answered 15/11, 2019 at 21:0 Comment(0)
I
1

Encountered the same issue, removed library(xlsx) & library(readxl) from script and now runs without issue. Using the below libraries now in script for context:

library(openxlsx)
library(rio)
library(rJava)
International answered 19/1, 2021 at 5:44 Comment(0)
M
0

I experienced a similar issue when I tried to re-run code to export an XLSX file. To have my code working well, I just made sure the workbook (wb) was removed from my global environment. I think inserting the following line right before your code may help.

  rm(list=deparse(substitute(wb)),envir=.GlobalEnv)
Milano answered 22/3, 2021 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.