Search code examples
shinysemantic-ui

R Shiny semanticPage(): Change content based on menu()


How do you use the menu() function in semanticPage() to change the content of the ui page based on which menu_item() is selected? And how to always show it regardless of which ui page is selected?

As an example:

library(shiny)
library(shiny.semantic)

ui <- semanticPage(
  title = "My page",
  menu(
    list(
      menu_item("Home", href = "/"),
      menu_item("About", href = "/About")
    )
  )
)

server <- function(input, output, session) {
}

shinyApp(ui = ui, server = server)

I understand href changes the browser link, but I'm not sure how to use it in this case. When the above code is run, The menu appears, but I can't get content to show. Furthermore, clicking the About tab causes it to switch to another page and make the menu disappear.

I'm new to this, so if there are better alternatives out there, please let me know, and thank you in advance!


Solution

  • To get some contents depending on the selected menu item, the shiny.semantic package has to be used jointly with the shiny.router package. Below is an example, but I don't master these packages.

    library(shiny)
    library(shiny.semantic)
    library(shiny.router)
    library(plotly)
    
    root_page <- div(h1("Welcome page"), p("content ..."))
    other_page <- div(h1("Second page"), plotlyOutput("my_plot"))
    
    ui <- semanticPage(
      title = "My page",
      menu(
        menu_item(icon("wrench"), "Open", href = route_link("/")),
        menu_item(icon("upload"), "Upload", href = route_link("other"))
      ),
      router_ui(
        route("/", root_page),
        route("other", other_page)
      )
    )
    
    server <- function(input, output, session) {
      router_server()
      
      output$my_plot <- renderPlotly({
        plot_ly(mtcars, x = ~mpg, y = ~cyl, width = "100%")
      })
      
    }
    
    shinyApp(ui = ui, server = server)