Reputation: 556
I wanted to place an email button on the navbar. I have looked around stackoverflow and on the web, however I couldn't find anything solid beside this- Add action button on the right side of navbar page. The closest I have gotten is by using the fluidRrow
and column placements, but it looked cumbersome in my actual app.
At the moment, I have button placed where I wanted, however, it doesn't react. I know that I can use this within the ui.R
-
a(actionButton(inputId = "an_email", label = "Contact",
icon = icon("envelope", lib = "font-awesome")),
href="mailto:[email protected]")
But not sure how to take this idea and go towards the server.R
I would appreciate if somebody can help me.
library(shiny)
# library(markdown)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
navbarPage("Navbar!",
tabPanel("Plot"
),
tags$script(
HTML("var header = $('.navbar > .container-fluid');
header.append('<div style=\"float:right; padding-top: 8px\"><button id=\"sendemail\" type=\"button\" class=\"btn btn-primary action-button\" onclick=\"sendEmail()\">Contact</button></div>')")
)
)
)
server <- function(input, output, session) {
# not sure if I need an obserEvent here
observeEvent(input$sendemail,{cat("contact envoked")})
onclick("sendemail", runjs("window.open('mailto:[email protected]')"))
# Tried this approach also
#onclick("sendemail", runjs("function sendEmail() {
# var link = 'mailto:[email protected]';
# window.location.href = link;}"))
#}
runApp(shinyApp(ui= ui, server= server))
Upvotes: 0
Views: 486
Reputation: 18561
When I view your example in EDGE it is actually working (in Firefox the popup is blocked and in RStudio it is not working). However, you don't need the server part. You can just wrap the button in <a href="mailto:[email protected]">...</a>
.
library(shiny)
library(shinyjs)
shinyApp(ui = fluidPage(
useShinyjs(),
navbarPage("Navbar!",
tabPanel("Plot"),
tags$script(
HTML("var header = $('.navbar > .container-fluid');
header.append('<div style=\"float:right; padding-top: 8px\"><a href=\"mailto:[email protected]\"><button id=\"sendemail\" type=\"button\" class=\"btn btn-primary action-button\">Contact</button></a></div>')")
)
)),
server = function(input, output, session) {
# no server part needed
})
Upvotes: 2