gpt4 book ai didi

r - R Shiny 仪表板标题中的主页按钮

转载 作者:行者123 更新时间:2023-12-04 12:13:17 25 4
gpt4 key购买 nike

我正在尝试在我的 Shiny 应用程序的标题中添加一个主页按钮,以便每当有人从任何选项卡单击它时,它都会重定向到第一页。目前,我在每个选项卡中使用一个 actionButton 并使用 observeEvent 返回第一页。

我无法在 Shiny 应用程序的标题部分添加任何 actionButton。这个功能有什么办法吗?

它是这样的:
Sample Shiny Look

可重现的代码:

library(shiny)
library(shinydashboard)
library(shinyjs)
options(shiny.maxRequestSize=1000*1024^2)

app <- shinyApp(
a <- dashboardPage(
dashboardHeader(title = "Sample Shiny", titleWidth=1450),
dashboardSidebar(sidebarMenu(id='tabs',
menuItem("Welcome", tabName = "welcome"),
menuItem("Tab1", tabName = "tab1"),
menuItem("Tab2",
menuSubItem("Tab2_1", tabName = "tab2_1"),
menuSubItem("Tab2_2", tabName = "tab2_2"))
)
),
dashboardBody( shinyjs::useShinyjs(),
tabItems(
tabItem(tabName="welcome", tabPanel(title = "Score",fluidRow(valueBoxOutput("box_01"),valueBoxOutput("box_02")))),
# First tab content
tabItem(tabName = "tab1",actionButton("homeButton1", "Home")),
# Second tab content
tabItem(tabName = "tab2_1",tabsetPanel(id = "test",tabPanel(title = "tab2_1",actionButton("homeButton2", "Home"),actionButton("NextButton2", "Tab3")))),
tabItem(tabName = "tab2_2",tabsetPanel(id = "outputTabset",tabPanel(title = "Tab 3",actionButton("homeButton3", "Home"))))
)
)),

b<-shinyServer(function(input, output, session) {

##########Links from first page
output$box_01 <- renderValueBox({
box1<-valueBox(value=01,
icon = icon("database",lib="font-awesome")
,width=NULL
,color = "blue"
,href="#"
,subtitle=HTML("<b>Tab 1</b>")
)
box1$children[[1]]$attribs$class<-"action-button"
box1$children[[1]]$attribs$id<-"button_box_01"
return(box1)

})

output$box_02 <- renderValueBox({
box2<-valueBox(value=02,
icon = icon("user-secret",lib="font-awesome")
,width=NULL
,color = "yellow"
,href="#"
,subtitle=HTML("<b>Tab 2</b>")
)
box2$children[[1]]$attribs$class<-"action-button"
box2$children[[1]]$attribs$id<-"button_box_02"
return(box2)

})

observeEvent(input$button_box_01,{
if(input$button_box_01[1]>0){
newtab <- switch(input$tabs,
"welcome" = "tab1",
"tab1" = "welcome"
)
updateTabItems(session, "tabs", newtab)
} })

observeEvent(input$button_box_02,{
if(input$button_box_02[1]>0){
newtab <- switch(input$tabs,
"welcome" = "tab2_1",
"tab2_1" = "welcome"
)
updateTabItems(session, "tabs", newtab)
} })


### HomeButtons

observeEvent(input$homeButton1,{
newtab <- switch(input$tabs,
"welcome" = "tab1",
"tab1" = "welcome"
)
updateTabItems(session, "tabs", newtab)
})
observeEvent(input$homeButton2,{
newtab <- switch(input$tabs,
"welcome" = "tab2_1",
"tab2_1" = "welcome"
)
updateTabItems(session, "tabs", newtab)
})

observeEvent(input$NextButton2,{
newtab <- switch(input$tabs,
"tab2_2" = "tab2_1",
"tab2_1" = "tab2_2"
)
updateTabItems(session, "tabs", newtab)
})

observeEvent(input$homeButton3,{
newtab <- switch(input$tabs,
"welcome" = "tab2_2",
"tab2_2" = "welcome"
)
updateTabItems(session, "tabs", newtab)
})


#######SideBar Disable

addClass(selector = "body", class = "sidebar-collapse")


})
)

shiny::runApp(app,launch.browser=TRUE,host="0.0.0.0",port=6105)

最佳答案

请参阅以下解决方案。您仍然需要使用 CSS 设置位置样式。关键是将 actionButton 放入带有 tags$li(class = "dropdown", ...) 的 header 中,否则dashboardHeader 不会接受:

ui <- dashboardPage(
dashboardHeader(title = "Demo", tags$li(class = "dropdown", actionButton("home", "Home"))),
dashboardSidebar(sidebarMenu(id = "sidebar", # id important for updateTabItems
menuItem("Home", tabName = "home", icon = icon("house")),
menuItem("Tab1", tabName = "tab1", icon = icon("table")),
menuItem("Tab2", tabName = "tab2", icon = icon("line-chart")),
menuItem("Tab3", tabName = "tab3", icon = icon("line-chart")))
),

dashboardBody(
tabItems(
tabItem("home", "This is the home tab"),
tabItem("tab1", "This is Tab1"),
tabItem("tab2", "This is Tab2"),
tabItem("tab3", "This is Tab3")
))
)
server = function(input, output, session){
observeEvent(input$home, {
updateTabItems(session, "sidebar", "home")
})
}
shinyApp(ui, server)

enter image description here

关于r - R Shiny 仪表板标题中的主页按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47569992/

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