R闪亮仪表板中标题中的主页按钮
Posted
技术标签:
【中文标题】R闪亮仪表板中标题中的主页按钮【英文标题】:Home Button in Header in R shiny Dashboard 【发布时间】:2018-05-14 04:16:07 【问题描述】:我正在尝试在我的 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)
【问题讨论】:
能否请您添加一个代码的最小示例。 这个问题中的一些提示,但我想这并不简单(需要使用一些javascript/html):***.com/q/28967949/3871924 【参考方案1】:请参阅以下解决方案。您仍然需要使用 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)
【讨论】:
【参考方案2】:这是一个使用 javascript 的选项和一个非常适合标题的主页图标:
dashboardHeader(title = "Your Title",
tags$li(a(onclick = "openTab('home')",
href = NULL,
icon("home"),
title = "Homepage",
style = "cursor: pointer;"),
class = "dropdown",
tags$script(HTML("
var openTab = function(tabName)
$('a', $('.sidebar')).each(function()
if(this.getAttribute('data-value') == tabName)
this.click()
;
);
")))
)
将openTab('home')
部分中的home
更改为您的主页选项卡,单击时它将切换到该选项卡。
【讨论】:
以上是关于R闪亮仪表板中标题中的主页按钮的主要内容,如果未能解决你的问题,请参考以下文章