简体   繁体   English

单击Shiny App中的tabPanel后如何定向到另一个网页

[英]How to direct to another web page after clicking tabPanel in Shiny App

I have the following Shiny app: 我有以下闪亮的应用程序:

library(shiny)

shinyApp(
  ui <- shinyUI(
    navbarPage("X-men",
               tabPanel("",icon = icon("home", lib = "glyphicon")  ),
               tabPanel("Plot")
            )),
  server <- shinyServer(function(input, output) {})
)
# Run the application 
shinyApp(ui = ui, server = server)

It produces: 它产生:

在此处输入图片说明

As stated there. 如那里所述。 How can I direct the page to www.google.com after clicking Home button tabPanel() ? 单击“主页”按钮tabPanel()后,如何将页面定向到www.google.com?


Update 更新资料

After using Phi's html centric way latest method: 在使用Phi的html centric way最新方法之后:

在此处输入图片说明

A UI-only solution 仅UI的解决方案

There's no need to use reactive logic to redirect. 无需使用反应式逻辑进行重定向。 We just want to put a link in the html. 我们只想在html中放置一个链接。


Sometimes shiny lets you forget that what you're really doing is just writing html on the ui. 有时,闪亮会让您忘记您真正在做什么,只是在ui上编写html。

For example, if we print the output of navbarPage to the console we get: 例如,如果我们将navbarPage的输出打印到控制台, navbarPage得到:

> navbarPage("X-men",
+            tabPanel("",icon = icon("home", lib = "glyphicon")  ),
+            tabPanel("Plot")
+ )
<nav class="navbar navbar-default navbar-static-top" role="navigation">
  <div class="container-fluid">
    <div class="navbar-header">
      <span class="navbar-brand">X-men</span>
    </div>
    <ul class="nav navbar-nav">
      <li class="active">
        <a href="#tab-8961-1" data-toggle="tab" data-value="">
          <i class=" glyphicon glyphicon-home"></i>

        </a>
      </li>
      <li>
        <a href="#tab-8961-2" data-toggle="tab" data-value="Plot">Plot</a>
      </li>
    </ul>
  </div>
</nav>
<div class="container-fluid">
  <div class="tab-content">
    <div class="tab-pane active" data-value="" data-icon-class="glyphicon glyphicon-home" id="tab-8961-1"></div>
    <div class="tab-pane" data-value="Plot" id="tab-8961-2"></div>
  </div>
</div>

The issue here is that the function navbarPage is applying some logic, it assumes that you only put tabPanel objects up there and it is doing something very special with the objects (it's creating a link in that spot, so any link we put inside that will get nullified). 这里的问题是,功能navbarPage正在应用一些逻辑,它假定您只在其中放置了tabPanel对象,并且对这些对象进行了非常特殊的操作(它在该位置创建了一个链接,因此我们放置在其中的任何链接都会无效)。 We can change that in one of two ways. 我们可以通过以下两种方式之一进行更改。

1) Write HTML using the tag and/or tags objects and make your own function 1)使用tag和/或tags对象编写HTML并编写自己的函数

The first is simply to rewrite the navbarPage function in a way that produces the kind of output you're after. 第一种方法是简单地重写navbarPage函数,使其产生所需的输出类型。

You can look up the html for making a bootstrap navbar page and replicate it using ?tags or ?tag 您可以查找用于制作引导导航页的html,并使用?tags?tag复制它

(This is a great way to add new functionality to shiny. I wrote a package to speed this process by turning an html string into equivalent shiny functions: https://github.com/shapenaji/midas ). (这是向闪亮添加新功能的好方法。我编写了一个程序包,通过将html字符串转换为等效的闪亮功能来加快此过程: https : //github.com/shapenaji/midas )。 But for something like this, that's overkill. 但是对于这样的事情,那太过分了。

2) Hack in the desired object to the output of the function. 2)将所需的对象加入到函数的输出中。

Here, we really just want to replace that home object in the header with a link, so lets just do that. 在这里,我们真的只想用链接替换标头中的该home对象,因此就可以这样做。

We save the output of the navbar function. 我们保存导航栏功能的输出。

Then we replace that part of the object with our desired hack ...err... link. 然后,用所需的hack ... err ...链接替换对象的该部分。

A shiny object, like the output of navbarPage is just a list, and we can navigate our way to the bottom in the same way we would navigate a list. navbarPage的输出一样,一个有光泽的对象只是一个列表,我们可以像导航列表一样导航到底部。

(In this one, we have to dig a little bit to get down to the object. It's fairly deep in the nested list. But you can explore the object in the console to replace the parts you're after. I just peeled the list one at a time.) (在此示例中,我们需要花一点时间才能找到该对象。它在嵌套列表中相当深。但是您可以在控制台中浏览该对象以替换您想要的部分。我只是剥离了该列表一次一个。)

(I have removed the shinyUI and shinyServer functions, as those wrappers are no longer necessary in the current version of shiny) (我删除了shinyUI和shinyServer函数,因为在当前版本的shiny中不再需要那些包装器)

library(shiny)

shinyApp(

  ui = {
      page <- 
        navbarPage("X-men",id = "navibar",
                   tabPanel("placeholder"),
                   tabPanel("Plot",value = "plot"),
                   selected = "plot"
        )
      # Replace the second object in the navbar with the link
      page[[3]][[1]]$children[[1]]$children[[2]]$children[[1]] <- 
        tags$li(tags$a(
          href = 'http://google.com', 
          icon("home", lib = "glyphicon")
          )
        )
      # Finally return the page object, modified
      page
  },

  server = function(input, output, session) {
    # No server code necessary (yet...)
  }
)

How can I modify when navbarPage() is wrapped under fixedPage() navbarPage()包装在fixedPage()下时,如何修改

Answer: Change what you're modifying, the entire navbar page is inside the fixed page now, which adds page[[3]][[1]]$children[[1]] to the beginning 答:更改您要修改的内容,整个导航栏页面现在位于固定页面内,这会将page[[3]][[1]]$children[[1]]到开头

page[[3]][[1]]$children[[1]][[3]][[1]]$children[[1]]$children[[2]]$children[[1]] <- 
      tags$li(
        tags$a(
          href = 'http://google.com', 
          icon("home", lib = "glyphicon")
        )
      )

EDIT 2: A Cleaner way to do it: 编辑2:一种更清洁的方法:

library(shiny)

shinyApp(

  ui = {
      page <- 
        navbarPage("X-men",id = "navibar",
                   tabPanel("placeholder"),
                   tabPanel("Plot",value = "plot"),
                   selected = "plot"
        )
      # Replace the second object in the navbar with the link
      page[[3]][[1]]$children[[1]]$children[[2]]$children[[1]] <- 
        tags$li(tags$a(
          href = 'http://google.com', 
          icon("home", lib = "glyphicon")
          )
        )
      # Finally return the page object, wrapped by fixedPage
      fixedPage(page)
  },

  server = function(input, output, session) {
    # No server code necessary (yet...)
  }
)

There are many ways you can accomplish this in R and Shiny. 您可以通过多种方式在R和Shiny中完成此操作。 Here's one : 这是一个:

library(shiny)
shinyApp(
shinyUI(
uiOutput("navigate")
),

shinyServer(function(input, output, session) {
output$navigate <- renderUI({
  fixedPage(
    navbarPage("X-men",id = "navibar",
               tabPanel("",icon = icon("home", lib = "glyphicon"),value = "home"),
               tabPanel("Plot",value = "plot"),
               selected = "plot"
    )
  )
})
observeEvent(input$navibar,{
  if(input$navibar == "home"){
    browseURL("https://www.google.com")
  }
})
})
)

Basically all thats happening is that the observeEvent() function is triggered when the tab is clicked on opening a browser window to the URL.Here's also an example of using ui.R and server.R rather than a single file. 基本上所有发生的事情是在打开URL的浏览器窗口时单击选项卡时触发了observeEvent()函数。这也是使用ui.R和server.R而不是单个文件的示例。

ui.R 用户界面

library(shiny)

shinyUI(
fluidPage(
navbarPage("X-men",id = "navibar",
                         tabPanel("",icon = icon("home", lib =  "glyphicon"),value = "home"),
                         tabPanel("Plot",value = "plot"),
                         selected = "plot"
)
)
)

server.R 服务器

library(shiny)

shinyServer(function(input, output) {
observeEvent(input$navibar,{
if(input$navibar == "home"){
  browseURL("https://www.google.com")
}
})
})

Indeed we can also do this in a more html centric way, as Shape suggested, with a much simpler : 实际上,我们也可以按照Shape的建议,以更加HTML中心的方式进行此操作,并且更加简单:

library(shiny)

shinyApp(
ui <- shinyUI(
navbarPage("X-men",
           tabPanel(tags$a(href = 'http://google.com', icon("home", lib = "glyphicon"))),
           tabPanel("Plot")
        )),
server <- shinyServer(function(input, output) {})
)

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

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