Connect Excel With R


# Connecting Excel with R

# XLConnect is package in R that is used create Spreadsheet.
# It is dynamic report of R analysis.
# install the package XLConnect and subsequently the library

>install.packages("XLConnect")
>library(XLConnect)


CREATE A WORKBOOK


# Lets first create the path where we would like to store the excel file and name it as fileXls.
# unlink command is to delete the files and directories Specified by x, i.e x=fileXls.
# Then command loadWorkbook is used creates an R workbook object.
# createSheet command is used to create a sheet named "Input".
# saveWorkbook command save the R object in an xlsx file.

>fileXls=paste("C:\\Users\\Desktop\\Statistics","newfile.xlsx",sep="\\")
>unlink(fileXls,recursive = FALSE,force = FALSE)
>exc=loadWorkbook(fileXls,create = TRUE)
>createSheet(exc,"Input")
>saveWorkbook(exc)


FILL DATA WITHIN THE FILE


# data.frame is used to create rows and columns with values.
# writeWorksheet writes the content of this data in the input sheet from the cell(1,2)
# saveWorkbook will save the content.

>input<-data.frame("Subjects"=c("Maths","Physics","Statistics"),"Marks"=c(29,35,40))
>writeWorksheet(exc,input,sheet = "input",startRow = 1,startCol = 2)
>saveWorkbook(exc)