Created
August 14, 2014 14:13
-
-
Save mplourde/5d04503cd4e5142bc420 to your computer and use it in GitHub Desktop.
controlling shiny app page with URL variables
This file contains hidden or 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
ui <- navbarPage('TEST', id='page', collapsable=TRUE, inverse=FALSE, | |
# define a message handler that will receive the variables on the client side | |
# from the server and update the page accordingly. | |
tags$head(tags$script(" | |
Shiny.addCustomMessageHandler('updateSelections', | |
function(data) { | |
var nav_ref = '#page a:contains(\"' + data.nav + '\")'; | |
var tabpanel_id = data.nav == 'Alpha' ? '#alpha_tabs' : '#beta_tabs'; | |
var tab_ref = tabpanel_id + ' a:contains(\"' + data.tab + '\")'; | |
$(nav_ref).tab('show'); | |
$(tab_ref).tab('show'); | |
} | |
) | |
")), | |
tabPanel('Alpha', | |
tabsetPanel(id='alpha_tabs', | |
tabPanel('Tab') | |
) | |
), | |
tabPanel('Beta', | |
tabsetPanel(id='beta_tabs', | |
tabPanel('Golf'), | |
tabPanel('Hotel', | |
selectInput("beverage", "Choose a beverage:", choices = c("Tea", "Coffee", "Cocoa")) | |
) | |
) | |
) | |
) | |
server <- function(input, output, session) { | |
observe({ | |
data <- parseQueryString(session$clientData$url_search) | |
session$sendCustomMessage(type='updateSelections', data) | |
updateSelectInput(session, 'beverage', selected=data$beverage) | |
}) | |
} | |
runApp(list(ui=ui, server=server), port=5678, launch.browser=FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment