Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shiny Gadget For Emailing Cute Puppies
library(shiny)
library(miniUI)
library(gmailr)
library(rtweet)
library(purrr)
use_secret_file("~/client_id.json")
send_email <- function(to_address, picture_url, msg = "",
subject = "Cute Picture", expanded_url = picture_url){
html_msg <-
tagList(
pre(msg),
br(),
img(
src = picture_url,
style = 'max-height: 100%; max-width: 100%; margin: auto; display:block;'),
p("Image Source:", expanded_url)
)
mime_msg <- mime(to = to_address, subject = subject)
mime_msg <- html_body(mime_msg, as.character(html_msg))
send_message(mime_msg)
}
cute_browse_and_send <-
function(handle = c("CuteEmergency","CuteBabyAnimals","dog_rates"), ntweets = 10) {
tmls <- get_timelines(handle, n = ntweets)
pictures <- flatten_chr(pluck(tmls, "ext_media_url"))
pictures <- pictures[!is.na(pictures)]
expanded_urls <- flatten_chr(pluck(tmls, "ext_media_expanded_url"))
expanded_urls <- expanded_urls[!is.na(expanded_urls)]
if (length(pictures) == 0)
stop("No pictures found in first ",ntweets," tweets!\nMaybe try more?")
if (length(pictures) != length(expanded_urls))
warning("Length of picture urls and expanded urls do not match!")
ui <- miniPage(
gadgetTitleBar("Select Cute Picture"),
miniContentPanel(
uiOutput("picture")
),
miniButtonBlock(
actionButton("previouspic", "Previous Picture"),
actionButton("nextpic", "Next Picture"),
actionButton("send", "Send Picture")
)
)
server <- function(input, output, session) {
status <- reactiveValues(ix = 1,
emails = list())
observeEvent(input$nextpic, {
if (status$ix == length(pictures)) {
status$ix <- 1
} else{
status$ix <- status$ix + 1
}
})
observeEvent(input$previouspic, {
if (status$ix == 1) {
status$ix <- length(pictures)
} else{
status$ix <- status$ix - 1
}
})
observeEvent(input$send, {
showModal(modalDialog(
title = "Send Picture?",
textInput("email", "Recipient Email address"),
textInput("subject","Subject", value = "Cute Picture!"),
textAreaInput("message", "Message"),
easyClose = TRUE,
footer = tagList(
modalButton("Cancel"),
actionButton("oksend","Send")
)
))
})
observeEvent(input$oksend,{
new_email <-
send_email(
to_address = input$email,
picture_url = pictures[status$ix],
subject = input$subject,
msg = input$message,
expanded_url <- expanded_urls[status$ix])
status$emails <- append(status$emails, new_email)
removeModal()
})
output$picture <- renderUI({
tagList(
img(
src = pictures[status$ix],
style = 'max-height: 100%; max-width: 100%; margin: auto; display:block;'))
})
observeEvent(input$done, {
returnValue <- status$emails
stopApp(returnValue)
})
}
runGadget(ui, server)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment