Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
How to Observe a JavaScript Button in a Shiny App

I am looking for help please to observe when a button in the navbar of a Shiny App with navbarPage is clicked.

I have included a button right in the Shiny navbar with some javascript. I have provided an id to the button tag (‘id = btn_show’) and have introduced an observeevent to listen for ‘input$btn_show’. The button isn’t being observed though… what is required please? Thanks

library(shiny)
library(shinyjs)

ui <- navbarPage(
  title = 'Button Up',
  tabPanel('Tab 1'),
  tabPanel('Tab 1')
  , tags$script(HTML(
    "var header = $('.navbar> .container-fluid');
                   header.append('<div style=\"float:right; style=\"valign:middle;\"><button id = \"btn_show\">Show Controls</button></div>');
                    console.log(header)"))
  
  
)

server <- function(input, output, session) {
  
  observeEvent(input$btn_show,{
    
    print('The button was observed')
    
  })
  
}

shinyApp(ui, server)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add the onclick attribute to your button:

<button id=\"btn_show\" onclick=\"Shiny.setInputValue(\\\"btn_show\\\", true, {priority: \\\"event\\\"})\">Show Controls</button>

I'm not sure the triple backslashes will work. If not, define a function:

function f(){
  Shiny.setInputValue(\"btn_show\", true, {priority: \"event\"})
}

Then onclick = \"f()\".

EDIT

Works like this:

  tags$script(HTML(
    "var header = $('.navbar> .container-fluid');
    var f = function(){Shiny.setInputValue(\"btn_show\", true, {priority: \"event\"})};
    header.append('<div style=\"float:right; valign:middle;\"><button id=\"btn_show\" onclick=\"f()\">Show Controls</button></div>');
    console.log(header)"))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda