Skip to content

Instantly share code, notes, and snippets.

@xiaodaigh xiaodaigh/server.R
Created Sep 22, 2013

Embed
What would you like to do?
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
You can’t perform that action at this time.