Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
Shiny Javascript Events no funciona con el selector jQuery $(#id)

En la documentación de RStudio sobre eventos de javascripts en shiny https://shiny.rstudio.com/articles/js-events.html , hay este ejemplo para el evento shiny:value :

 $('#foo').on('shiny:value', function(event) { // append a character string to the output value event.value += ' Oh that is nice!'; }); // use event.target to obtain the output element $(document).on('shiny:value', function(event) { // cancel the output of the element with id 'foo' if (event.target.id === 'foo') { event.preventDefault(); } });

y dice:

Dado que estos eventos se activan específicamente en un elemento de salida, puede agregar el oyente en el elemento de salida en lugar de en el documento, aunque este último también funciona, por ejemplo.

pero cuando trato de usar el primer método en mi aplicación brillante, parece que no funciona.

Por ejemplo, si ejecuta la aplicación a continuación, solo funciona el método con el selector $(document) y activa un mensaje.

 library(shiny) ui <- fluidPage( tags$head( tags$script( "$('#my_table').on('shiny:value', function(event) { alert('MSG 1'); });" ), tags$script( "$(document).on('shiny:value', function(event) { if (event.target.id === 'my_table') { alert('MSG 2'); } });" ) ), sliderInput("my_slider", "Rows", 1, 20, 1), tableOutput("my_table") ) server <- function(input, output, session) { output$my_table <- renderTable(iris[1:input$my_slider, ]) } shinyApp(ui, server)

Supongo que me falta algo como en la documentación, dice que el método 2 debería funcionar. Cualquier ayuda apreciada, gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Eso es porque la tabla aún no existe (no está en el DOM) cuando se lee el script. Tu puedes hacer:

 $(document).ready(function(){ $('#my_table').on('shiny:value', function(event) { alert('MSG 1'); }); });

Otra forma en que debería funcionar es colocar el script al final de la interfaz de usuario, sin tags$head .

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!