How to fill an excel file using R, while in the excel file - r

I'm trying to access an R script via an Excel file. That is, while I'm still in the Excel file, I'd like to run an R script, that fills that same Excel file. The reason for this is that Excel is not computationally efficient enough.
Now I know that normally, you're not able to fill an Excel file via R script when the Excel file is opened. My question is, is there a way to work around this? For instance, by letting the R script close, fill and then open the Excel file I'm calling the script from?
Any help would be much appreciated

Related

Writing new data to an existing excel file that has an XML map attached, without losing the XML data in R

I am trying to write to an excel file that needs to be uploaded somewhere. The target software creates an excel file which has an XML map attached to it. I recreated the entire file structure in R using code, but any time I try to write to that excel file, i think R actually deletes the old file and creates a new one instead, because the XML map is gone the moment I start writing any data to it. Loading up the workbook also doesn't seem to bring in the xml map, only the workbook data and sheets.
Is there a way to write data to this existing file within R (or python) without losing the XML map? Now i need to generate a file and manually copy paste the data into the other excel file.
I've been trying with xlsx, readxl, xml2 packages.
In the past Ive deal with a similar problem. To my knowledge, almost all the R packages that interact with excel replace the entire file with a new one. Except the openxlsx package. You can replace specific sheets, and range of cells, whitout touching the rest (data, styling , etc..). One last comment is that I dont know much about XLM maps, but maybe you are lucky.
Here is the vignette:
https://cran.r-project.org/web/packages/openxlsx/vignettes/Introduction.html
Hope it helps

Exporting CSV from R, not being read by program until saved by excel

I'm exporting CSV files from R, using write.csv
I've also tried various encoding options.
The CSV files open fine and read manually.
However, I need to process these files in another program (METAL https://genome.sph.umich.edu/wiki/METAL) and the program is pretty much unable to recognize the text in the files.
But, when I open the CSV files in excel, and save them again manually with excel-- same name, same place, same encoding, without changing anything except clicking save as CSV, then METAL is able to recognize the text in the CSV files.
I was wondering if anyone has any suggestions how to fix this? It's very cumbersome to go in each file and have to re-save manually.

how to save and close excel file in R using Shell()

I have an excel file that queries Olap cube using DAX.
For some unfortunate reasons with DBAs, this is currently only viable way for me to load cube data into R.
So I am trying to automatize opening, refreshing, and closing excel file using R script, instead of doing that manually.
After some web search, I am able to open the excel file using Shell(). And the excel file is set to refresh itself when it is opened.
What come's next is where I need help; I want to save this file (like Ctrl + S) and close it. So I can load it to my R.
#open excel file
shell.exec("zero_billed_cases.xlsx")
#save excel file
"How do I do this?"
#close excel file (this is what I found online, but this only opens the file)
shell('\"zero_billed_cases.xlsx\"')
Thanks for your time and effort!

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.

Output of saved .R files in r

I want to save my code in R. I did:
save(Data,file="Code_Data.R")
When I open the file in R again, the code looks like hieroglyphics.
How can I save the code in a way, that I can read the code in an editor or RStudio again?
save outputs a binary copy of the objects you tell it to save, not R code. Because you are naming this file with a ".R" extension, RStudio is blindly trying to open this binary file as R code, and you are seeing the results of that mess.
Technically, the R language doesn't care what the extension of the file is. As long as you know that the file contains, you can load it back in with the command load("Code_Data.R"). However, if you want to get RStudio to recognize that this is actually a file containing binary data and not R code, try saving the file with the canonical ".RData" extension:
save(Data, file="Code_Data.RData")
Using the ".RData" extension will also help you and other programmers who look at your code avoid this confusion in the future.

Resources