在 Shiny 中单击导航栏按钮不起作用
a navbar button doesn't work on click in Shiny
我想在导航栏上放置一个电子邮件按钮。我环顾了 Whosebug 和网络,但除此之外我找不到任何可靠的东西 - Add action button on the right side of navbar page。我得到的最接近的是使用 fluidRrow
和列放置,但它在我的实际应用程序中看起来很麻烦。
目前,我将按钮放在了我想要的位置,但是它没有反应。我知道我可以在 ui.R
-
中使用它
a(actionButton(inputId = "an_email", label = "Contact",
icon = icon("envelope", lib = "font-awesome")),
href="mailto:xyz@email.us")
但不确定如何接受这个想法并走向 server.R
如果有人能帮助我,我将不胜感激。
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:xyz@email.us')"))
# Tried this approach also
#onclick("sendemail", runjs("function sendEmail() {
# var link = 'mailto:me@example.com';
# window.location.href = link;}"))
#}
runApp(shinyApp(ui= ui, server= server))
当我在 EDGE 中查看您的示例时,它实际上在工作(在 Firefox 中,弹出窗口被阻止,在 RStudio 中,它不工作)。但是,您不需要服务器部分。您可以将按钮包装在 <a href="mailto:xyz@email.us">...</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:xyz@email.us\"><button id=\"sendemail\" type=\"button\" class=\"btn btn-primary action-button\">Contact</button></a></div>')")
)
)),
server = function(input, output, session) {
# no server part needed
})
我想在导航栏上放置一个电子邮件按钮。我环顾了 Whosebug 和网络,但除此之外我找不到任何可靠的东西 - Add action button on the right side of navbar page。我得到的最接近的是使用 fluidRrow
和列放置,但它在我的实际应用程序中看起来很麻烦。
目前,我将按钮放在了我想要的位置,但是它没有反应。我知道我可以在 ui.R
-
a(actionButton(inputId = "an_email", label = "Contact",
icon = icon("envelope", lib = "font-awesome")),
href="mailto:xyz@email.us")
但不确定如何接受这个想法并走向 server.R
如果有人能帮助我,我将不胜感激。
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:xyz@email.us')"))
# Tried this approach also
#onclick("sendemail", runjs("function sendEmail() {
# var link = 'mailto:me@example.com';
# window.location.href = link;}"))
#}
runApp(shinyApp(ui= ui, server= server))
当我在 EDGE 中查看您的示例时,它实际上在工作(在 Firefox 中,弹出窗口被阻止,在 RStudio 中,它不工作)。但是,您不需要服务器部分。您可以将按钮包装在 <a href="mailto:xyz@email.us">...</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:xyz@email.us\"><button id=\"sendemail\" type=\"button\" class=\"btn btn-primary action-button\">Contact</button></a></div>')")
)
)),
server = function(input, output, session) {
# no server part needed
})