gpt4 book ai didi

r - 在 R Shiny 中使用 ActionButton 跳转到选项卡项目

转载 作者:行者123 更新时间:2023-12-04 10:52:41 42 4
gpt4 key购买 nike

我正在使用 bs4Dash 构建应用程序包,我想在主页中包含操作按钮,允许用户跳转到适当的页面。但是,按钮不执行任何操作。

这与 this question 非常相似.我相信这里的问题是 updatebs4TabItems需要一个 TabSetPanel inputId... 除了我不希望在此处包含 tabset 面板。

library(shiny)
library(bs4Dash)

ui <- bs4DashPage(
# Sidebar -------------------------------------------------------------
sidebar = bs4DashSidebar(
bs4SidebarMenu(
bs4SidebarMenuItem(
"Welcome",
tabName = "item0"
),
bs4SidebarMenuItem(
"Page 1",
tabName = "item1"
),
bs4SidebarMenuItem(
"Page 2",
tabName = "item2"
)
)
),
# Body -------------------------------------------------------------
body = bs4DashBody(
bs4TabItems(
bs4TabItem(
tabName = "item0",
fluidRow(
actionButton("JumpToV1", "Go to Page 1"),
actionButton("JumpToV2", "Go to Page 2")
)
),
bs4TabItem(
tabName = "item1",
fluidRow(
bs4Callout(
title = "This is Page 1",
elevation = 4,
status = "danger"
)
)
),
bs4TabItem(
tabName = "item2",
fluidRow(
bs4Callout(
title = "This is Page 2",
elevation = 4,
status = "danger")
)
)
)
)
)

server <- function(input, output, session) {
observeEvent(input$JumpToV1, {
updatebs4TabItems(session, "item0", selected = "item1")
})

observeEvent(input$JumpToV2, {
updatebs4TabItems(session, "item0", selected = "item2")
})
}

shinyApp(ui, server)

最佳答案

你很接近!有两件事需要调整。从文档中,请注意 updatebs4TabItems 中的 selected 参数采用整数(所选选项卡的位置),而不是字符串(因此不是您拥有的 id 名称)。此外,updatebs4TabItems 的 inputID 参数将引用您需要设置的 sidebarID。下面的代码应该按照你想要的方式工作。

library(shiny)
library(bs4Dash)

ui <- bs4DashPage(
# Sidebar -------------------------------------------------------------
sidebar = bs4DashSidebar(
bs4SidebarMenu(
id = "sidebarID", #note the new ID here
bs4SidebarMenuItem(
"Welcome",
tabName = "item0"
),
bs4SidebarMenuItem(
"Page 1",
tabName = "item1"
),
bs4SidebarMenuItem(
"Page 2",
tabName = "item2"
)
)
),
# Body -------------------------------------------------------------
body = bs4DashBody(
bs4TabItems(
bs4TabItem(
tabName = "item0",
fluidRow(
actionButton("JumpToV1", "Go to Page 1"),
actionButton("JumpToV2", "Go to Page 2")
)
),
bs4TabItem(
tabName = "item1",
fluidRow(
bs4Callout(
title = "This is Page 1",
elevation = 4,
status = "danger"
)
)
),
bs4TabItem(
tabName = "item2",
fluidRow(
bs4Callout(
title = "This is Page 2",
elevation = 4,
status = "danger")
)
)
)
)
)

server <- function(input, output, session) {
observeEvent(input$JumpToV1, {
#changed ID and selected here and below
updatebs4TabItems(session, inputId = "sidebarID", selected = 2)
})

observeEvent(input$JumpToV2, {
updatebs4TabItems(session, inputId = "sidebarID", selected = 3)
})
}

shinyApp(ui, server)

关于r - 在 R Shiny 中使用 ActionButton 跳转到选项卡项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59396187/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com