Alternative for r2excel package in R 3.3.3 - r

Until recently I was happily using the r2excel package to save multiple data sheets into excel format using the script:
library("r2excel")
filename <- "r2excel-example1.xlsx"
wb <- createWorkbook(type="xlsx")
sheet <- createSheet(wb, sheetName = "example1")
xlsx.addTable(wb, sheet, head(iris), startCol=2)
saveWorkbook(wb, filename)
Now that I have updated R to version 3.3.3 this package is no longer available:
devtools::install_github("kassambara/r2excel")
Error in loadNamespace(name) : there is no package called ‘curl’*
I have found it VERY difficult to export files to excel using any other packages in the old version. Can anyone recommend a solution for exporting multiple dataframes into a single excel file, that is compatible with R3.3.3?
(saving to CSV is not an option for several reasons including the need for saving multiple sheets in one file).
Thanks

Related

Can I save an Excel Workbook as a binary file using R?

In R I am trying to save an Excel workbook as a binary worksheet (.xlsb) instead of the standard (.xlsx or .xls) method. Using packages like openxlsx or xlsx do not work because they do not convert the file into binary format. I have been digging and found the package excel.link but it keeps crashing my R session and doesn't seem to work in a timely manner.
Does anyone know of a method to achieve this?
No, Excel Binary format is a proprietary encoding/compression format used by Microsoft that is not shared. You can only view and edit Binary files in excel. Is there any reason you cant save them as csvs or a regular excel file? If it is to large you can save it as a gzip file with
data.table::fwrite(file, "filename.gzip")
It is possible to convert an XLSX file to XLSB with the R package RDCOMClient
library(RDCOMClient)
path_XLSX_File <- "C:\\...\\xlsx_File.xlsx"
path_XLSB_File <- "C:\\...\\xlsb_File.xlsb"
xlApp <- COMCreate("Excel.Application")
xlApp[['Visible']] <- FALSE
xlWbk <- xlApp$Workbooks()$Open(path_XLSX_File)
xlWbk$SaveAs(path_XLSB_File, 50)
xlWbk$Close()
xlApp$Quit()
For all the format, see https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/ff198017(v=office.14). The "XLSB" format is "xlExcel12".

Write out data to xlsx template in R without using XLConnect

I wanted to write out some data to xlsx file using a template xlsx file with pre-designed formatting.
I have already read the previous question: "Write from R into template in excel while preserving formatting". But the answer propose the use of XLConnect package which is not compatible with my environment as I am already using xlsx package. These two packages cannot be loaded at the same time due to the different Apache POI versions.
I wanted to ask what equivalent functions may exist outside the XLConnect package to loadWorkbook or setStyleAction which was mentioned as answer in the previous question as follows:
wb <- loadWorkbook("test.xlsx", create=TRUE)
setStyleAction(wb,XLC$"STYLE_ACTION.NONE")

Reading in xls and xlsx files in R

I am working with loads of xls and xlsx files at the same time with no easy way to convert them to the same file type.
I am facing issue reading them in because read.xlsx() from "xlsx" package works just fine with xls files but I am getting the Java Out of Memory error when trying to read in xlsx files. I tried to use the following line to extend memories with no success:
options(java.parameters = "-Xmx1000m")
As an alternative option I have tried read.xlsx() from "openxlsx" package but it does not read xls files and the aforementioned two packages are not compatible when loaded at the same time. I faced the same difficulty with the "XLConnect" package where again I face java errors when trying to use "xlsx" and "XLConnect" packages loaded at the same time.
I would be interested what people do to solve situations like this?
You can consider the read_excel function in the readxl package:
read_excel(path, sheet = 1, col_names = TRUE, col_types = NULL, na = "", skip = 0)
You can even specify which sheet in the xlsx file you want to import in, whether the first row consists of column names, as well as the missing value used in the excel files.

R XLSX and XLConnect packages - Formatting the workbood object created by XLConnect using xlsx package?

I have written a R code to create excel workbook and added the data to it using XLConnect package.
wb <- XLConnect::loadWorkbook(Name,create = TRUE)
and added some data frame to this file. Now, I want to access this XLConnect object wb from xlsx package and do some formatting like adding a border, font, wraptext and alignment on the dataframe inside the file. Is this possible?
Kindly let me know if anything is unclear or need more clarification.
It seems that it is not possible to use XLConnect and xlsx packages in the same R session or at least not in single package. I am using openxlsx package for formatting the excel file.

Load 93-2003 Excel Worksheet (.XLS) into Excel R

I am trying to load excel worksheets into R using the xlsx package. The files are saved as old 97-2003 worksheets (the endings are .XLS) for newer files the code below worked fine.
df <- read.xlsx(filename,sheetIndex=2)
However, when I try on the older files I get the error message:
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
org.apache.poi.hssf.OldExcelFormatException: The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)
I know the error has to do with the files being in the older format but I do not know how to solve this. I have too many files to manually update each one.
Any suggestions would be greatly appreciated!
P.S. apologies for not adding a fully reproducible example. I do not know how to attach files to go along with my question.
Package readxl is one way to read Excel files. The advantage is that there is no dependy to Java or other.
Your code would be
library(readxl)
df <- read_excel(path = filepath, sheet =2)
It should work with XLS and XLSX files.
Use excel_sheets(filepath) to get the name of sheets to import and pass them through the sheet arg of read_excel. You can do a loop with that if it helps you.

Resources