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

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

Related

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

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.

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.

How to write data into a macro-enabled Excel file (write.xlslx corrupts my document)?

I'm trying to write a table into a macro-enabled Excel file (.xlsm) through the R. The write.xlsx (openxlsx) and writeWorksheetToFile (XLconnect) functions don't work.
When I used the openxlsx package, as seen below, the resulting .xlsm files ended up getting corrupted.
Code:
library(XLConnect)
library(openxlsx)
for (i in 1:3){
write.xlsx(Input_Files[[i]], Inputs[i], sheetName="Input_Sheet")
}
#Input_Files[[i]] are the R data.frames which need to be inserted into the .xslm file
#Inputs[i] are the excel files upon which the tables should be written into
Corrupted .xlsm file error message after write.xlsx:
Excel cannot open the file 'xxxxx.xslm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file
After researching this problem extensively, I found that the XLConnect connect package offers the writeWorksheetToFile function which works with .xlsm, albeit after running it a few times it yields an error message that there is no more free space. It also runs for 20+ minutes for tables with approximately 10,000 lines. I tried adding xlcFreeMemory at the beginning of the for loop, but it doesn't solve the issue.
Code:
library(XLConnect)
library(openxlsx)
for (i in 1:3){
xlcFreeMemory()
writeWorksheetToFile(Inputs[i], Input_Files[[i]], "Input_Sheet")
}
#Input_Files[[i]] are the R data.frames which need to be inserted into the .xslm file
#Inputs[i] are the excel files upon which the tables should be written into
Could anyone recommend a way to easily and quickly transfer an R table into an xlsm file without corrupting it?

How to open SAS files using Excel?

I have a set of SAS data sets and I want to open it using Excel or R. I don't have a SAS software with me so i can't use the export option in it. Is there any converter that converts from SAS7BDAT to excel?
Thanks
I help develop the Colectica for Excel addin, which opens SAS data files in Excel. No SAS software or ODBC configurations are required. The addin directly reads the SAS file and then inserts the data and metadata into your worksheet.
Imports SAS .sas7bdat data and column names
Imports SAS .sas7bcat formats and value labels when avalaible
The Excel addin is downloadable from http://www.colectica.com/software/colecticaforexcel
Documentation is available in the user manual.
You could use SAS add in for Microsoft office to open the SAS dataset in Excel. Not sure if it is free though.
http://support.sas.com/software/products/addin/
As Reese suggested you can use - SAS Universal Viewer , its free!!
Here is the link :-
https://support.sas.com/downloads/browse.htm?fil=&cat=74
Or you can download SAS University Edition, which is also free, it is more than just a viewer, you can write and execute programs in here.
http://www.sas.com/en_us/software/university-edition/download-software.html
Here a quick-and-dirty python five-liner to convert a .xpt file to .csv
import pandas as pd
FILE_PATH = "(directory containing file)"
FILE = "ABC" # filename itself (without suffix)
# Note: might need to substitute the column name of the index (in quotes) for "None" here
df = pd.read_sas(FILE_PATH + FILE + '.XPT', index=None)
df.to_csv(FILE_PATH + FILE + '.csv')
Hopefully this might help someone
I came across the same "need" and after some research here and there, I found a nice and easy way with R and the latest version of RStudio (as per 2020 June date - the FREE one). Using it, you can open various formats of files and RStudio generates for you the R script it ran behind. You can use this as a starting point, in order to have the .sas7bdat file opened, and then do the conversion step.
Steps to follow in order to import the file using the RStudio "visual" way: Evironment tab -> Import Dataset -> From SAS...
It will ask you to import the haven library. After the installation you will have a tab with the preview of the data within the file and also the R script ran behind which will look like this:
library(haven)
aux <- read_sas("//PATH_ON_YOUR_MACHINE_TO_FILE/actual_file.sas7bdat", NULL)
View(aux)
Notice the NULL there, it has the purpose of converting empty strings to NULL.
But wait, we also need to convert it to a .csv file in order to have the final job done. For this you simply add below those lines from above the following:
write.csv(aux, "actual_file.csv")
Which will produce within the same folder with the original SAS file, the desired .CSV one. If you want to have ";" as separator instead on "," use write.csv2(aux, "actual_file.csv"). Anyway Strings are enclosed by " " so it should be fine.

Resources