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

查看:106
本文介绍了单击Shiny App中的tabPanel后如何定向到另一个网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 发光 应用程序:

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)

它产生:

如此处所述.单击主页按钮tabPanel()后如何将页面定向到www.google.com?

As stated there. How can I direct the page to www.google.com after clicking Home button tabPanel()?

更新

使用Phi的html centric way最新方法后:

After using Phi's html centric way latest method:

推荐答案

仅限用户界面的解决方案

无需使用反应式逻辑进行重定向.我们只想在html中放置一个链接.

There's no need to use reactive logic to redirect. We just want to put a link in the html.

有时候闪亮会让您忘记您实际上在做的只是在ui上编写html.

Sometimes shiny lets you forget that what you're really doing is just writing html on the ui.

例如,如果我们将navbarPage的输出打印到控制台,则会得到:

For example, if we print the output of navbarPage to the console we get:

> 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>

这里的问题是函数navbarPage正在应用一些逻辑,它假定您只在其中放置tabPanel对象,并且对对象做一些非常特别的事情(它在该位置创建了一个链接,因此任何链接我们放入其中将被取消).我们可以通过以下两种方式之一进行更改.

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). We can change that in one of two ways.

1)使用tag和/或tags对象编写HTML,并创建自己的函数

1) Write HTML using the tag and/or tags objects and make your own function

第一个方法就是简单地重写navbarPage函数,使其产生您想要的输出类型.

The first is simply to rewrite the navbarPage function in a way that produces the kind of output you're after.

您可以查找用于创建引导导航栏的html,并使用?tags?tag

You can look up the html for making a bootstrap navbar page and replicate it using ?tags or ?tag

(这是将新功能添加到闪亮功能的好方法.我编写了一个程序包,通过将html字符串转换为等效的闪亮功能来加快此过程: https://github.com/shapenaji/midas ).但是对于这样的事情,那就太过分了.

(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). But for something like this, that's overkill.

2)将所需对象添加到函数的输出中.

在这里,我们真的只想用链接替换标头中的该原始对象,所以让我们这样做.

Here, we really just want to replace that home object in the header with a link, so lets just do that.

我们保存导航栏功能的输出.

We save the output of the navbar function.

然后,将对象的该部分替换为所需的hack ... err ...链接.

Then we replace that part of the object with our desired hack ...err... link.

一个有光泽的对象,就像navbarPage的输出一样,只是一个列表,我们可以像导航列表一样导航到底部.

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.

(在这一步中,我们需要花点时间才能找到该对象.它在嵌套列表中相当深.但是您可以在控制台中浏览该对象以替换您要使用的部分.我只是一次剥掉一个列表.)

(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.)

(我已删除了ShinyUI和ShinyServer函数,因为在当前版本的Shiny中不再需要那些包装器了)

(I have removed the shinyUI and shinyServer functions, as those wrappers are no longer necessary in the current version of 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...)
  }
)


navbarPage()包裹在fixedPage()


How can I modify when navbarPage() is wrapped under fixedPage()

答案:更改您要修改的内容,整个导航栏页面现在位于固定页面内,这会将page[[3]][[1]]$children[[1]]添加到开头

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

一种更清洁的方法:

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...)
  }
)

这篇关于单击Shiny App中的tabPanel后如何定向到另一个网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆