我制作了一个带有两个面板的Shiny应用程序。在第一个面板中,显示了单选按钮。选择其中一个按钮后,应用程序将移至第二个面板,并且在该面板中显示所选单选按钮的值。在第二个面板中,有一个“返回第一个面板”操作按钮。
单击此按钮时,程序将移回到第一个面板。现在,当用户单击与以前相同的单选按钮时,该程序不会像第一次那样移动到第二个面板。
考虑到Shiny机制,在所有输入(即单选按钮输入)的值都没有变化之后,我可以理解这一点。
尽管如此,我还是希望更改程序,以便在单击任何单选按钮时始终将其移至第二个面板,即使它与以前单击的单选按钮相同。
有没有办法使程序具有这种行为?
R代码如下:
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)
{
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)
答案 0 :(得分:1)
我们可以通过在按下后退按钮时使用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)