使用 updateNavbarPage 为 > 2 个页面添加 'Previous' 按钮(也适用于 updateTabsetPanel)
Use updateNavbarPage to add 'Previous' button for > 2 pages (also applies to updateTabsetPanel)
我正在尝试在我的 RShiny 应用程序上实现一个 'Previous' 按钮,该按钮在按下时导航到上一页。我发现我可以让它在两页上工作,但是当我有第三页并尝试导航回第二页时它不再起作用。奇怪的是,对 'Next' 按钮使用相同的逻辑效果很好。
这是一个最小的可重现示例:
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 和 运行 的类似方法解决同样的问题,所以我怀疑我的实现的根本问题对于这两个函数是相同的。
两个输入元素不能有相同的inputId。尝试将第二个 link 的 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)
希望对您有所帮助!
我正在尝试在我的 RShiny 应用程序上实现一个 'Previous' 按钮,该按钮在按下时导航到上一页。我发现我可以让它在两页上工作,但是当我有第三页并尝试导航回第二页时它不再起作用。奇怪的是,对 'Next' 按钮使用相同的逻辑效果很好。
这是一个最小的可重现示例:
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 和 运行 的类似方法解决同样的问题,所以我怀疑我的实现的根本问题对于这两个函数是相同的。
两个输入元素不能有相同的inputId。尝试将第二个 link 的 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)
希望对您有所帮助!