How to read the external excel workbooks inside Formulas using R? - 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.

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

Is there a R function to add data from a df and append it to a sheet in excel

I have a R code that uses joins from various tables and finally have an output dataframe. I need this to be appended to a new worksheet in excel file that already has three sheets in it. I am on a mac and however I try the library(rjava) that is required for library(xlsx) wont load. Is there any other library (that doesnt need java) that I can use for this?
Edit to add: the existing excel sheet has graphs and charts in it

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

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.

Exporting output from R into an open Excel workbook

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..

Resources