Skip to content

Instantly share code, notes, and snippets.

@kellobri
Created November 26, 2018 19:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellobri/f78e29d0cbcf640fd8dbb9d8f7624c22 to your computer and use it in GitHub Desktop.
Save kellobri/f78e29d0cbcf640fd8dbb9d8f7624c22 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)
```
@kellobri
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment