使用updateNavbarPage为>添加“上一页”按钮2页(也适用于updateTabsetPanel)

时间:2017-08-28 23:40:20

标签: r shiny actionlink

我正在尝试在我的RShiny应用程序上实现一个“上一页”按钮,该按钮在按下时导航到上一页。我发现我可以让它工作两页,但当我有第三页并尝试导航回第二页时它不再有效。奇怪的是,对“下一步”按钮使用相同的逻辑可以正常工作。

这是一个可重复性最小的例子:

ui <- fluidPage(
                navbarPage(title = "",
                           id = "panels_main",
                           tabPanel(title = "A", #value = "intro_page",
                                    p(paste("Panel A")),
                                    fluidRow(column(width = 1, offset = 9, actionLink("link_to_tabpanel_B", "Next")))

                           ),

                           tabPanel(title = "B", #value = "intro_page",
                                    p(paste("Panel B")),
                                    fluidRow(
                                      column(width = 1, offset = 0, actionLink("link_to_tabpanel_A", "Previous")),
                                      column(width = 1, offset = 9, actionLink("link_to_tabpanel_C", "Next")))
                           ),

                           tabPanel(title = "C", #value = "intro_page",
                                    p(paste("Panel C")),
                                    fluidRow(
                                      column(width = 1, offset = 0, actionLink("link_to_tabpanel_B", "Previous")))
                           )
                )
)

server <- function(input, output, session) {

  observeEvent(input$link_to_tabpanel_A, {
    newvalue <- "A"
    updateNavbarPage(session, "panels_main", newvalue)
  }) 

  observeEvent(input$link_to_tabpanel_B, {
    newvalue <- "B"
    updateNavbarPage(session, "panels_main", newvalue)
  }) 

  observeEvent(input$link_to_tabpanel_C, {
    newvalue <- "C"
    updateNavbarPage(session, "panels_main", newvalue)
  }) 
}

shinyApp(ui = ui, server = server)

请注意,当您运行应用程序时,您可以使用下一个按钮从A导航到B导航到C,但只有B到A前一个按钮才能工作。 C到B前一个按钮不起作用。有谁知道为什么会这样,并建议如何解决它?

顺便说一句,我也尝试过使用updateTabsetPanel的类似方法并遇到同样的问题,所以我怀疑我的实现的基本问题对于这两个函数都是一样的。

1 个答案:

答案 0 :(得分:1)

两个输入元素不能具有相同的inputId。尝试将第二个链接的link_to_tabpanel_B更改为link_to_tabpanel_B1。我已经修改了你的代码来做到这一点。看看:

 library(shiny)

  ui <- fluidPage(
    navbarPage(title = "",
               id = "panels_main",
               tabPanel(title = "A", #value = "intro_page",
                        p(paste("Panel A")),
                        fluidRow(column(width = 1, offset = 9, actionLink("link_to_tabpanel_B", "Next")))

               ),

               tabPanel(title = "B", #value = "intro_page",
                        p(paste("Panel B")),
                        fluidRow(
                          column(width = 1, offset = 0, actionLink("link_to_tabpanel_A", "Previous")),
                          column(width = 1, offset = 9, actionLink("link_to_tabpanel_C", "Next")))
               ),

               tabPanel(title = "C", #value = "intro_page",
                        p(paste("Panel C")),
                        fluidRow(
                          column(width = 1, offset = 0, actionLink("link_to_tabpanel_B1", "Previous")))
               )
    )
  )

  server <- function(input, output, session) {

    observeEvent(input$link_to_tabpanel_A, {
      newvalue <- "A"
      updateNavbarPage(session, "panels_main", newvalue)
    }) 

    observeEvent(input$link_to_tabpanel_B ,{
      newvalue <- "B"
      updateNavbarPage(session, "panels_main", newvalue)
    }) 

    observeEvent( input$link_to_tabpanel_B1,{
      newvalue <- "B"
      updateNavbarPage(session, "panels_main", newvalue)
    }) 

    observeEvent(input$link_to_tabpanel_C, {
      newvalue <- "C"
      updateNavbarPage(session, "panels_main", newvalue)
    }) 
  }

  shinyApp(ui = ui, server = server)

希望它有所帮助!