R Shiny语义页面():根据menu()更改内容

问题描述 投票:0回答:1

如何使用

menu()
中的
semanticPage()
功能来根据选择的
menu_item()
更改 ui 页面的内容?以及如何无论选择哪个 ui 页面都始终显示它?

举个例子:

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)

我知道 href 会更改浏览器链接,但我不确定在这种情况下如何使用它。运行上述代码时,会出现菜单,但我无法显示内容。此外,单击

About
选项卡会切换到另一个页面并使菜单消失。

我是新手,所以如果有更好的选择,请告诉我,提前谢谢你!

shiny semantic-ui
1个回答
0
投票

要根据所选菜单项获取某些内容,shiny.semantic包必须与shiny.router包一起使用。下面是一个例子,但我不掌握这些包。

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)
© www.soinside.com 2019 - 2024. All rights reserved.