Мы можем сделать это, нажав кнопку обновления на кнопке updateRadioButtons
, когда нажата кнопка «Назад».
observe({
if (input$goBack>0)
{
updateRadioButtons(session,inputId = "entries", label=NULL,
choiceNames = c("A", "B", "C", "D"), choiceValues = c(1,2,3,4), selected = 0)
shinyjs::hide(selector = '#navBar li a[data-value="Panel2"]')
shinyjs::show(selector = '#navBar li a[data-value="Panel1"]')
updateNavbarPage(session, "navBar", selected="Panel1")
}
})
Полный код:
library(shiny)
ui <- tagList(
navbarPage(
title=NULL, id = "navBar",
tabPanel(title = "Panel1", uiOutput('showRadioButtons')),
tabPanel(title = "Panel2", uiOutput('showChoice'), hr(), actionButton("goBack", "Back to first panel"))
)
)
server <- function(input, output, session)
{
observe({
shinyjs::hide(selector = '#navBar li a[data-value="Panel2"]')
})
output$showRadioButtons <- renderUI({
radioButtons(inputId = "entries", label = NULL, choiceNames = c("A", "B", "C", "D"), choiceValues = c(1,2,3,4), selected = 0)
})
output$showChoice <- renderUI({
if (length(input$entries)>0)
return(input$entries) else return(NULL)
})
observe({
if (length(input$entries)>0)
{
shinyjs::hide(selector = '#navBar li a[data-value="Panel1"]')
shinyjs::show(selector = '#navBar li a[data-value="Panel2"]')
updateNavbarPage(session, "navBar", selected="Panel2")
}
})
observe({
if (input$goBack>0)
{
updateRadioButtons(session,inputId = "entries", label=NULL,
choiceNames = c("A", "B", "C", "D"), choiceValues = c(1,2,3,4), selected = 0)
shinyjs::hide(selector = '#navBar li a[data-value="Panel2"]')
shinyjs::show(selector = '#navBar li a[data-value="Panel1"]')
updateNavbarPage(session, "navBar", selected="Panel1")
}
})
}
shinyApp(ui = ui, server = server)