Functions for saving and reading data to/from Excel file.
xl.read.file(
filename,
header = TRUE,
row.names = NULL,
col.names = NULL,
xl.sheet = NULL,
top.left.cell = "A1",
na = "",
password = NULL,
write.res.password = NULL,
excel.visible = FALSE
)xl.save.file(
r.obj,
filename,
row.names = TRUE,
col.names = TRUE,
xl.sheet = NULL,
top.left.cell = "A1",
na = "",
password = NULL,
write.res.password = NULL,
excel.visible = FALSE,
file.format = xl.constants$xlOpenXMLWorkbook
)
a character
a logical value indicating whether the file contains the names of the variables as its first line. If TRUE and top-left corner is empty cell, first column is considered as row names. Ignored if row.names or col.names is not NULL.
a logical value indicating whether the row names of r.obj are to be read/saved along with r.obj
a logical value indicating whether the column names of r.obj are to be read/saved along with r.obj
character. Name of Excel sheet where data is located/will be saved. By default it is NULL and data will be read/saved from/to active sheet.
character. Top-left corner of data in Excel sheet. By default is 'A1'.
character. NA representation in Excel. By default it is empty string.
character. Password for password-protected workbook.
character. Second password for editing workbook.
a logical value indicating will Excel visible during this operations. FALSE by default.
R object.
integer. Excel file format. By default it is
xl.constants$xlOpenXMLWorkbook
. You can use
xl.constants$xlOpenXMLWorkbookMacroEnabled
for workbooks with macros
(*.xlsm) or xl.constants$xlExcel12
for binary workbook (.xlsb).
xl.read.file
always returns data.frame. xl.save.file
invisibly returns NULL.
xl.read.file
reads only rectangular data set. It is highly
recommended to have all column names and ids in data set. Orphaned
rows/columns located apart from the main data will be ignored.
xl.save.file
can save all objects for which xl.write
method exists -
see examples.
xl.write
, xl.workbook.save
,
xl.workbook.open
, current.graphics
# NOT RUN { # } # NOT RUN { data(iris) xl.save.file(iris,"iris.xlsx") xl.iris = xl.read.file("iris.xlsx") all(iris == xl.iris) # Shoud be TRUE unlink("iris.xlsx") # Save to file list with different data types dists = dist(iris[,1:4]) clusters = hclust(dists,method="ward.D") iris$clusters = cutree(clusters,3) png("1.png") plot(clusters) dev.off() pl.clus = current.graphics(filename="1.png") cross = table(iris$Species,iris$clusters) png("2.png") plot(cross) dev.off() pl.cross = current.graphics(filename="2.png") output = list("Iris",pl.clus,cross,pl.cross,"Data:","",iris) xl.save.file(output,"output.xls") xl.workbook.open("output.xls") # xl.workbook.close() # close workbook # unlink("output.xls") # delete file # password-protected file data(iris) xl.save.file(iris,"iris.xlsx", password = "pass") xl.iris = xl.read.file("iris.xlsx", password = "pass") all(iris == xl.iris) # Shoud be TRUE unlink("iris.xlsx") # }
Run the code above in your browser using DataCamp Workspace