【问题标题】:How to direct HTML links to siderbar items in R Shiny如何将 HTML 链接定向到 R Shiny 中的侧边栏项目
【发布时间】:2019-09-15 08:57:32
【问题描述】:

我正在尝试在我的 Shiny 仪表板中添加一个 HTML 链接。理想的解决方案是通过单击侧栏上的项目将用户定向到网站。但是,我找不到这样做的解决方案。

这是我的代码:

library(shiny)
library(shinydashboard)

header <- dashboardHeader(
  title = "Test Dashboard"
)

sidebar <- dashboardSidebar(
  sidebarMenu (
    menuItem("Test",startExpanded = TRUE,
             menuSubItem("Dashboard", tabName = "tab"),
             menuSubItem("Link")
    )
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "tab",
            box(title = "Table", width = 10, status = "warning", DT::dataTableOutput("table"))
            )
  )
  )

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {

  output$table = DT::renderDataTable({
    DT::datatable(tabledata)
  })
}

shiny::shinyApp(ui, server)

This 是当前侧边栏布局。

目前,我只在 Dashboard 选项卡中显示数据表,Link 选项卡中不显示任何内容。

我想知道是否可以通过单击侧边栏上的标签名称 "Link" 将用户重定向到网站 (https://shiny.rstudio.com/)。

我知道解决方案可能是在 Server 中呈现 HTML 链接并在 UI 中输出。请指教。

提前致谢。

【问题讨论】:

    标签: javascript html r shiny rstudio


    【解决方案1】:

    请看?menuSubItem 它有一个href 参数:

    library(shiny)
    library(shinydashboard)
    
    header <- dashboardHeader(
      title = "Test Dashboard"
    )
    
    sidebar <- dashboardSidebar(
      sidebarMenu (
        menuItem("Test",startExpanded = TRUE,
                 menuSubItem("Dashboard", tabName = "tab"),
                 menuSubItem(text = "Link", href = "https://shiny.rstudio.com/")
        )
      )
    )
    
    body <- dashboardBody(
      tabItems(
        tabItem(tabName = "tab",
                box(title = "Table", width = 10, status = "warning", DT::dataTableOutput("table"))
        )
      )
    )
    
    ui <- dashboardPage(header, sidebar, body)
    
    server <- function(input, output) {
    
      tabledata <- data.frame(replicate(10, sample(runif(10, 1, 10), rep=TRUE)))
    
      output$table = DT::renderDataTable({
        DT::datatable(tabledata)
      })
    }
    
    shiny::shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2011-06-18
      • 2019-09-18
      • 2014-11-26
      • 1970-01-01
      相关资源
      最近更新 更多