Skip to content

Instantly share code, notes, and snippets.

@Andrei-WongE
Forked from kellobri/basic-email.Rmd
Created January 7, 2019 15:03
Show Gist options
  • Save Andrei-WongE/7077e12899e89f5f103e1ec4b870341e to your computer and use it in GitHub Desktop.
Save Andrei-WongE/7077e12899e89f5f103e1ec4b870341e to your computer and use it in GitHub Desktop.
Basic Email Report on RStudio Connect
---
title: "Email Report and CSV File"
output: html_document
rmd_output_metadata:
rsc_email_subject: "CSV Data Report"
rsc_email_attachments:
- "df.csv"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
- Create data
```{r}
df <- data.frame(a=rnorm(10), b=rnorm(10))
```
- Write data (CSV file)
```{r}
write.csv(df, "df.csv", row.names=FALSE)
```
- Plot data
```{r}
library(ggplot2)
# Create a plot.
df_plot <-
ggplot(data = df,
aes(
x = a,
y = b
)) +
geom_point()
```
- Save Plot (PNG Image file)
```{r}
library(htmltools)
ggplot2::ggsave(
"plot.png",
plot = df_plot,
device = "png",
width = 5,
height = 5,
dpi = "screen"
)
# Encode the PNG image as base64.
plot_base64 <- base64enc::base64encode("plot.png")
```
- Create Email
```{r}
library(glue)
report_name <- Sys.getenv("RSC_REPORT_NAME")
report_url <- Sys.getenv("RSC_REPORT_URL")
message <- glue(paste(h1("data plot"),
# Use the filename "plot.png" as the Content ID by using "cid:"
# in the image tag's "src" attribute:
p(img(src = "cid:plot.png")),
'--',
p('This {report_name} document is available at {report_url}'),
sep = "\n"))
images <- list(plot.png = plot_base64)
rmarkdown::output_metadata$set(rsc_email_body_html = message)
rmarkdown::output_metadata$set(rsc_email_images = images)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment