2017-08-28 38 views
0

我試圖在我的RShiny應用程序中實現'上一個'按鈕,該按鈕在按下時導航到上一頁。我發現我可以讓它工作兩頁,但當我有第三頁並嘗試導航回第二頁時,它不再有效。奇怪的是,對'下一步'按鈕使用相同的邏輯工作得很好。使用updateNavbarPage爲> 2頁添加'上一頁'按鈕(也適用於updateTabsetPanel)

這裏是一個最小的可重複的例子:

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) 

注意,當你運行你可以從一個導航使用Next按鈕B到C的應用程序,但只有B到以前按鈕的作品。 C到B上一個按鈕不起作用。有誰知道這是爲什麼,並有如何解決它的建議?另外,我也試過類似的方法,使用updateTabsetPanel並運行到相同的問題,所以我懷疑我的實現的基本問題是兩個函數相同。

回答

0

兩個輸入元素不能具有相同的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) 

希望它有幫助!

+0

太好了,非常感謝!這工作完美,正是我需要的。我試圖提高你的答案,但它沒有顯示,因爲我是堆棧溢出的新手。再次感謝!! –

相關問題