Here I will show you simple examples of exporting your reports using:
- google sheets (googlesheets)
- excel files (openxlsx)
- email (mailR)
Export with google sheets
You will need:
- Install googlesheets package
- Service account key (how to create it you can find here)
Do not to forget enable google sheets api in google cloud console.
About options(httr_oob_default = TRUE) you can read here.
Example
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/spreadsheets")
options(httr_oob_default = TRUE)
library(googlesheets) # library for working with google sheets
library(googleAuthR) # library for auth with google api
gar_auth_service(json_file="/srv/shiny-server/intelis/yd/d.json") # add serviceKey
all_sheets <- gs_ls() # all sheets
iris_ss <- gs_new("iris", input = head(iris, 3), trim = TRUE) # create new sheet from an R object
Export with email
I like this method, often use this method on projects.
You will need:
- Install mailR package
- Email on which reports will be sent (you can create something like [email protected])
Yup, enable api.
If you have problems turn on secure app access.
Example
library(mailR)
send.mail(from = "from email",
to = c("to email"),
subject = "Test",
body = paste0("Test"),
encoding = "utf-8",
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "you email", passwd = "password", ssl = TRUE), # example for gmail
authenticate = TRUE,
send = TRUE,
#attach.files = "", # you can add file
debug = TRUE)
Excel file
You will need:
- Install openxlsx
library(openxlsx)
wb <- createWorkbook("report") # create workbook
addWorksheet(wb, "total") # add worksheet
hs <- createStyle(fgFill = "forestgreen", halign = "CENTER", textDecoration = "bold",
border = "TopBottomLeftRight", fontName = "Calibri", fontSize = 11) # create head style
writeData(wb, sheet = "total", "you data here", startCol = 1, startRow = 1, headerStyle = hs, borders = "all") # write data to sheet
saveWorkbook(wb, paste0("report_", Sys.Date(), ".xlsx"), overwrite = TRUE) # save workbook