Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email from shiny

Tags:

r

shiny

I am a new Shiny user and I am interested in creating a web app where visitors can fill in some questions (depending on random R data) and they can submit them.

My problem is to find the way to send to me that information via email, for example, each time they submit the data.

I am an university lecturer and I think this is a good way to assess my students.

like image 559
user3149230 Avatar asked Dec 31 '13 12:12

user3149230


3 Answers

Here is the Shiny email sender I wrote to test the sendmailR package in a Shiny app. On a Linux platform, I have not configured anything and the app perfectly works. The user types the body of the message in a text area generated and handled by the shinyAce package.

ui.R

shinyUI(pageWithSidebar(

  headerPanel("Email sender"),

  sidebarPanel(
    textInput("from", "From:", value="[email protected]"),
    textInput("to", "To:", value="[email protected]"),
    textInput("subject", "Subject:", value=""),
    actionButton("send", "Send mail")
  ),

  mainPanel(    
    aceEditor("message", value="write message here")
  )

))

server.R

library(shinyAce)
library(sendmailR)

shinyServer(function(input, output, session) {

  observe({
    if(is.null(input$send) || input$send==0) return(NULL)
    from <- isolate(input$from)
    to <- isolate(input$to)
    subject <- isolate(input$subject)
    msg <- isolate(input$message)
    sendmail(from, to, subject, msg)
  })

})
like image 116
Stéphane Laurent Avatar answered Oct 18 '22 03:10

Stéphane Laurent


R can definitely send an e-mail. Googling for R send email leads me to the sendmailR package, which is available from CRAN. Also take a look at:

  • how to send email with attachment from R in windows
like image 34
Paul Hiemstra Avatar answered Oct 18 '22 03:10

Paul Hiemstra


This should be a good start :

library(shiny)
ui <- pageWithSidebar(
  headerPanel("fill this and send"),
  sidebarPanel(

  ),
  mainPanel(
    textInput("name", "Name:", ""),
    textInput("body", "Body:", ""),
    actionButton("goButton",label = "Send this")

  )
)


server <- function(input, output) {
  observe({
    # Take a dependency on input$goButton
    if (input$goButton == 0)
      return(NULL)
    # Use isolate() to avoid dependency on input$goButton
    isolate({
      info <- data.frame(subject=paste("New info from:",input$name),
                         body = info$body)
      InfromMe(info)
    })
  })
}
runApp(list(ui=ui,server=server))

where inforMe , is the mail function using PaulHimstra answer:

#####send plain email
InfromMe <- function(info){
  from <- "[email protected]"
  to <- "[email protected]"
  subject <- info$subject
  body <- info$body                    
  mailControl=list(smtpServer="serverinfo")
  sendmail(from=from,to=to,subject=subject,msg=body,control=mailControl)
}
like image 1
agstudy Avatar answered Oct 18 '22 04:10

agstudy