简体   繁体   English

将外部超链接添加到r Shiny中的tabPanel或navbarMenu

[英]Add external hyperlink to tabPanel or navbarMenu in r Shiny

I am trying to add external hyperlinks to the tabPabel and navbarMenu tabs/dropdowns in a navbarPage setup in Shiny (using bootstrapPage ). 我试图在Shiny的navbarPage设置中(使用bootstrapPage )将外部超链接添加到tabPabelnavbarMenu选项卡/下拉列表。 I found multiple questions that refer to linking to another tab within a Shiny app, but I want to specifically link to another webpage without opening a new browser window. 我发现了多个问题,这些问题涉及到链接到Shiny应用程序中的另一个选项卡,但是我想专门链接到另一个网页,而无需打开新的浏览器窗口。

I found the following questions that kind of get there: 我发现以下问题可以解决:

How to direct to another web page after clicking tabPanel in Shiny App 单击Shiny App中的tabPanel后如何定向到另一个网页

Open URL by tabPanel in Shiny 在Shiny中按tabPanel打开URL

The second question is what I want to do; 第二个问题是我想做什么? however, when I use the following method to accomplish this, it adds a "phantom" tab: 但是,当我使用以下方法完成此操作时,它添加了一个“幻影”选项卡:

tabPanel(a("Open Sales Gsheet", href="http://google.com", target="_blank"))

Here is some example code for the Shiny app setup that I am working with: 这是我正在使用的Shiny应用程序设置的一些示例代码:

library(shiny); library(shinythemes)

ui <- bootstrapPage("", 
                navbarPage(
                  id = "navbar", 
                  theme = shinytheme("yeti"),
                  title = a("Home", href = "https://google.com", style = "color:white;"),  ## page title with hyperlink and browser tab title (works as intended)

                  tabPanel(title = HTML("Panel_1</a></li><li><a href='http://google.com' target='_blank'>test")),  ## tabPanel hyperlink test (adds "phantom" tab)

                  navbarMenu(title = "Test Menu", 
                             tabPanel(title = a("Open Sales Gsheet", href="http://google.com", target="_blank"))   ## navbarMenu hyperlink test (adds "phantom" option)
                             )
                  )
            )

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

  ## empty server

  }

shinyApp(ui, server)

Here is a screenshot of the "phantom" tab issue: 这是“幻影”标签问题的屏幕截图:

https://i.imgur.com/tIYbhzT.png https://i.imgur.com/tIYbhzT.png

As you can see, both the tabPanel and navbarMenu tabs/dropdowns have additional "phantom" tabs that have been added as a result. 如您所见, tabPanelnavbarMenu选项卡/下拉菜单均具有其他“幻像”选项卡,因此已添加了这些选项卡。 The first question I posted above shows an answer that involves editing the html code (or the list that is produced in R)... but I cannot figure out how to do this with a tabPanel or navbarMenu object. 我在上面发布的第一个问题显示了一个涉及编辑html代码(或R中生成的列表)的答案...但是我无法弄清楚如何使用tabPanelnavbarMenu对象执行此navbarMenu

I just want this to look like a normal navbarPage dropdown where the tabPanel and navbarMenu selections link to an external site (in the same browser window - browseURL as an observeEvent in the server script does not work since it opens in another window). 我只是想这看起来像一个正常的navbarPage下拉菜单,其中的TabPanel和navbarMenu选择链接到外部网站(在同一个浏览器窗口- browseURL作为observeEvent在服务器脚本不工作,因为它在另一个窗口中打开)。 Any help would be appreciated! 任何帮助,将不胜感激!

It's tricky to add custom elements in a shiny navbar page but it can be done with some javascript. 在闪亮的导航栏页面中添加自定义元素很棘手,但是可以使用一些javascript来完成。 The following code should add your link to the dropdown menu in the navbar. 以下代码应将您的链接添加到导航栏中的下拉菜单。 Save it as a .js file in your app's base directory then include the script in your ui function. 将其另存为应用程序基本目录中的.js文件,然后将该脚本包含在ui函数中。

navAppend.js in your app's base directory: 应用程序基本目录中的navAppend.js

$(document).ready(function() {
  $(".navbar .container-fluid .navbar-nav .dropdown .dropdown-menu").append('<li><a href="https://google.com" target="_blank">Open Sales Gsheet</a></li>');
});

in your ui : 在您的ui

ui <- tagList(
  tags$head(includeScript("navAppend.js")),
  navbarPage(
    id = "navbar", 
    theme = shinytheme("yeti"),
    title = a("Home", href = "https://google.com", style = "color:white;"),  ## page title with hyperlink and browser tab title (works as intended)

    # nav menu the link will be added to
    navbarMenu(title = "Test Menu")
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM