Skip to content

Instantly share code, notes, and snippets.

@bborgesr
Created May 9, 2017 16:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bborgesr/21e3a955b8640cfe480dd11f06b21bf5 to your computer and use it in GitHub Desktop.
Save bborgesr/21e3a955b8640cfe480dd11f06b21bf5 to your computer and use it in GitHub Desktop.
How to dynamically add items to a dropdownMenu in shinydashboard
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Dropdowns 2.0",
dropdownMenuOutput("menu")
),
dashboardSidebar(
helpText("Add another item to the dropdown menu by clicking ",
"on the button below"),
actionButton("addItem", "Add another item")
),
dashboardBody()
)
server <- function(input, output, session) {
tasks <- reactiveValues(
code = list(id = "code", value = 15, color = "aqua",
text = "Refactor code"),
layout = list(id = "layout", value = 40, color = "green",
text = "Design new layout"),
docs = list(id = "docs", value = 25, color = "red",
text = "Write documentation")
)
# actually render the dropdownMenu
output$menu <- renderMenu({
items <- lapply(tasks, function(el) {
taskItem(value = el$value, color = el$color, text = el$text)
})
dropdownMenu(
type = "tasks", badgeStatus = "danger",
.list = items
)
})
observeEvent(input$addItem, {
showModal(modalDialog(title = "Add new task",
textInput(paste0("id", input$addItem), "Task ID"),
numericInput(paste0("val", input$addItem), "Task value", 0),
selectInput(paste0("col", input$addItem), "Task color",
choices = c("red", "yellow", "aqua", "blue",
"light-blue", "green", "navy", "teal",
"olive", "lime", "orange", "fuchsia",
"purple", "maroon", "black")
),
textInput(paste0("text", input$addItem), "Task text"),
actionButton(paste0("go", input$addItem), "Add item"),
easyClose = TRUE, footer = NULL
))
observeEvent(input[[paste0("go", input$addItem)]], {
tasks[[paste0("id", input$addItem)]] <- list(
id = input[[paste0("id", input$addItem)]],
value = input[[paste0("val", input$addItem)]],
color = input[[paste0("col", input$addItem)]],
text = input[[paste0("text", input$addItem)]]
)
removeModal()
})
})
}
shinyApp(ui, server)
@Nicolabo
Copy link

Thanks for this great example! However, there is one tricky thing here when I add additional tabs in dashboardSidebar. For example, when I have two tabs and I move to second tab, and then decide to add new task in dynamic dropdownMenu, It will automatically move to first tab. Do you know how to block this behaviour? You can find modified version of your code here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment