Skip to content

Instantly share code, notes, and snippets.

@xiaodaigh
Created September 22, 2013 18:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaodaigh/6662679 to your computer and use it in GitHub Desktop.
Save xiaodaigh/6662679 to your computer and use it in GitHub Desktop.
A simple Shiny spy to tell Shiny that the session has closed
library(shiny)
data(pressure)
data(cars)
shinyServer(function(input, output, session) {
# Partial example
observe({
print(input$shinyspy)
})
})
var shinyspy = new Shiny.InputBinding();
$.extend(shinyspy, {
find: function(scope) {
return $(scope).find(".shinyspy");
},
getValue: function(el) {
return($(el).data("session"))
},
setValue: function(el, value) {
},
subscribe: function(el, callback) {
$(el).on("closingshinysession.shinyspy", function(e) {
console.log("dfsdfs")
callback();
});
},
unsubscribe: function(el) {
$(el).off(".shinyspy");
}
});
Shiny.inputBindings.register(shinyspy);
library(shiny)
shinyspy <- function() {
tagList(
HTML("<shinyspy class='shinyspy' id='shinyspy' data-session='open' / >")
,tags$script("$(window).on('beforeunload',function() {
$('.shinyspy').data({session:'closed'})
$('.shinyspy').trigger('closingshinysession')
return('Shiny will get alert of this if you leave has been alerted?')
})"))
}
shinyUI(
basicPage(
# Partial example
# selectInput("dataset", "Dataset", c("diamonds", "rock", "pressure", "cars")),
# conditionalPanel(
# condition = "output.nrows",
# checkboxInput("headonly", "Only use first 1000 rows"))
# ,
shinyspy(),
includeScript("shinyspy.js")
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment