Created
September 22, 2013 18:49
-
-
Save xiaodaigh/6662679 to your computer and use it in GitHub Desktop.
A simple Shiny spy to tell Shiny that the session has closed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
data(pressure) | |
data(cars) | |
shinyServer(function(input, output, session) { | |
# Partial example | |
observe({ | |
print(input$shinyspy) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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