2017-04-09 56 views
2

我想在標題面板的下拉菜單中放置多個鏈接,但現在我只能通過標記$ li創建一個平整的水平佈局,而我想要一個垂直分組下拉菜單。shinydashboard header dropdown add group links

最小可重複代碼如下,我的意思是我想將linkA和linkB放在grouplinkAB下,用戶可以在新窗口中打開其中的一個。可以通過代碼中的dropdownMenu(type ='notifications',...)來實現,但我不知道在哪裏放置「grouplinkAB」的組名,以及點擊鏈接,我也必須隱藏文字「你有2個通知」,所以我想用tags $ li和tags $ ul來實現它,但是我對HTML有一點了解,任何幫助都將不勝感激。

library(shinydashboard) 
library(shiny) 

runApp(
    shinyApp(
    ui = shinyUI(
     dashboardPage(
     dashboardHeader(title='Reporting Dashboard', 
         tags$li(class="dropdown",tags$a("grouplinkAB",href="http://stackoverflow.com/", target="_blank")), 
         tags$li(class="dropdown",tags$a("linkA",href="http://stackoverflow.com/", target="_blank")), 
         tags$li(class="dropdown",tags$a("linkB",href="http://stackoverflow.com/", target="_blank")), 
         dropdownMenu(type='notifications', 
            notificationItem(text='linkA',href="http://stackoverflow.com/"), 
            notificationItem(text='linkB',href="http://stackoverflow.com/") 
            ) 
     ), 
     dashboardSidebar(), 
     dashboardBody() 
    ) 
    ), 
    server = function(input, output){} 
), launch.browser = TRUE 
) 

回答

2

好的,我在一年前看到了類似的要求,但沒有看太多。這一次,我試圖讓你的代碼工作,然後我不能看到dropdownMenu代碼,並看到它沒有設置來處理這個問題,但可以相當容易地進行修改。

雖然我選擇不這樣做,但我創建了專門做這個的dropdownMenu的新版本。

下面是代碼:

library(shinydashboard) 

dropdownHack <- function (...,badgeStatus = NULL, .list = NULL,menuname=NULL) 
{ 
    if (!is.null(badgeStatus)){ 
    shinydashboard:::validateStatus(badgeStatus) 
    } 
    items <- c(list(...), .list) 
    lapply(items, shinydashboard:::tagAssert, type = "li") 
    dropdownClass <- paste0("dropdown ", "text-menu") 
    numItems <- length(items) 
    if (is.null(badgeStatus)) { 
    badge <- NULL 
    } 
    else { 
    badge <- span(class = paste0("label label-", badgeStatus), numItems) 
    } 
    tags$li(class = dropdownClass, a(href="#", class="dropdown-toggle", 
            `data-toggle`="dropdown", menuname, badge), 
      tags$ul(class = "dropdown-menu", items ) 
) 
} 

menuitemHack <- function(text,href){ 
    notificationItem(text=text,href=href,icon=shiny::icon("rocket")) 
} 

runApp(
    shinyApp(
    ui = shinyUI(
     dashboardPage(
     dashboardHeader(title='Reporting Dashboard', 
         dropdownHack(menuname="GroupAB", 
            menuitemHack(text='linkA',href="http://stackoverflow.com/"), 
            menuitemHack(text='linkB',href="http://stackoverflow.com/") 
         ), 
         dropdownMenu(type='notifications', 
            notificationItem(text='linkA',href="http://stackoverflow.com/"), 
            notificationItem(text='linkB',href="http://stackoverflow.com/") 
         ) 
     ), 
     dashboardSidebar(), 
     dashboardBody() 
    ) 
    ), 
    server = function(input, output){} 
), launch.browser = TRUE 
) 

這裏是結果:

enter image description here

注:

  • 它需要一個圖標,你可以選擇任何fontAwesome或字畫,如果你想什麼都沒有,那裏可能有一個空白的地方。
  • 我想如果ShinyDashboard結構發生很大變化,它會中斷,所以記住這一點。
  • 也許下一個版本也會支持這個選項,它只是幾行額外的代碼。
+1

太棒了!它按預期工作,非常感謝。 現在下拉菜單的默認設置太寬了,並且鏈接無法在新窗口中打開。我會搜索一些關於它的設置。 – earclimate