![](/img/trans.png)
[英]How to display a fixed image on/with/upon a fixed header on R Shiny Dashboard
[英]R Shiny - Fixed sidebar and main header in a Shiny dashboard
我有一個簡化的閃亮儀表板(見下面的代碼)。 我想修復側邊欄和主標題。 所以,在這里的其他帖子的幫助下,我寫了一個CSS文件來解決這個問題。
.sidebar {
color: #FFF;
position: fixed;
width: 220px;
white-space: nowrap;
overflow: visible;
}
.main-header {
position: fixed;
width:100%;
}
它幾乎可以工作......側邊欄和主標題是固定的,但主標題有一個問題,隱藏第一個框的標題。 我想在CSS中有一些關鍵字可以解決問題,但我沒有找到它們(CSS中的新手......)。
Shiny儀表板代碼如下。
library(ggplot2)
library(shiny)
library(shinydashboard)
CountPlotFunction <- function(MyData)
{
MyPlot <- ggplot(data = MyData, aes(x = MyData)) +
geom_bar(stat = "count", aes(fill = MyData)) +
geom_text(stat = "count", aes(label = ..count..)) +
scale_x_discrete(drop = FALSE) +
scale_fill_discrete(drop = FALSE)
return(MyPlot)
}
# The data
var1 <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada")
var2 <- c("UnitedStates","France","SouthAfrica","SouthAfrica","UnitedStates","SouthAfrica","France","Norge","SouthAfrica","France","France")
var3 <- c("Brazil","Colombia","China","China","Brazil","China","Colombia","Belgium","China","Colombia","Colombia")
df <- data.frame(var1, var2, var3)
# The Shiny app
Interface <-
{
dashboardPage(
title = "Dashboard", skin = "yellow",
dashboardHeader(title = "Dashboard"),
dashboardSidebar(
sidebarMenu(
sidebarSearchForm(textId = "searchText", buttonId = "searchButton", label = "Search..."),
menuItem(text = "Analysis", tabName = "Analysis", icon = icon("dashboard")))
),
dashboardBody(
tags$head(includeCSS('www/style.css')),
tabItem(tabName = "Analysis",
fluidPage(box(title = "Choose the questions", status = "warning", solidHeader = TRUE, collapsible = FALSE, width = 12,
checkboxGroupInput(inputId = "ChooseQuestion", label = NULL, choices = colnames(df),
selected = colnames(df)[1])),
box(title = "Results", status = "warning", solidHeader = TRUE, collapsible = FALSE, width = 12,
uiOutput("ui_plot")))
)
)
)
}
Serveur <- function(input, output)
{
# gen plot containers
output$ui_plot <- renderUI({
out <- list()
if (length(input$ChooseQuestion)==0){return(NULL)}
for (i in 1:length(input$ChooseQuestion)){
out[[i]] <- plotOutput(outputId = paste0("plot",i))
}
return(out)
})
# render plots
observe({
for (i in 1:length(input$ChooseQuestion)){
local({
ii <- i
output[[paste0('plot',ii)]] <- renderPlot({
if ( length(input$ChooseQuestion) > ii-1 ){
return(CountPlotFunction(MyData = df[input$ChooseQuestion[[ii]]]))
}
NULL
})
})
}
})
}
shinyApp(ui = Interface, server = Serveur)
將以下內容添加到CSS文件中,它應該可以正常工作。
.content {
padding-top: 60px;
}
我不知道這是因為一年前事情發生了變化,但我運氣好多了:
.content {
margin-top: 50px;
}
像熊兵的答案那樣有padding-top
在底部留下了一個奇怪的空白區域(也就是50px而不是60px)。
希望這對后代有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.