Chr*_*Chr 8 css r css-position footer shiny
我想在闪亮的应用程序中调整页脚位置。当页面内容短于视口时,页脚应位于视口的底部。当页面内容长于视口时,页脚应位于内容下方。这篇文章建议了通常如何在 CSS 中实现它。当手动编写页面的 HTML 代码时,此解决方案和类似的解决方案通常易于使用。
有关于闪亮页脚位置的讨论,其中一些设法将页脚移动到底部。但是,它们无法防止页脚与页面主要内容的底部重叠,这需要缩短主要内容的容器。
考虑以下最小工作示例:
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)
Run Code Online (Sandbox Code Playgroud)
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.
So, we need to write some custom 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.
<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>
Run Code Online (Sandbox Code Playgroud)
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. 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")
)
Run Code Online (Sandbox Code Playgroud)
Now all that is left to do is adding CSS that positions the footer. I use the example found on the Bootstrap website. A way to integrate this into shiny is like so:
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")
)
Run Code Online (Sandbox Code Playgroud)
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.