I have a Shiny app using the shinydashboard
package in which I'm dynamically creating menuSubItem
s in the sidebarMenu
of a dashboardSidebar
. The creation of the subItems is triggered by an actionButton. I can create the menuSubItem
s on the server side just fine, but I would like to also make them sortable using the sortable
package and sortable_js
function. I just can't seem to figure out where to place the sortable_js
function to make this actually work, though.
Here's my MRE:
library(shiny)
library(shinydashboard)
library(sortable)
# Define UI for shinydashboard
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("tab_one", tabName = "test_body"),
menuItemOutput("test"),
id = "sidebar"
)
),
dashboardBody(
tabItem("test_body",
actionButton("click_me", "Click Me"))
)
)
# Define server logic to dynamically create menuSubItems
server <- function(input, output) {
observeEvent(input$click_me, {
tabs_list <-
lapply(1:5, function(x) {
menuSubItem(text = paste("tab", x))
})
output$test <- renderMenu({
menuItem("test_tabs", do.call(tagList, tabs_list))
})
sortable_js("test_tabs")
})
}
# Run the application
shinyApp(ui = ui, server = server)
Any help is much appreciated