-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use updateTabsetPanel to go to a tabPanel created with renderUI #772
Comments
I think the issue here is one of timing - the Edit: it turns out that we had discussed this for |
Thanks for the update @wch. Just to be clear, I'm hoping to use A related approach is mentioned here. @jcheng5 commented on that approach here. If there are other alternatives or approaches in the works please let me know |
I'm in a hurry so pardon if I'm missing something.
|
@jcheng5 I'd like to create links that can point to any (sub) tab. I expect that hard-coding sub2 in the example using I'm thinking of something like |
I didn't mean selected should be hard coded--you can figure out the desired On Mon, Jun 1, 2015 at 6:01 PM Vincent Nijs notifications@github.com
|
Interesting idea @jcheng5 (cc @wch). I hacked something up that seems to work (see below). If the url query is A great enhancement would be to change the url as the user is browsing. I assume that would require something like the library(shiny)
url1 <- url2 <- ""
runApp(list(
ui = shinyUI(
navbarPage( "test", id = 'someID',
tabPanel("tab1", h1("page1")),
navbarMenu( "menu",
tabPanel('tab2a', value='foo', h1("page2a")),
tabPanel('tab2b', value='bar', h1("page2b")),
tabPanel('tab_sub', uiOutput("sub_tabPanel"))
)
)),
server = function(input, output, session) {
observe(print(input$someID))
output$sub_tabPanel <- renderUI({
tabsetPanel(
tabPanel("Summary", h1("sub1")),
tabPanel("Plot", h2("sub2")),
selected = ifelse(url2 == "", "Summary", url2)
)
})
observe({
# suppose url is http://127.0.0.1:5682/?url=tab_sub/Plot
query <- parseQueryString(session$clientData$url_search)
if(!is.null(query$url)) {
url <- strsplit(query$url,"/")[[1]]
url1 <<- url[1]
url2 <<- url[2]
updateTabsetPanel(session, 'someID', url1)
}
})
}
)) Shiny.addCustomMessageHandler('setURL',
function(data) {
// make each key and value URL safe (replacing spaces, etc.), then join
// them and put them in the URL
var search_terms = [];
for (var key in data) {
search_terms.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
}
window.history.pushState('object or string', 'Title', '/?' + search_terms.join('&'));
}
); |
@vnijs I was able to come up with a (non-pretty) solution for navigating to any tab, no matter how deep it is. The basic idea is that I keep track of how deep I navigated so far and I look at the url, and until I get to the same depth, I update the next tabpanel in the list. Because they don't update right away and you can't update a tabpanel that's not visible, there's some logic that makes it ignore until the tab was properly switched. For this idea to work, you need to follow a certain naming scheme for the tabs: if you have a tabpanel inside a tab with id/value of "sometab", then the id of the tabpanel must be "sometab_tabs". It's a bit restrictive but it works I think. I didn't deal with the other issue of saving the current tab history, that can certainly be solved as well, but this seemed like a fun problem and I wanted to try it. It is a bit ugly so maybe you won't use it, but here's my attempt :)
|
That was really useful @daattali. Thanks. Based on your setup I created the solution below. Observers are created (and disabled) as needed. @jcheng5 @wch Do you see any problems with this approach? I'm now working on putting information about the active tab back into the url. I expect this will require reversing the order of the key:value pairs in the url_patterns list below (see SO question) and session$sendCustomMessage. url_patterns <- list(
"base/single-mean/" = list("nav_radiant" = "Single mean", "tabs_single_mean" = "Summary"),
"base/single-mean/plot/" = list("nav_radiant" = "Single mean", "tabs_single_mean" = "Plot"),
"sample/sampling/" = list("nav_radiant" = "Sampling"),
"sample/sample-size/" = list("nav_radiant" = "Sample size")
)
observe({
url_query <- parseQueryString(session$clientData$url_search)
if (!"url" %in% names(url_query)) return()
## create an observer and suspend when done
url_observe <- observe({
url <- url_patterns[[url_query$url]]
if (is.null(url)) {
## if pattern not found suspend observer
url_observe$suspend()
return()
}
for(u in names(url)) {
if(is.null(input[[u]])) return()
if(input[[u]] != url[[u]])
updateTabsetPanel(session, u, selected = url[[u]])
if(names(tail(url,1)) == u) url_observe$suspend()
}
})
}) |
I'm sorry, I don't have the bandwidth to review this right now... I'm busy trying to close down leaflet and d3heatmap in preparation for useR. If you still need a review in the second week of July, ping me then :) |
What I have seems to working ok so I'll close this. |
I'm facing a similar problem. I can update the url using the java script snippet above, but unfortunately the observe-part of session$clientData$url_search is not executed. I have a navbar which initially containing a single navbarMenu with several (sub)entries. Whenever I chose one of the menu-entries I'd like to create a (new) tabPanel (next to the initial navbarMenu) which is focused. I think all the above code should make this possible, but I can't figure it out. Will this be possible? Do you have some hints / minimal examples? |
I don't have a minimal example. The relevant code is here: https://github.com/vnijs/radiant/blob/master/inst/base/init.R#L169-L316 |
Can you use updateTabsetPanel to navigate to tab sub2?
The text was updated successfully, but these errors were encountered: