Exporting csv using excel into R - 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)

Related

Writing .xlsx in R, importing into PowerBi error

I'm experiencing an odd error. I have a large dataframe in R (75000 rows, 97 columns) and I need to save it out and then import it into Power Bi.
At first I just did the simple:
library(tidyverse)
write_csv(Visits,"Visits.csv")
and while it seems to export and looks fine in excel, the csv itself is all messed up when I look at the contents in Power Bi. Here's an example of what I mean:
The 'phase.x' column should only have "follow-up" or "treatment" in that column. In excel, looks great:
but that exact same file gets screwed up in Power Bi:
I figured that being a 'comma separated variable' file, there must be some extra comma somewhere, and I saved it as an .xlsx instead.
So, while in excel, I saved that .csv as an .xlsx and it opened great in Power Bi!
Jump forward a moment and instead of write_csv() in R, I use write.xlsx(). But now I get this error:
If I simply go to that file, open it in excel, save it and hit close, that error goes away and it can load into Power Bi just fine. I figure it has something to do with this question on here.
Any ideas on what I might be screwing up as I save it out of R? Somehow I can fix it in R and not have to open and save it every time?
In power BI check that your source has ignore quoted line breaks enabled. I've found this is often an issue with .csv files in PowerBI.

How to open mht file via R?

I met a problem to open mht files in R. There is an approach in which first I need to open it in Excel then save as .xlsx and after that read it in R.
But this way doesn't correspond to my requirements as soon as I need the program which must work automatically (no manual work needed).
But unfortunately I didn't found in the Internet how to do this.
Can someone advise me the way in which I can open file with mht format with some data in R?

R Copying to and Reading from csv Files

When I go to save Excel data that I've pasted into a .csv file, I get a formatting issue and often the saved file has all the numbers in each row as one long string.
My read statement is
resids<-read.csv("C:\\Projects\residuals_Parts3.csv",header=TRUE)
Any ideas on how to fix this?
The warning you are getting is fairly standard in Excel - any formatting you've added to the file (e.g. widening columns) will get lost if you don't save the file as an excel file.. and the warning is supposed to remind you of this. Personally, the extra click or two annoys me too.
If you would like to avoid converting excel files to CSV before bringing them into R, try the openxls package. It's saved me from a lot of that monkey business.

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

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?

Generating Excel file with XLConnect-Removed Feature: Format from /xl/styles.xml part (Styles)

I am using XLConnect in R for the purpose of daily report generation. I have a program that runs automatically at specific time to append the data for most recent date daily into an excel file (Excel 2007). The program works fine to do this task. But, sometimes when i open the excel file it says that "Excel found unreadable content. do you want to recover the content of this workbook?"
The best part of this issue is that i can't reproduce this issue again to know the exact root cause for the problem. It arises in a random manner. Because, when i try to run the program again it works fine. Can somebody help me to identify the root cause?

Resources