Carregant...
 

Correus-e via R en GenCat amb Blastula

Trobada analistes de dades de la Gencat a l'ACA (C/ Provença 260, BCN). Dll. 29/05/2023, 16:00-17:30h.

Xavier de Pedro Puente, Ph.D. CEB - xavierdepedro.ceb@gencat.cat


Start Slideshow Presentation

Trobada analistes de dades

La idea és desvirtualitzar-nos, conèixer-nos, compartir informació... i després qui vulgui ens podem allargar i anar a prendre quelcom.
Accés obert a partir de les 15h15. Entrant per la porta del carrer (Provença 260), just la primera sala a l’esquerra.


1. Cas d'ús

Enviar correus des de la xarxa informàtica de la Generalitat de Catalunya (servidors amb outlook i office365), via scripting quan es compleixen unes condicions del negoci basades en dades.

Image

1.1. Exemple: posada a punt

Codi a Gitlab GenCat:
https://git.intranet.gencat.cat/ceb/informes-en-r

Cal escollir un sistema d'enviarment de correus, entre les opcions:

  • create_smtp_creds_key()
  • create_smtp_creds_file()
    amb els paràmetres:
    create_smtp_creds_*( id, user = NULL, provider = NULL, host = NULL, port = NULL, use_ssl = NULL, overwrite = FALSE )

1.2. Crea clau smtp en configuració de sistema via nom proveïdor

# Creo la clau d'enviament de correus (i en windows) una vegada només create_smtp_creds_key( id="foo_ceb", user = "foo@gencat.cat", provider = "office365", overwrite = FALSE ) # The system key store has been updated with the "blastula-v1-foo_ceb" key with the `id` value "foo_ceb". # * Use the `view_credential_keys()` function to see all available keys # * You can use this key within `smtp_send()` with `credentials = creds_key("foo_ceb")`

1.3. Preparar objectes: gràfics ggplot2

p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, size=cyl)) + geom_point() + theme(legend.position="none") p1 <- ggExtra::ggMarginal(p, type="histogram") my_ggplot <- blastula::add_ggplot( p1 , width=5)

1.4. Preparar objectes: imatges

my_image <- blastula::add_image(file = system.file("img", "Rlogo.png", package="png"), width = 200)

1.5. Preparar objectes: taules

my_table_gt <- starwars %>% head() %>% gt() %>% data_color( columns = birth_year, colors = scales::col_numeric(palette = "Blues", domain = range(0,800)) ) %>% as_raw_html()


1.6. Composar el missatge - opció 1: compose_email()

Opció 1: compose_email(body=..., footer=...)

correu_alerta1 <- blastula::compose_email( body = md(glue::glue( "Benvolgut/da, acció requerida!: Gràfic: {my_ggplot} Imatge: {my_image} Taula heatmap: {my_table_gt} ")), footer = md(glue::glue("Correu enviat el {Sys.time()}.")) )

1.7. Composar el missatge - opció 2: render_email()

Opció 2: render_email(input=".Rmd")

correu_alerta2 <- blastula::render_email(input = "Plantilla 2.Rmd")

1.8. Afegir adjunts (pdf, htmlwidgets, ...)

correu_alerta1 <- correu_alerta1 %>% add_attachment(file = system.file('img', 'pexels-photo-267151.jpeg',package='blastula') ) %>% # Imatge via adjunt add_attachment(file = system.file('examples', 'austin_home_sales.csv',package='blastula') ) %>% # CSV add_attachment(file = system.file('doc', 'DT.html',package='DT') ) %>%# html autocontingut add_attachment(file = system.file('legacy', 'examples', 'choropleth', 'Rplots.pdf',package='leaflet') ) # pdf

1.9. Enviar el correu

# L'adreça "from" ha de ser real i existir al correu de gencat.cat, i ha de coincidir amb l'usuari que ha creat el seu "creds_key()" amb les seves credencials correu_alerta1 |> blastula::smtp_send( to = "direccio@lameva.aapp.cat", cc = "jo@lameva.aapp.cat", from = "automatitzacions@lameva.aapp.cat", subject = "Reviseu Canvis - Informe setmanal", credentials = creds_key("foo_ceb") )

2. Resultats

Image

Exemple d'arxiu de codi R Markdown amb tot junt:

Image Blastula GenCat.Rmd (Link)

3. Alternatives

Paquet d'R vius
Paquet Versió Títol Descripció Last release (at 2023-02) License URL
blastula 0.3.3 Easily Send HTML Email Messages Compose and send out responsive HTML email messages that render perfectly across a range of email clients and device sizes. Helper functions let the user insert embedded images, web link buttons, and 'ggplot2' plot objects into the message body. Messages can be sent through an 'SMTP' server, through the 'RStudio Connect' service, or through the 'Mailgun' API service . 2023-01-07 MIT + file LICENSE url
cleanrmd 0.1.0 Clean Class-Less 'R Markdown' HTML Documents A collection of clean 'R Markdown' HTML document templates using classy-looking classless CSS styles. These documents use a minimal set of dependencies but still look great, making them suitable for use a package vignettes or for sharing results via email. 2022-06-14 MIT + file LICENSE url
emayili 0.7.13 Send Email Messages A light, simple tool for sending emails with minimal dependencies. 2022-11-27 GPL-3 url
juicyjuice 0.1.0 Inline CSS Properties into HTML Tags Using 'juice' There are occasions where you need a piece of HTML with integrated styles. A prime example of this is HTML email. This transformation involves moving the CSS and associated formatting instructions from the style block in the head of your document into the body of the HTML. Many prominent email clients require integrated styles in HTML email; otherwise a received HTML email will be displayed without any styling. This package will quickly and precisely perform these CSS transformations when given HTML text and it does so by using the JavaScript 'juice' library. 2022-11-10 MIT + file LICENSE url
mailmerge 0.2.5 Mail Merge Using R Markdown Documents and 'gmailr' Perform a mail merge (mass email) using the message defined in markdown, the recipients in a 'csv' file, and gmail as the mailing engine. With this package you can parse markdown documents as the body of email, and the 'yaml' header to specify the subject line of the email. Any '{}' braces in the email will be encoded with 'glue::glue()'. You can preview the email in the RStudio viewer pane, and send (draft) email using 'gmailr'. 2022-08-14 MIT + file LICENSE url
mailR 0.8 A Utility to Send Emails from R Interface to Apache Commons Email to send emails from R. 2021-12-03 GPL-3 url
Microsoft365R 2.3.4 Interface to the 'Microsoft 365' Suite of Cloud Services An interface to the 'Microsoft 365' (formerly known as 'Office 365') suite of cloud services, building on the framework supplied by the 'AzureGraph' package. Enables access from R to data stored in 'Teams', 'SharePoint Online' and 'OneDrive', including the ability to list drive folder contents, upload and download files, send messages, and retrieve data lists. Also provides a full-featured 'Outlook' email client, with the ability to send emails and manage emails and mail folders. 2022-03-16 MIT + file LICENSE url
sendgridr 0.6.1 Mail Sender Using 'Sendgrid' Service Send email using 'Sendgrid' mail API(v3) . The goal of sendgridr is to mail using r with sendgrid service that provides free 100 mail per day forever. 2022-11-06 MIT + file LICENSE url
sendmailR 1.4-0 Send Email Using R Package contains a simple SMTP client with minimal dependencies which provides a portable solution for sending email, including file attachments and inline html reports, from within R. SMTP Authentication and SSL/STARTTLS is implemented using curl. 2023-01-12 GPL-2 url


4. Gràcies

Xavier de Pedro Puente, Ph.D.

xavierdepedro.ceb@gencat.cat

Aquestes diapositives són disponibles a:
https://seeds4c.org/BlastulaGencat

Image






Unless elsewhere noted, contents of this web site are released under a Creative Commons license.



Image Llavor: nom \ˈllavor\ : el principi d'alguna cosa que continua desenvolupant-se o creixent

Llavors de coneixement

Canvia l'Idioma