Last active
August 29, 2015 14:01
-
-
Save reinholdsson/2f34d983383d4f1eb4f2 to your computer and use it in GitHub Desktop.
Send mail through R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(knitr) | |
library(mailR) | |
library(XLConnect) | |
rmd_to_html <- function(input, output = paste0(tempfile(), ".html")) { | |
knit2html(input = input, output = output, quiet = T,options="") | |
return(output) | |
} | |
df_to_csv <- function(input, output = paste0(tempfile(), ".csv")) { | |
write.csv2(x = input, file = output, row.names = F) | |
return(output) | |
} | |
df_to_xlsx <- function(input, output = paste0(tempfile(), ".xlsx")) { | |
wb <- loadWorkbook(output, create = T) | |
createSheet(wb, name = "output") | |
writeWorksheet(wb, input, sheet = "output") | |
saveWorkbook(wb) | |
return(output) | |
} | |
email <- send.mail( | |
from = "[email protected]", | |
to = "[email protected]", | |
subject = "Mailutskick från R", | |
html = T, | |
inline = T, | |
body = rmd_to_html("report.Rmd"), | |
smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = "USERNAME", passwd = "PASSWORD", ssl = T), | |
authenticate = T, | |
send = F, | |
attach.files = c(df_to_csv(cars), df_to_xlsx(mtcars)), | |
file.names = c("cars.csv", "mtcars.xlsx"), | |
file.descriptions = c("cars data frame in csv", "mtcars data frame in xlsx") | |
) | |
# email$send() # send mail! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mailutskick från R | |
Detta är ett exempel på **mailutskick** från R. | |
Mailet är skapat utifrån ett markdown-template, se följande länk för mer information: [http://www.rstudio.com/ide/docs/authoring/using_markdown](http://www.rstudio.com/ide/docs/authoring/using_markdown). | |
## Exempel på rapport | |
Det innebär att man enkelt kan kombinera kod med text, nedan är ett exempel där jag hämtar data från MonetDB på `localhost`: | |
```{r, message=F, tidy=F} | |
library(dplyr) | |
db <- src_monetdb(host = "localhost", dbname = "voc", user = "monetdb", password = "monetdb") | |
iris <- tbl(db, "iris") | |
iris %.% | |
group_by(species) %.% | |
summarize( | |
petal_length = min(petal_length), | |
sepal_width = max(sepal_width), | |
count = n() | |
) %.% | |
collect() | |
``` | |
Det går också bra att skicka vanliga sql-anrop: | |
```{r, tidy=F} | |
query <- "select * from iris where species = 'virginica'" | |
x <- collect(tbl(db, sql(query))) | |
head(x) | |
``` | |
Samma resultat fast presenterad i en html-formaterad tabell: | |
```{r, echo=F,results='asis'} | |
library(xtable) | |
print(xtable(head(x)), type = "html", include.rownames = F) | |
``` | |
Nedan är ett exempel på en plot som inkluderas i mailet. | |
```{r fig.width=7.5, fig.height=4, tidy=F} | |
library(ggplot2) | |
data <- collect(iris) | |
qplot(sepal_length, petal_length, data = data, color = species) | |
``` | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment