简体   繁体   English

Shiny 中的页脚 Position

[英]Footer Position in Shiny

I would like to adjust the footer position in a shiny app.我想在 shiny 应用程序中调整页脚 position。 When the page content is shorter than the viewport, the footer should be at the bottom of the viewport.当页面内容比视口短时,页脚应该在视口的底部。 When the page content is longer than the viewport, the footer should be below the content.当页面内容长于视口时,页脚应位于内容下方。 This post suggests how to usually implement it in CSS. 这篇文章建议了如何通常在 CSS 中实现它。 This and similar solutions are commonly straightforward to use when writing the page's HTML code by hand.在手动编写页面的 HTML 代码时,这种和类似的解决方案通常很容易使用。

There are discussions on footer positions in shiny and some of them manage to move the footer to the bottom. shiny 中有关于页脚位置的讨论,其中一些设法将页脚移动到底部。 However, they fail to keep the footer from overlapping with the bottom of the page's main content, which requires shortening the main content's container.但是,它们无法防止页脚与页面主要内容的底部重叠,这需要缩短主要内容的容器。

Consider the following minimal working example:考虑以下最小的工作示例:

library(shiny)

ui <- navbarPage(title = "Some Example", id = "example",
    tabPanel(title = "Something", value = "something",
        textOutput("some_text")
    ),
    footer = "The footer."
)

server <- function(input, output, session) {
    output$some_text <- renderText(stringi::stri_rand_lipsum(5))
}

shinyApp(ui = ui, server = server)

It is possible to have a footer like you describe, but it is not straightforward to implement.可以有一个像你描述的页脚,但实现起来并不简单。 There does not seem to be a built-in function to position the footer, let alone in the way you would like.似乎没有内置的 function 到 position 页脚,更不用说你想要的方式了。

So, we need to write some custom CSS.所以,我们需要编写一些自定义的 CSS。 To do so, we need to be able to target the footer.为此,我们需要能够定位页脚。 When we look at the HTML produced by the example, we can see that the content specified by the argument footer is simply wrapped in a <div> tag with class row .当我们查看示例生成的 HTML 时,我们可以看到参数footer指定的内容简单地包装在带有 class row<div>标记中。

      <div class="container-fluid">
        <div class="tab-content" data-tabsetid="6611">
          <div class="tab-pane active" data-value="something" id="tab-6611-1">
            <div id="some_text" class="shiny-text-output"></div>
          </div>
        </div>
        <div class="row">The footer.</div>
      </div>
    </body>
    </html>

In any reasonably sized shiny app, there will be multiple <div> s with this class, which makes it difficult to write CSS which reliably targets just the footer.在任何大小合理的 shiny 应用程序中,都会有多个<div>与此 class 一起使用,这使得编写可靠地仅针对页脚的 CSS 变得困难。 A workaround is the following:解决方法如下:

    ui <- tagList(navbarPage(
      title = "Some Example",
      id = "example",
      tabPanel(
        title = "Something",
        value = "something",
        textOutput("some_text")
      )),
      tags$footer("The footer.", class = "footer")
    )

Now all that is left to do is adding CSS that positions the footer.现在剩下要做的就是添加定位页脚的 CSS。 I use the example found on the Bootstrap website .我使用在Bootstrap 网站上找到的示例。 A way to integrate this into shiny is like so:将其集成到 shiny 的方法如下:

    ui <- tagList(
      tags$head(
        tags$style(HTML(
          "html {
             position: relative;
             min-height: 100%;
           }
           body {
             margin-bottom: 60px; /* Margin bottom by footer height */
           }
           .footer {
             position: absolute;
             bottom: 0;
             width: 100%;
             height: 60px; /* Set the fixed height of the footer here */
             background-color: #f5f5f5;
           }"))),
      navbarPage(
      title = "Some Example",
      id = "example",
      tabPanel(
        title = "Something",
        value = "something",
        textOutput("some_text")
      )),
      tags$footer("The footer.", class = "footer")
    )

Replacing the UI in the example with the UI above will produce the desired footer that sticks to the bottom of the viewport when the content is short, but is below the content when the content is longer than the viewport.将示例中的 UI 替换为上面的 UI 将生成所需的页脚,当内容短时,它会粘在视口的底部,但当内容长于视口时,它会在内容下方。

I am not proficient in html, is this how the footer needs to look?我不精通html,这是页脚需要的样子吗?

library(shiny)

ui <- navbarPage(title = "Some Example", id = "example",
                 tabPanel(title = "Something", value = "something",
                          textOutput("some_text"),
                          ##add a footer with 2 empty lines and the info to display
                          tags$footer(HTML('
                          <br>
                          <br>
                          <p>Author: Your Name<br>
                          <a href="mailto:your_name@example.com">your_name@example.com</a></p>'))
                 )
                 
)

server <- function(input, output, session) {
    output$some_text <- renderText(stringi::stri_rand_lipsum(15)) #change this number (15) to view how the footer reacts
}

shinyApp(ui = ui, server = server)

Or perhaps:也许:

library(shiny)

ui <- navbarPage(title = "Some Example", id = "example",
                 tabPanel(title = "Something", value = "something",
                          textOutput("some_text"),
                          
                 
#add some empty lines to avoid overlap at the bottom    
tags$div(HTML('   <br>
                  <br>
                  <br>
                  <br>
                  <br>
                  <div class="footer">
                    <p>Author: Your Name<br>
                    <a href="mailto:your_name@example.com">your_name@example.com</a></p>
                  </div>')),
                  tags$style('
                  .footer {
                   position: fixed;
                   left: 0;
                   bottom: 0;
                   width: 100%;
                   background-color: lightgrey;
                   color: white;
                   text-align: right;}')),

tabPanel(title = "no_footer", value = "something",
         textOutput("some_text2")))

                 
                 


server <- function(input, output, session) {
    output$some_text <- renderText(stringi::stri_rand_lipsum(15)) #change this number (15) to view how the footer reacts
    output$some_text2 <- renderText(stringi::stri_rand_lipsum(5))
}

shinyApp(ui = ui, server = server)

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

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