using xlsx package in R to adjust page setting in xlsx file - r

I am trying to change the page setting of a xlsx file so that it will be printed as landscape, but not portrait, I tried the following:
library(xlsx)
wb <- createWorkbook()
sheet <- createSheet(wb, "Sheet1")
ps <- printSetup(sheet, landscape=TRUE, copies=3)
This is OK if I create a new Excel workbook, but I can't use it when I am using the loadWorkbook function to load an xls file. I wonder why.
Update: I am working on a xls file but not xlsx file and found the answer below cannot solve my problem, any further suggestions? Thanks.

I can get it to work by using getSheets to select the sheet to set the print area for:
wb <- loadWorkbook("test.xlsx")
printSetup(getSheets(wb)[[1]], landscape=TRUE, copies=3)
saveWorkbook(wb,"test.xlsx")

Related

How can I load an xlsx workbook into R?

I have an xlsx workbook on my desktop with existing sheets. I have a dataframe in R that I would like to add to this workbook as a new sheet.
Using library(openxlsx) I want to do:
wb <- loadWorkbook("workbook.xlsx", isUnzipped=TRUE)
addWorksheet(wb, "New_Sheet")
writeData(wb, "New_Sheet", df)
saveWorkbook(wb, "workbook.xlsx", overwrite=TRUE)
however the program fails at the first line:
wb <- loadWorkbook("workbook.xlsx", isUnzipped=TRUE)
I get the error message:
Error in match(sheetrID, file_rIds): object sheetrId not found
I also created a dummy file (excel workbook with just two existing sheets and some dummy strings in each sheet) and I get the same error message.
I also tried uninstalling openxlsx and re-installing. No change.
As mentioned in the comments by #MrFlick (thank you!), Excel files are actually zipped files that contain XML files. So the workbook must be unzipped before it can be opened in R.

Change Excel Worksheet Tab Color

I have added a worksheet to an excel spreadsheet using the following code using the xlsx package in R
library(xlsx)
wb <- CreateWorkbook()
newsheet <- createSheet(wb, sheetName = 'Sheet1')
Is there a way to automatically change the color of the tab for "Sheet1"?
There is no reference to tabs in xlsx
My first instinct was to look look at XLConnect but there is no reference to tabs there either.

Remove a worksheet in Excel using r package xlsx

As the title stated, I would like to delete the second worksheet in excel using r with package xlsx. However, after running the code, nothing happened. Following is a sample code I used to delete the sheet:
path = "C://ECOS//Code//Test//data.xlsx"
sheets = getSheets(loadWorkbook(path))
removeSheet(loadWorkbook(path), sheetName = names(sheets[2]))
Really appreciate helps. Thank you!
You need to save the workbook in the same file (or different if you prefer).
wb = loadWorkbook(path)
removeSheet(wb, sheetName = names(sheets[2]))
saveWorkbook(wb, path)

Readin .xlsx file into R

Im trying to read an excel file into R. It's about the following file in my cwd:
> list.files()
[1] "Keuren_Op_Afspraak.xlsx"
I installed XLConnect and am doing the following:
library(XLConnect)
demoExcelFile <- system.file("Keuren_Op_Afspraak.xlsx", package = "XLConnect")
wb <- loadWorkbook(demoExcelFile)
But this gives me the error:
Error: FileNotFoundException (Java): File '' could not be found - you may specify to automatically create the file if not existing.
But I dont understand where this is coming from. Any thoughts?
I prefer using the readxl package. It is written in C so it is faster. It also seems to handle large files better. The command would be:
library(readxl)
wb <- read_excel("Keuren_Op_Afspraak.xlsx")
You can also use the xlsx package.
library(xlsx)
wb <- read.xlsx("Keuren_Op_Afspraak.xlsx", sheet = 1)
Edit :#Verena
You can also use this function much faster:
wb <- read.xlsx2("Keuren_Op_Afspraak.xlsx", sheet = 1)
You have to change your code that way:
library(XLConnect)
demoExcelFile <- "Keuren_Op_Afspraak.xlsx"
wb <- loadWorkbook(demoExcelFile)
You probably took the example from here:
http://www.inside-r.org/packages/cran/XLConnect/docs/loadWorkbook
This line
system.file("demoFiles/mtcars.xlsx", package = "XLConnect")
is a way to get sample files that are part of a package. If you download the zip File of XLConnect and look into the folder structure you will see that there is a folder demoFiles that contains mtcars.xlsx. And the parameter package="XLConnect" tells the method to look for the file in this package.
If you type it into the command line it returns the absolute path to the file:
"C:/Users/Expecto/Documents/R/win-library/3.1/XLConnect/demoFiles/mtcars.xlsx"
To use loadWorkbook you simply need to pass the relative or absolute filepath.

Can't open Excel File created in R language

I get the corruption error when I try to open the Excel workbook created in R.
I tried with both .xlsx and .xls extensions but neither worked!
The code that I used for doing all this is:
wb <- loadWorkbook("RCreated.xls", create = TRUE);
saveWorkbook(wb)
createSheet(wb, name = "First")
HELP!
Create the sheet BEFORE saving the workbook.

Resources