How to open an R data file in Excel Power Query? - r

How can I save data in an R compressed file format and then open it in Excel Power Query? I've opened in PowerBI Power Query no problem by setting the R path and using R.Execute(), but Power Query in Excel only has an RData.FromBinary() function.
Hopefully it can be loaded into Excel without any external dependencies as I'd like to switch from accdb files on a shared drive for the purpose of sharing easily refreshable data sets.
I tried exporting with these 3 methods:
saveRDS(tmp, "c:/users/ian/Documents/CRAN_mirrors.rds")
save(tmp, file="c:/users/ian/Documents/CRAN_mirrors.RData")
save.image(file="c:/users/ian/Documents/CRAN_mirrors.RData")
And tried importing with both:
= RData.FromBinary(File.Contents("C:\Users\ian\Documents\CRAN_mirrors.rds"))
and
= RData.FromBinary(File.Contents("C:\Users\ian\Documents\CRAN_mirrors.RData"))
Either of those import attempts gives me the following error:
DataFormat.Error: Exception of type 'Microsoft.Analytics.Modules.R.ErrorHandling.RException.Primitives.NotValidRDataException' was thrown.

Related

Why doesn't Power BI recognize my R output as an excel document?

I use the following function from the openxlsx package to export my dataframe to a local folder as an xlsx file:
write.xlsx(data frame, path, overwrite = TRUE)
I want to pull it into a Power BI project, but when I do, I get the following error from Power BI:
"The input couldn't be recognized as a valid Excel document."
But when I go into the file, save it and close, it then works fine.
Thank you

Saving Excel Table to OneDrive with R PowerQuery

I have a report that all it does is export it's data to excel via power automate using some R code.
"write_xlsx(dataset,"file_path.xlsx")"
However, I need to manipulate it further and it needs to be in a tabular format to do so. Yet, when I try "write.table" I get the error that my connection to OneDrive is not open. So I can export an Excel sheet with the data, but not the same data as a Table.
Any ideas?

Getting embedded nul string while reading an excel csv

I'm trying to open an Excel CSV file within R Studio but I get this error:
Error Is this a valid CSV file? embedded nul in the string: 'C\0a\0m\0p\0a\0g\0n\0e\0_\0N\0o\0C\0a\0r\0a\0v\0a\0g\0g\0i\0o\0_\0C\0o\0s\0t\0o\0>\00'
the file is generated automatically by the Google Ads platform as Excel csv and it works normally with Excel but in order to open it on R Studio I have to convert it as .xlsx
is there a way to bypass this or to convert the file without opening it?
otherwise the script which is based upon this file needs a manual passage to convert the source file
What function are you using to open it? Check your file and see if you have other commas within a column value; this may confuse the function. Also, it is worth trying to use the "Import dataset" option in the environment window within Rstudio. Try to use the readr option and adjust your import options until you have it correct. Check the package RAdwords maybe you can extract your Google Ads information without the CSV exporting step.

Importing to R an Excel file saved as web-page

I would like to open an Excel file saved as webpage using R and I keep getting error messages.
The desired steps are:
1) Upload the file into RStudio
2) Change the format into a data frame / tibble
3) Save the file as an xls
The message I get when I open the file in Excel is that the file format (excel webpage format) and extension format (xls) differ. I have tried the steps in this answer, but to no avail. I would be grateful for any help!
I don't expect anybody will be able to give you a definitive answer without a link to the actual file. The complication is that many services will write files as .xls or .xlsx without them being valid Excel format. This is done because Excel is so common and some non-technical people feel more confident working with Excel files than a csv file. Now, the files will have been stored in a format that Excel can deal with (hence your warning message), but R's libraries are more strict and don't see the actual file type they were expecting, so they fail.
That said, the below steps worked for me when I last encountered this problem. A service was outputting .xls files which were actually just HTML tables saved with an .xls file extension.
1) Download the file to work with it locally. You can script this of course, e.g. with download.file(), but this step helps eliminate other errors involved in working directly with a webpage or connection.
2) Load the full file with readHTMLTable() from the XML package
library(XML)
dTemp = readHTMLTable([filename], stringsAsFactors = FALSE)
This will return a list of dataframes. Your result set will quite likely be the second element or later (see ?readHTMLTable for an example with explanation). You will probably need to experiment here and explore the list structure as it may have nested lists.
3) Extract the relevant list element, e.g.
df = dTemp[2]
You also mention writing out the final data frame as an xls file which suggests you want the old-style format. I would suggest the package WriteXLS for this purpose.
I seriously doubt Excel is 'saved as a web page'. I'm pretty sure the file just sits on a server and all you have to do is go fetch it. Some kind of files (In particular Excel and h5) are binary rather than text files. This needs an added setting to warn R that it is a binary file and should be handled appropriately.
myurl <- "http://127.0.0.1/imaginary/file.xlsx"
download.file(url=myurl, destfile="localcopy.xlsx", mode="wb")
or, for use downloader, and ty something like this.
myurl <- "http://127.0.0.1/imaginary/file.xlsx"
download(myurl, destfile="localcopy.csv", mode="wb")

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