File opened with XLConnect doesn't close - r

I am working on an excel file with several sheets:
path_metadata<- file.path("archivos", "metadatos", "metadatos-copia.xlsx")
metadata_wb <- loadWorkbook(path_metadata)
Then I do some sheet manipulation (e.g. I write a data frame on a sheet), so I write it:
writeWorksheet(metadata_wb, df, sheet = "metadata")
And apply some style I have previously worked out
setCellStyle(metadata_wb,
formula = rango_header_1,
cellstyle = style_header)
And lastly, I save it
saveWorkbook(metadata_wb)
The problem comes when I try then to move onto the excel file and modify and save anything on it, it reports I cannot do it, because another instance keeps the file open.
I cannot do a close(metadata_wb) with an object of class workbook
What am I missing?
Thanx

Related

XLConnect: Error: IllegalArgumentException (Java): Sheet index (-1) is out of range (no sheets)

I am trying to use XLConnect to load in a series of excel workbooks that I have. Using the code:
BASZ <- loadWorkbook("BASZ.xlsx", create = TRUE)
works every time, and gives me a formal class workbook. However when I go to read in the worksheet I wish to use:
data <- readWorksheet("BASZ", sheet = "Sheet1")
I always get the same arguement:
"Error: IllegalArgumentException (Java): Sheet index (-1) is out of range (no sheets")
Just yesterday this code worked, im new to this and wondering why this continues to occur. Furthermore; it doesn't matter which excel workbook I try to load, the same error occurs when trying to read in the specific sheet I want to work with. It must be a syntax issue or something im doing wrong right? I fail to understand why it would work, then I close out Studio, then the next day it won't...?
If you have already loaded the excel file using loadWorkbook(), you can use the function readWorksheet() to read individual sheets. You would only use readWorksheetFromFile() if you had not previously loaded the file. So your code should read:
BASZ <- loadWorkbook("BASZ.xlsx", create = TRUE)
data <- readWorksheet(BASZ, sheet = "Sheet1")
Note that in the second line, the first argument is the variable BASZ, not a quoted string.
Okay so just in case someone else makes the same mistake as me; you have to be working within the directory your xlsx file is in.

XLConnect: saveworkbook update error

saveWorkbook() function in XLConnect saves the workbook and the changes and updated calculations are visible in the excel file but not on R (because it has a formula not accepted by the apache poi)
However, to view the cell I save the file to disk and call it using another function. But when I call the same file again the calculated fields still show the old values. I don't want to save the excel file every time I make a change in the workbook.
Would you know a workaround to be able to call the new values without manually saving excel?
Code -
options(java.parameters = "-Xmx1024m")
library(rJava)
library(XLConnect)
wb = loadWorkbook(file.choose(), create = TRUE)
readWorksheet(wb,16, region = 'D25:D26')
writeWorksheet(wb,-.45,sheet = 16,startRow = 25,startCol = 4)
setForceFormulaRecalculation(wb,sheet = 16, TRUE)
saveWorkbook(wb)
detach("package:XLConnect", unload=TRUE)
detach("package:XLConnectJars", unload=TRUE)
library(xlsx)
y = read.xlsx(file.choose(), sheetIndex = 16)
So the Excel file on the system shows the changes corresponding to the new -.45 value but when I read the file again, the calculated values are the old values and not the new ones. This gets fixed if I save the file manually.
I believe the command you are using is correct but maybe some small modifications would make this work.
I think you could try placing the needed calculations in a different sheet in excel and treat the data you inserted as a dependency for those calculations in the new sheet.
Then read it in as a fresh workbook and call the new sheet. I think that will you the output you need.
setForceFormulaRecalculation(wb, sheet = "*", TRUE)
I would use this command to force all sheets to recalculate instead.
Hope that helps!

writeWorkSheet function in R not pasting values into Excel

I am trying to copy some data from an R data frame (Shipments) to an excel file using writeWorkBook function in the XLConnect package. However, it is not copying anything to the excel file. The execution doesn't result in any error/warning appearing in the console. It just doesn't copy.
I have loaded the library XLConnect and made sure I am not loading the library xlsx. The column to be copied has been type-casted to dataframe as I thought that might be an issue.
wbnames is an additional thing. I directly wrote the sheet name in the writeWorkBook and it should have worked fine. Even with wbnames there hasn't been any change in the result.
I originally intended to copy the content to a macro file and then run the macro file from R itself but it wasn't working. So I thought it may be because of macro file but the function is not working on .xlsx itself.
So, not sure what is the issue. Would be grateful if I can get some help here. Am I missing something?
library(XLConnect)
library(RDCOMClient)
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open(FILEPATH+FILENAME.XLSX)
xlWb <- loadWorkbook(FILEPATH+FILENAME.XLSX)
wbnames <- as.vector(getSheets(xlWb))
# Copy a column from the existing data frame and paste it to the first
# sheet of the FILENAME.XLSX, starting at Row#6, no headers and no rownames:
writeWorksheet(xlWb, as.data.frame(Shipments$SHIPMENT_ID),
sheet = wbnames[1], startRow = 6, header = F, rownames = NULL)
xlWb is the R-object that contains the workbook. It looks like the data has been written to the workbook, which is good. In order to view in Excel format, however, you still need to save the workbook to Excel. Add this line after your code and you should see a document called your_file_name.xlsx with your data in your working directory:
XLConnect::saveWorkbook(xlWb, "your_file_name.xlsx")

How do I modify an existing a sheet in an Excel Workbook using Openxlsx package in R?

I am using "openxlsx" package to read and write excel files. I have a fixed file with a sheet called "Data" which is used by formulas in other sheets. I want to update this Data sheet without touching the other.
I am trying the following code:
write.xlsx(x = Rev_4, file = "Revenue.xlsx", sheetName="Data")
But this erases the excel file and creates a new one with just the new data in the "Data" sheet while all else gets deleted. Any Advice?
Try this:
wb <- loadWorkbook("Revenue.xlsx")
writeData(wb, sheet = "Data", Rev_4, colNames = F)
saveWorkbook(wb,"Revenue.xlsx",overwrite = T)
You need to load the complete workbook, then modify its data and then save it to disk. With writeData you can also specify the starting row and column. And you could also modify other sections before saving to disk.
I've found this package. It depends on openxlsx and helps to insert many sheets on a xlsx file. Maybe it makes easier:
Package documentation
library(xlsx2dfs)
# However, be careful, the function xlsx2dfs assumes
# that all sheets contain simple tables. If that is not the case,
# use the accepted answer!
dfs <- xlsx2dfs("Revenue.xlsx") # all sheets of file as list of dfs
dfs["Data"] <- Rev_4 # replace df of sheet "Data" by updated df Rev_4
dfs2xlsx(dfs, "Revenue.xlsx") # this overwrites the existing file! cave!

Appending r output in a single sheet of xlsx file

How can i append my R outputs in a single sheet of xlsx file? I am currently working on web crawling wherein i need to scrap the user reviews from website and save it in my deskstop in xlsx format. I need to every time change the website url(as user reviews are in different pages) in my code and save the output in one sheet of xlsx file.
Can you please help me with the code of appending outputs in a single sheet of xlsx file? Below is the code which i am using: Every time i need to change the website url and run the same below code and save the corresponding output in a single sheet of mydata.xlsx
library("rvest")
htmlpage <- html("http://www.glassdoor.com/GD/Reviews/Symphony-Teleca-Reviews-E28614_P2.htm?sort.sortType=RD&sort.ascending=false&filter.employmentStatus=REGULAR&filter.employmentStatus=PART_TIME&filter.employmentStatus=UNKNOWN")
proshtml <- html_nodes(htmlpage, ".pros")
pros <- html_text(proshtml)
pros
data=data.frame(pros)
library(xlsx)
write.xlsx(data, "D:/mydata.xlsx", append=TRUE)
A trivial, but super-slow way:
If you only need to add (a few) row(s) to an existing Excel file, and it only has one sheet to which you want to append, you can just do a simple read => overwrite step:
SHEET.NAME <- '...' # fill in with yours
existing.data <- read.xlsx(file, sheetName = SHEET.NAME)
new.data <- rbind(existing.data, data)
write.xlsx(new.data, file, sheetName = SHEET.NAME, row.names = F, append = F)
Note:
It's quite slow in general, will work only for small scale
read.xlsx is a slow function. Try read.xlsx2 to make it much faster (see the difference in the docs)
If your R process is run once and keeps working for a long time, obviously don't do it this way (reading and overwriting a file is ridiculous in that case)
look at package xlsx.
?write.xlsx will show you what you want. append=TRUE is the key.
========= EDIT TO CORRECT =========
As #Jakub pointed out, append=TRUE adds another worksheet to the file.
========= EDIT TO ADD: ANOTHER METHOD ==========
Another method is to save the data to a .csv file, which could easily open from excel. In this case, the append=T works as expected (adding to the existing sheet):
write.table(df,"D:/MyFile.csv",append=T,sep=",")

Resources