Overwrite or Append data in Excel sheet using R

Hello friends! Today we’ll be learning to overwrite or append data in Excel sheet using R

Below is the code for Overwrite the sheet and Append the excel sheet within same Excel file. You may need to add a blank sheets with the names in Excel workbook.

install.packages("readxl")
install.packages("openxlsx")
library(readxl)
library(openxlsx)
main <- function(){
#report- clear
employeeID <- "1"
fstName <- "Somia"
lstName <- "Rojas"
age <- 34L
#Audit - appending
ID_report <- 1L
dateReport <- 2020-11-17
adminUSer <- "Alex"
dfReport <- data.frame("EMPLOYEE_ID" = employeeID, "FIRST_NAME" = fstName, "LAST_NAME" = lstName,
"AGE"= age)
wb <- loadWorkbook("Report.xlsx")
writeData(wb, sheet = "report", dfReport)
saveWorkbook(wb,"Report.xlsx",overwrite = T)
dfAudit <- data.frame( "ID_REPORT"= ID_report,    "DATE_REPORT"= dateReport, 
"ADMIN_USER"= adminUSer,   "EMPLOYEE_ID" = employeeID, "FIRST_NAME" = fstName, 
"LAST_NAME" = lstName, "AGE"= age)
xlAudit <- read_excel("Report.xlsx", sheet = "Audit")
dfAudit1 <- rbind(dfAudit,xlAudit)
wb1 <- loadWorkbook("Report.xlsx")
writeData(wb1, sheet = "Audit", dfAudit1)
saveWorkbook(wb1,"Report.xlsx",overwrite = T)
# print(xlAudit)
# print(dfAudit)
# print(dfAudit1)
}
main()

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries


Leave a Reply

Your email address will not be published. Required fields are marked *