My question is similar to Shiny how to block the user from accessing a tab?
But what I need here is: -Block other tabs unless the user clicks on a submit button -Automatically move to the other tab once that submit button is clicked. Here is an example code
JavaScript
x
39
39
1
library(shiny)
2
3
ui <- shinyUI(navbarPage(
4
"",
5
tabPanel(
6
h1("Tab1"),
7
value = "nav1",
8
mainPanel(
9
br(),
10
h2("The user must press this button to access the other tab."),
11
br(),
12
shiny::actionButton('button', 'press the button', onclick = "$(tab).removeClass('disabled')")
13
)
14
),
15
tabPanel(
16
h1("Tab2"),
17
value = "nav2",
18
uiOutput("tab2contents")
19
),
20
tags$script(
21
'
22
var tab = $('a[data-value="nav2"]').parent().addClass("disabled");
23
$(function(){
24
$(tab.parent()).on("click", "li.disabled", function(e) {
25
e.preventDefault();
26
return false;
27
});
28
});
29
'
30
)
31
))
32
33
server <- shinyServer(function(input, output) {
34
35
})
36
37
# Run the application
38
shinyApp(ui = ui, server = server)
39
In the above code, the 2nd tab is blocked unless the button is clicked, but how can I update this further so that once the button is clicked the user is automatically taken to the 2nd tab?
Advertisement
Answer
Since you put the tabPanel
in a navbarPage
you have to use updateNavbarPage
. And you need to give the navbarPage
an id
.
JavaScript
1
42
42
1
library(shiny)
2
3
ui <- shinyUI(navbarPage(
4
"", id = "mainpage",
5
tabPanel(
6
h1("Tab1"),
7
value = "nav1",
8
mainPanel(
9
br(),
10
h2("The user must press this button to access the other tab."),
11
br(),
12
shiny::actionButton('button', 'press the button', onclick = "$(tab).removeClass('disabled')")
13
)
14
),
15
tabPanel(
16
h1("Tab2"),
17
value = "nav2",
18
uiOutput("tab2contents")
19
),
20
tags$script(
21
'
22
var tab = $('a[data-value="nav2"]').parent().addClass("disabled");
23
$(function(){
24
$(tab.parent()).on("click", "li.disabled", function(e) {
25
e.preventDefault();
26
return false;
27
});
28
});
29
'
30
)
31
))
32
33
server <- shinyServer(function(input, output) {
34
observeEvent(input$button,
35
{
36
updateNavbarPage(inputId = "mainpage", selected = "nav2")
37
}, ignoreInit = TRUE)
38
})
39
40
# Run the application
41
shinyApp(ui = ui, server = server)
42