0

I'm trying to pass param through pipes and then send the data thru email. My code crash when I click send mail button. I'm not sure how to solve this question as I relatively new in R language.

library(shiny)
library(gmailr)
suppressPackageStartupMessages(library(gmailr))
library(shinyAce)

gm_auth_configure(path = "credentials.json")

ui <- fluidPage(
  pageWithSidebar(
    
    headerPanel("Email sender"),
    
    sidebarPanel(
      textInput("from", "From:", value="from@gmail.com"),
      textInput("to", "To:", value="to@gmail.com"),
      textInput("subject", "Subject:", value=""),
      actionButton("send", "Send mail")
    ),
    
    mainPanel(    
      textInput(inputId =  "message", value="", label=""),
      )))

server <- function(input, output, session) {
  
  from <- reactive({input$from})
  to <- reactive({input$to})
  subject <- reactive({input$subject})
  message <- reactive({input$message})
  
  mail <- gm_mime() %>%
    gm_from(from) %>%
    gm_to(to) %>%
    gm_subject(subject) %>%
    gm_text_body(message)
    
  observeEvent(input$send, {
    gm_send_message(mail)
  })
}

shinyApp(ui = ui, server = server)

Here the warning error I got. Thanks in advance.

Warning: Error in as.vector: cannot coerce type 'closure' to vector of type 'character'
  87: as.character.default
  85: gregexpr
  84: substitute_regex
  83: quoted_printable_encode
  82: as.character.mime
  80: lapply
  75: as.character.mime
  73: gm_send_message
  72: observeEventHandler [C:/Users/Desktop/cred/sendemails.R#42]
   1: runApp
stefan
  • 90,330
  • 6
  • 25
  • 51
Afiq
  • 17
  • 4
  • Try with `from()`, `to()`, ... a reactive behaves like a function, hence you have to call it like a function. – stefan Dec 29 '20 at 09:48
  • Error in from() <- reactive({ : invalid (NULL) left side of assignment – Afiq Dec 29 '20 at 10:19
  • Sorry. My bad. `from <- reactive(..` is fine. When you want to get the value of `from` you have to use `from()`, e.g. `gm_from(from())` – stefan Dec 29 '20 at 10:25
  • ... just realized another issue. `reactive`s can only be used inside reactive expressions, i.e. you have to make `mail` a reactive too. – stefan Dec 29 '20 at 10:29

1 Answers1

0

Does this work?

ui <- fluidPage(pageWithSidebar(
  headerPanel("Email sender"),
  
  sidebarPanel(
    textInput("from", "From:", value = "from@gmail.com"),
    textInput("to", "To:", value = "to@gmail.com"),
    textInput("subject", "Subject:", value = ""),
    actionButton("send", "Send mail")
  ),
  
  mainPanel(textInput(
    inputId =  "message",
    value = "",
    label = ""
  ),)
))

server <- function(input, output, session) {
  from <- reactive({
    input$from
  })
  to <- reactive({
    input$to
  })
  subject <- reactive({
    input$subject
  })
  message <- reactive({
    input$message
  })
  
  mail <- reactive({
    gm_mime() %>%
      gm_from(from()) %>%
      gm_to(to()) %>%
      gm_subject(subject()) %>%
      gm_text_body(message())
  })
  observeEvent(input$send, {
    gm_send_message(mail())
  })
}

shinyApp(ui = ui, server = server)
Steffen
  • 206
  • 1
  • 7
  • Thanks God, you really save me. I spend almost half day to configure this part. Thank you. – Afiq Dec 29 '20 at 14:10