How to use a Shiny event in a jQuery function ?

854 views
Skip to first unread message

Stéphane Laurent

unread,
Feb 14, 2014, 2:53:11 PM2/14/14
to shiny-...@googlegroups.com
Hello, 

 I'm a newbie in jQuery (and html). 

 Here is one of my first tests of jQuery : http://pagist.info/9007180 (code: https://gist.github.com/stla/9007180)

The animation is activated when the user push the button #btn1:

$("#btn1").click(function(){
$("#box").animate({width:"300px"});
$("#box").animate({height:"300px"});
});

 Now, imagine for example I define a true/false reactive variable in the server.R file of a shiny app, by doing :

  output$sleep <- reactive({
    Sys.sleep(4)
    return(TRUE)
  })
  outputOptions(output, 'sleep', suspendWhenHidden=FALSE)

What  is the code I should use instead of the event $("#btn1").click  in order that the animation is activated when output.sleep is true ?


Joe Cheng

unread,
Feb 14, 2014, 7:31:49 PM2/14/14
to Stéphane Laurent, shiny-...@googlegroups.com
There's not an easy way to attach event handlers to output values changing, other than to write a custom output binding.

But in this case, it seems like what you're doing is not an output at all. I'd take a look at Shiny's custom messages feature if you want to invoke JS from the server.


--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Stéphane Laurent

unread,
Feb 15, 2014, 5:08:42 AM2/15/14
to shiny-...@googlegroups.com, Stéphane Laurent
Very nice, thank you.

Thus, for my example I can simply do :

    observe({
      Sys.sleep(2)
      js_string <- '$("#box").animate({width:"300px"});'
      session$sendCustomMessage(type='jsCode', list(value = js_string))
    })
Reply all
Reply to author
Forward
0 new messages