Necesito bloquear el acceso del usuario a otras pestañas hasta que se cumplan ciertas acciones. En este ejemplo reproducible, quiero bloquear al usuario para acceder a la Tab 2 hasta que presione el botón.
Así es como se ve la aplicación:
Aquí está el código de la aplicación:
library(shiny) ui <- shinyUI(navbarPage("", tabPanel(h1("Tab1"), value = "nav1", mainPanel( br(), h2("The user must press this button to access the other tab."), br(), shiny::actionButton('button', 'press the button') ) ), tabPanel(h1("Tab2"), value = "nav2", h3('Block access until user presses button') ) ) ) server <- shinyServer(function(input, output) { }) # Run the application shinyApp(ui = ui, server = server)Me gustaría que el usuario pueda ver que Tab2 existe, pero que no se pueda hacer clic hasta que presione el botón.
¿Algunas ideas?
conditionalPanel es una solución mejor, pero los usuarios aún pueden hacer clic en el botón de la pestaña, solo darles una página vacía.Aquí hay una solución aún mejor, usemos algunos js para deshabilitar el botón de pestaña a menos que los usuarios hagan clic en el botón de acción. Los usuarios pueden ver el botón de la pestaña, pero está gris y no se puede hacer clic en el inicio:
library(shiny) ui <- shinyUI(navbarPage( "", tabPanel( h1("Tab1"), value = "nav1", mainPanel( br(), h2("The user must press this button to access the other tab."), br(), shiny::actionButton('button', 'press the button', onclick = "$(tab).removeClass('disabled')") ) ), tabPanel( h1("Tab2"), value = "nav2", uiOutput("tab2contents") ), tags$script( ' var tab = $(\'a[data-value="nav2"]\').parent().addClass("disabled"); $(function(){ $(tab.parent()).on("click", "li.disabled", function(e) { e.preventDefault(); return false; }); }); ' ) )) server <- shinyServer(function(input, output) { }) # Run the application shinyApp(ui = ui, server = server)Utilice conditionalPanel() . ¿Condición? El botón no debe tener cero clics.
Su ejemplo ahora se convierte en:
library(shiny) ui <- shinyUI( navbarPage( title = "", tabPanel( title = h1("Tab1"), value = "nav1", mainPanel( br(), h2("The user must press this button to access the other tab."), br(), shiny::actionButton('button', 'press the button') ) ), tabPanel( h1("Tab2"), value = "nav2", # ----conditional panel here---- conditionalPanel( condition = "input.button != 0", h3('Block access until user presses button') ) ) ) ) server <- shinyServer(function(input, output) { }) # Run the application shinyApp(ui = ui, server = server)Agregando detalles a mi comentario anterior:
library(shiny) ui <- shinyUI(navbarPage("", tabPanel( h1("Tab1"), value = "nav1", mainPanel( br(), h2("The user must press this button to access the other tab."), br(), shiny::actionButton('button', 'press the button') ) ), tabPanel( h1("Tab2"), value = "nav2", uiOutput("tab2contents") ) ) ) server <- shinyServer(function(input, output) { v <- reactiveValues(tab2Active=FALSE) observeEvent(input$button, { v$tab2Active <- TRUE}) output$tab2contents <- renderUI({ if (v$tab2Active) { h3('Tab 2 is active') } else { h3('Block access until user presses button') } }) }) # Run the application shinyApp(ui = ui, server = server)