Cannot open the file after writing xlsx file using R openxlsx package - r

At first, I tried to read and write xlsx files in R (while comparing the output between the xlsx and openxlsx packages).
I work on mac os.
It worked well to read xlsx files using the read.xlsx() from both packages.
However, when it comes to writing a new file, only the xlsx::write.xlsx() worked.
To be more exact, the openxlsx::write.xlsx() command gave no error, and an xlsx file was successfully saved, but when I tried to open the file using Numbers (by double clicking on the file in the folder), an error message popped up telling me the file cannot be opened.
I tried different data frames, but the results remained the same. To show an example, please refer to the following line which I took directly from R help page. It should work but does not work for me.
write.xlsx(iris, file = "writeXLSX1.xlsx", colNames = TRUE, borders = "columns")
Anyone tell me what the problem is? I tried to google for old threads but it seems no one is discussing this problem. I know in many similar threads people suggested changing packages, okay...before that, can you tell me what the limitations of openxlsx are?

Related

Reading Excel: evaluation error with zip file cannot be opened [duplicate]

I am using R with readxl package. I try to import an Excel file with the following command:
library(readxl)
city_codes <- read_excel("./data/file.xlsx", sheet = "city_codes")
It says it is a zip file and cannot be opened:
Error in sheets_fun(path) :
Evaluation error: zip file './data/file.xlsx' cannot be opened.
Any ideas?
I had this error, but for me, it was just that I had the sheet open in Excel while trying to read it into R. I guess the package wrongly perceives it as a zip file when it's trying to read it while Excel has partial ownership of it (and this blocks the read).
The error message is readxl's funny way of saying "file not found". That exact line of code gives me the exact same error, and the file doesn't even exist for me.
Note: I'm on version 1.0.0 of readxl
For me the "Evaluation error: zip file" error message also appeared when the excel spreadsheet was still opened in MSExcel.
You can specify a path to a file, only if it is nested on the working directory.
For example: If your working directory is MyWD and there is a folder in it, named MyData and another folder within MyData named MyNestedData, and finally myExcelFile.xlsx
read_excel("MyData/MyNestedData/myExcelFile.xlsx",sheet = "Sheet2") #will work
read_excel("MyWD/MyData/MyNestedData/myExcelFile.xlsx",sheet = "Sheet2") #will not work
If your excel worksheet is password protected, read_excel won't be able to access it and will give you this error.
If it needs protection, I would suggest p/w protecting the folder it's in, and then unprotecting the worksheet.
You may also get this error if you are using the wrong read function.
For example, read_xlsx("file.xls" ...) will throw the error.
This can happen if you forget to do the following before you use the read_excel function
setwd("C:\\map\\map_in_map\\map_in_map_in_map_where_the_file_is")
Try to change file restrictions if you are working on company's computer. This worked for me to solve this issue.
You may try to specify the full path instead of ./path/to/file
Try to make a new file. click "save as" and make a new name. Run again your code. it worked well to me :)
Although the solution was already given, but for the sake of documentation I will leave my situation here:
I ran into the same problem and couldn't figure out why. It seems that my excel file had closed with an error or something similar. I had to open the excel file, and save it again. When I ran R, everything worked smoothly.
The issue was that there was hidden files inside my working directory. I copied them to outside and exclude all directories. Now it works when I run the code.
In my case the xlsx was created using Excel 2007, which caused this issue. Xlsx files from newer Excel versions work.
In my case the file was not visible in the directory although I had deleted it. I had to search for the file using a software called "Everyting" and I deleted it. After that it worked.
For me, I was downloading a file from Google Drive into a tempfile with extension .xlsx... Realised the file in Google Drive was .xls so I changed it to that and it worked. Kinda rare situation though.
I got the same error message.
In my case the line that worked properly specified the sheet by a string (copied from excel). Suddenly, it did not work anymore (with the mentioned error message). I tried several things explained here. Ended up specifying the sheet by its number in the excel file - worked!
read_xlsx("table.xlsx", sheet="name_of_sheet") # suddently corrupt
read_xlsx("table.xlsx", sheet=4) # works
Why? No update of the package or something. I dislike to have to use a less specific line.
UPDATE: Restarting the R session made the initial line work again (although, I HAD checked the wd before...)

Error when trying to export data frame using write_xlsx

Been using R for around two years now and this is the first time i've encountered this issue. I have a data.frame with approx 31,000 rows and 400 cols and I want to export to an excel sheet using write_xlsx
When I try this I get the following error:
write_xlsx(data, "FILEPATH")
[ERROR][libxlsxwriter/packager.c:1142]: Error in writing member in the zipfile
[ERROR] workbook_close(): Zip ZIP_ERRNO error while creating xlsx file 'FILEPATH'. System error = No error
Error: Error in libxlsxwriter: 'Zip generic error ZIP_ERRNO while creating the xlsx file.'
I think this error started when I inadvertently tried to open an Excel file that R was writing before it had actually completed. I have shut down R and restarted and also re-installed the writexl package to no avail.
The data i'm attempting to export is approx 150mb. When I try to write a smaller file (1.5mb) this works but when I try to open the excel file it says it is locked for editing by R and can open be opened 'read only'.
I have exported much larger data than this in the past with no issues so cannot figure this out, any help appreciated.
What OS are you using and is it possible that you are running out of free space in the tmp directory? – jmcnamara yesterday
I think you've cracked it. I had returned to work on Monday, ran the script again and, sure enough, it worked first time. Only difference is I had deleted some content from the directory I was working in because I had got a disk full notice!
I don't know write_xlsx but I would suggest a simple alternative as the following:
library(rio)
library(xlsx)
export(data, "exportname.xlsx")
However in your code might you need to add the file name as the following:
library(writexl)
write_xlsx(data, "FILEPATH// file name.xlsx")
Might this source could be helpful: https://datatofish.com/export-dataframe-to-excel-in-r/

Exporting csv using excel into R

I hope someone can help me solve the problem I am currently facing with excel. I have been trying to export a csv file I wrote in Excel Ver 16 into R studio but it keeps giving the "incomplete final line found by readTableHeader on 'Book1.csv'" error. I have included the screen shot of the error and the files I had used for this. This doesnt seem to happen for the other data set I downloaded directly from Kaggle called "adult-test.csv" though.
I have tried everything from reinstalling R, R studio, Excel, I even resorted to using Google Sheets and it still doesn't work. If anyone knows what I am doing wrong please do help!
Image of my R studio code
Picture of the csv file I am failing to read
Hard to guess here.
Sometimes Excel saves csv files incorrectly - even though cells below your table are empty Excel saves them in the csv because maybe in the past, there was something written in there.
So, here is a suggestion
Maybe try to read the Excel file directly, not save it first as a csv. You can do that for example with read.xlsx() from the openxlsx package.
If that does not help, please open the csv file with a text editor (not with Excel again). You will then be able to see the actual problem and if necessary, post the text here (instead of the Excel screenshot)

Unable to read SPSS file from working directory

I have created a folder that has my dataset and set it as my working directory in Rstudio.
The dataset is an SPSS file which I named "Stats in R", I downloaded the packages foreign and Hmisc and tried to run to the following command to get the dataset:
data = read.csv("Stats in R.csv", header = TRUE)
…but the console showed the following message:
Error in file.choose() : file choice cancelled.
The problem is, my dataset is in the working directory but whenever I try to open it, R shows me an empty folder. I run the dir() command and instead of getting the directory content in the console, I get the following message:
character(0).
I really don't know what's the problem, the SPSS dataset is saved as a SAV file. I tried other extensions as well like CSV but nothing worked. Any suggestions, please? I really need to sort out that issue soon. Thank you!
It seems like you have some issue with your working directory - it's either not where you think it is, or you don't have full permissions and something is blocking the R Studio interface from accessing it.
Since you want to get this resolved quickly, the approach I would try is importing the file using the entire file path instead of the working directory, and then you can troubleshoot your WD when you have time.
This should work:
df <- read.csv("C://Users//Mina//Folder//Subfolder//Stats in R.csv", header = TRUE)

Error Reading Multiple Excel Sheets Using openxlsx package in R

I'm trying to load an Excel workbook with a large number of tabs into R, do some analysis, and then export the results back into Excel. I'm using the openxlsx package because of some of the features of that package that are not easily accessible using other packages (such as the ability to create "comments" in the output file, color code the tabs, and work with 64-bit R).
When I try to read in the workbooks, I sometimes get the following error message (or something similar):
Error in unzip(xlsxFile, exdir = xmlDir) :
cannot open file 'C:/Users/MENDEL~1/AppData/Local/Temp/RtmpIb3WOf/_excelXMLRead/xl/worksheets/sheet5.xml': Permission denied
This error message doesn't always show up - but sometimes it will appear and the program crashes.
Does anyone have any ideas how to fix this problem? I don't know why the program sometimes thinks it doesn't have permission to access the sheets.
Thank you in advance!
I can think of two possible scenarios for this error:
Scenario 1:
C:/Users/MENDEL~1/AppData/Local/ (This looks like you are trying to read a temporary file)
Solution:
If that is the case try moving the file to a different location like desktop and make sure that you update your working directory accordingly.
Scenario 2
C:/Users/MENDEL~1/AppData/Local/Temp/RtmpIb3WOf/_excelXMLRead/xl/worksheets/sheet5.xml' (Looks like there is some issue with Sheet5 which is of type .xml and the openxlsx does not allow you to read .xml)
Solution:
Check if there is some issue with the format or contents of sheet5 in the file that you are trying to read.
For additional information check CRAN Documentation

Resources