Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use actionButton to send email in RShiny

I'm working on a web app in Shiny. I'm pretty familiar with R but my knowledge of HTML, CSS, jQuery, etc is lacking which makes formatting things nicely challenging.

I want to include an actionButton that the user can click on to send an email to the administrator in case of questions or concerns. Here's what I have so far, which kind of works but is pretty heinous:

library(shiny)
ui <- shinyUI(fluidPage(
  # Set layout/format for app
  sidebarLayout(
    sidebarPanel(
      downloadButton("download_data", "Download this Data"),
      br(),
      actionButton(inputId = "email1", 
                   icon = icon("envelope", lib = "font-awesome"), 
                   a("Contact Admin", 
                       href="mailto:my_awesome_email_address.com"))
    ),
    mainPanel(),
    position = "left"
  )
)
)

server <- shinyServer(function(input, output) {})
shinyApp(ui, server)

If possible, I'd like to keep using the actionButton or something similar in order to keep the same button format across both buttons.

Thanks in advance!

like image 848
kfurlong Avatar asked Jul 28 '17 15:07

kfurlong


People also ask

How do you make an action button shiny in R?

Create an action button with actionButton() and an action link with actionLink() . Each of these functions takes two arguments: inputId - the ID of the button or link. label - the label to display in the button or link.

Can a shiny app send email?

package for sending emails from R works well within a Shiny app. You just need to set it up right.


1 Answers

Okay, well this is annoying. Hate to be one of those people that answers his own question, but here goes:

I toyed around with the ordering of the different tags in my code and realized that my current code was wrong because I was only stating that I wanted the text of the button to link to my email address and not the button itself. To fix this, I flipped the nesting of the actionButton and the a() tag to get the following:

a(actionButton(inputId = "email1", label = "Contact Admin", 
                 icon = icon("envelope", lib = "font-awesome")),
    href="mailto:my_awesome_email_address.com")

Clicking anywhere on the button automatically opens up an email addressed to the given email address after the mailto argument. No extra HTML or CSS coding required. Hope this helps someone else so they can spend their time doing something more valuable than scouring the internet for hours on end.

like image 132
kfurlong Avatar answered Sep 27 '22 20:09

kfurlong