Exporting output from R into an open Excel workbook - r

I've found the package XLConnect to be useful for exporting matrices to a CLOSED workbook, but does anyone know how to write to an OPEN workbook?
Alternatively, does anyone know of code one can write in VBA to import a matrix from an R script file?
Thanks
Mike

I've been wanting to do just this and stumbled upon excel.link that writes easily into an active excel sheet. The method to write is very simple and straightforward:
library(excel.link)
xlrc[a1] <- seq(1, 10)
Note that inside the brackets you write the cell where the data will be written (if it is a dataframe, this cell will be the upper left of said dataframe).
Result in the active sheet of the active excel file:

Use package excel.link this can work with xlsm and also with open excels..

Related

Optimum way to overwrite an xlsx worksheet

I'm trying to write an Excel worksheet with the XLConnect package. The data I'm using is a data.frame (820*132). Once I'm done building the dataset, I'm using the writeWorksheetToFile function to export.
If the file does not exist yet and I am creating it from scratch, everything works well.
If I want to overwrite an existing sheet, the function takes approximately a minute to write and in addition, when I open the excel file, I have an error message saying: "we found a problem with some content in 'my_file.xlsx. Do you want to try to recover as much as we can?"
I tried to use other packages to write to excel like xlsx and openxlsx but they do not allow to overwrite a sheet without overwriting the entire workbook.
I've checked a few solutions such as this, but I not optimal.
I am looking for the most optimal way of writing excel worksheets, with an overwrite option that is suitable for large datasets.
I'm using the latest versions of R and RStudio.
My Excel verion is 1902, 64bits.

how to refresh an excel file from within R?

I have some excel file with simple formulas like =SUM(A1:A3).
I need to import the file into R, but before that I need to refresh the formulas. Is there a way to refresh the file from within R? There are good packages for importing the data in a R dataframe (eg. the R xslx package) but I need to refresh my formulas first.
Any suggestions?
Thanks!
You should be able to do this with RDCOMClient:
library(RDCOMClient)
ex = COMCreate("Excel.Application")
book = ex$Workbooks()$Open("my_file.xlsx")
book$Worksheets("Sheet1")$Calculate() # if you have many sheets you could loop through them or use apply functions based on their actual names
book$Save()
book$Close()
Here's another thread on the underlying VBA

How to read the external excel workbooks inside Formulas using R?

I would like to read the formulas inside a workbook using R. I have an excel file called "link.xlsx" with value 1 at cell A1 and another excel file called "myFile.xlsx" with value A1=[link.xlsx]Tabelle1!$A$1. I tried the following:
library('XLConnect')
wb<-loadWorkbook("myFile.xlsx")
getCellFormula(wb,1,1,1)
I get
[1]Tabelle1!$A$1
and not
[link.xlsx]Tabelle1!$A$1.
1) How I can fix the problem?
2)Is there a way to quickly get the list of external reference workbooks (linked excels) using R?
UPDATE: For the moment, I found a solution using the approach explained here:
Extract hyperlink from Excel file in R
but I still wonder if it is possible to solve the problem into a more compact way.

Read .rdata without R

I have received an .rdata file with what I think should be a list of co-ordinates (x,y) and their corresponding gray value; however, I do not know exactly the data type/format within the .rdata file. Is there a way for me to read these data without R? I do not have access to R. I have Excel and Matlab on a Mac. Please help. Unfortunately, I am not allowed to pass on the data (my Googling tells me someone familiar with R can export the data into a text of csv file easily).
Thank you in advance.
I downloaded R, read the .rdata in using load() and saved it out again using write.csv().

Fetch data from an open excel sheet into R?

I am wondering is it possible to read an excel file that is currently open, and capture things you manually test into R?
I have an excel file opened (in Windows). In my excel, I have connected to a SSAS cube. And I do some manipulations using PivotTable Fields (like changing columns, rows, and filters) to understand the data. I would like to import some of the results I see in excel into R to create a report. (I mean without manually copy/paste the results into R or saving excel sheets to read them later). Is this a possible thing to do in R?
UPDATE
I was able to find an answer. Thanks to awesome package created by Andri Signorell.
library(DescTools)
fxls<-GetCurrXL()
tttt<-XLGetRange(header=TRUE)
I was able to find an answer. Thanks to awesome package created by Andri Signorell.
library(DescTools)
fxls<-GetCurrXL()
tttt<-XLGetRange(header=TRUE)
Copy the values you are interested in (in a single spread sheet at a time) to clipboard.
Then
dat = read.table('clipboard', header = TRUE, sep = "\t")
You can save the final excel spreadsheet as a csv file (comma separated).
Then use read.csv("filename") in R and go from there. Alternatively, you can use read.table("filename",sep=",") which is the more general version of read.csv(). For tab separated files, use sep="\t" and so forth.
I will assume this blog post will be useful: http://www.r-bloggers.com/a-million-ways-to-connect-r-and-excel/
In the R console, you can type
?read.table
for more information on the arguments and uses of this function. You can just repeat the same call in R after Excel sheet changes have been saved.

Resources