Aligning a text with variable length in the middle of dashboard header - html

I used shiny-dashboard package and the header needs to have a title, a text and a logo.
Title should be on the left side, text should be in the middle/center of the header and logo should be on the right side. Dashboard sidebar has also two filters (select inputs).The text in the middle is showing the user selections and therefore the length of the text is different based on various selections. I don't have a css background and not sure how to position the variable length text at the center of the header. Another problem is that when I minimize my screen, the text and logo stack on top of each other and don't stay in one line as shown below:
To simplify the code, I just used a simple text and I didn't show my server code here but "long text with variable length dependant on user selections" is basically a uiOutput and will be changed based on selections.
shinyApp(
ui = dashboardPage(
dashboardHeader(
title = HTML(paste0("Dashboard example")) , titleWidth = 455,
tags$li(class="dropdown",
tags$table(style="80%;align:center;border-collapse:seperate;border-spacing:20px;",
tags$tr(
tags$td(h3("long text with variable length dependant on user selections")),
tags$td(
a(href='link',
img(src = 'http://www.clipartbest.com/cliparts/nTX/8nj/nTX8njyEc.jpeg',
title= "image title", height = "50px"),
style = "display:block;position:absolute,width:50px; padding-top:10px; padding-bottom:10px;padding-right:10px;"),
class="dropdown"))))),
dashboardSidebar(),
dashboardBody(tags$head(
tags$style(HTML("
.my_class {
font-weight: bold;
color:white;
}"))
))),
server = function(input, output) { }
)
Minimizing the screen breaks the header as below
Using the code provided below, I get this.

Try something like this:
library(shiny)
library(shinydashboard)
shinyApp(
ui = dashboardPage(
dashboardHeader(
title = "Dashboard example",
titleWidth = 455,
tags$li(
class = "dropdown",
fluidRow(
column(
width = 4,
offset = 4,
align = "center",
span("long text with variable length dependant on user selections")
),
column(
width = 4,
align = "right",
img(src = 'http://www.clipartbest.com/cliparts/nTX/8nj/nTX8njyEc.jpeg')
)
)
)
),
dashboardSidebar(width = 455),
dashboardBody(
tags$style(
".main-header .navbar-custom-menu {
width: calc(100% - 50px);
}
.main-header .navbar-nav,
.main-header .dropdown {
width: 100%;
}
.main-header img {
height: 50px
}"
)
)
),
server = function(input, output) {}
)

Related

Place actionButton on right side of titlePanel in shiny

I'm building a Shiny app with this UI. Here is nhl-logo.png
ui <- fluidPage(
titlePanel(tagList(
img(src = "nhl-logo.png", height = 60, width = 60),
"PLAY-BY-PLAY"),
windowTitle = "NHL Play-by-Play"),
div(style = "position:absolute;right:2em;",
actionButton('load_inputs', 'Load inputs'),
actionButton('save_inputs', 'Save inputs')
),
hr(),
fluidRow(...)
Unfortunately, the style is not what I want since it puts those actionButtons on a lower level than the title (NHL LOGO PLAY-BY-PLAY)
How do I change the style so that my actionButtons appear on the same horizontal level as the titlePanel?
You can add the title in a span which includes the buttons. The difference between a span and a div is that the span is inline (a div is a block).
ui <- fluidPage(
titlePanel(tagList(
img(src = "nhl-logo.png", height = 60, width = 60),
span("PLAY-BY-PLAY",
span(actionButton('load_inputs', 'Load inputs'),
actionButton('save_inputs', 'Save inputs'),
style = "position:absolute;right:2em;")
)
),
windowTitle = "NHL Play-by-Play"
),
hr(),
fluidRow()
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

Set distance between widget and its title (text) in shiny

I have a simple shiny app which diplays three colourInput() buttons. I would to reduce the blank space between every button and its title in order to be closer to it.
#ui.r
library(shiny)
library(shinydashboard)
shinyUI( dashboardPage(
dashboardHeader(
title="Styling Download Button"
),
dashboardSidebar(
div(style="display: inline-block;vertical-align:top; width: 115px;",colourInput("rightcolor",h5("Left"), value = "#00B2EE")),
div(style="display: inline-block;vertical-align:top; width: 115px;",colourInput("overlapcolor",h5("Overlap"), value = "#7CCD7C")),
div(style="display: inline-block;vertical-align:top; width: 115px;",colourInput("leftcolor",h5("Right"), value = "#FFFACD")),
),
dashboardBody()
))
#server.r
shinyServer(function(input, output) {
})
You have to change the div elements in which the titles are displayed. One way to do this is by adding the style argument to the h5 function. If you reduce the margin to 0 pixels by adding style='margin: 0px' you get the result that you want (you can also use: margin-top, margin-bottom, etc.).
If you want to adapt other Shiny widgets you can always wrap them in a div and adapt them with the style argument (example: div(style='margin: 0px; padding: 15px;', selectInput(...))). Information on other div arguments can be found here.
Your example
library(shiny)
library(shinydashboard)
library(colourpicker)
# Create ui
ui <- shinyUI( dashboardPage(
dashboardHeader(
title="Styling Download Button"),
dashboardSidebar(
div(style="display: inline-block;vertical-align:top; width: 115px;",colourInput("rightcolor",h5("Left", style='margin: 0px;'), value = "#00B2EE")),
div(style="display: inline-block;vertical-align:top; width: 115px;",colourInput("overlapcolor",h5("Overlap", style='margin: 0px;'), value = "#7CCD7C")),
div(style="display: inline-block;vertical-align:top; width: 115px;",colourInput("leftcolor",h5("Right", style='margin: 0px;'), value = "#FFFACD"))),
dashboardBody()
))
# Create Server
server <- shinyServer(function(input, output) {})
# Run app
shinyApp(ui, server)

Semi side bar collapse without hiding the title

I am trying to semi-hide the side bar from shiny app.
Using this function on the body:
tags$script(HTML("$('body').addClass('sidebar-mini');"))
I got this result:
Is there a way to hide the sidebar, without hiding the title, just modifying this script ?
You can define a short title, it will be displayed when the sidebar is collapsed :
library(shiny)
library(shinydashboard)
shinyApp(
ui = dashboardPage(
dashboardHeader(
title = tagList(
tags$span(
class = "logo-mini", "Short"
),
tags$span(
class = "logo-lg", "My Long Title"
)
)
),
dashboardSidebar(),
dashboardBody(
tags$script(HTML("$('body').addClass('sidebar-mini');"))
),
title = "Dashboard example"
),
server = function(input, output) { }
)
}
Normal:
Collapsed:
assuming that you have a built in CSS class called "hide".
tags$script(HTML("$('.sidebar-mini #YOUR_SIDEBAR_ID').addClass('hide');"))
IF you want to get fancy then do things like absolute position -9999 and opacity 0, then "reveal it" when needed...

How to reduce space between selectInput and numericInput

I have managed to code the following with Shinydashboard.
Is there any way to reduce
space between selectInput and numericInput as shown?
vertical space between "type" and "interval" selectInput?
narrow the height of menuItem "Charts"
A solution with adjustment to CSS would be good. Thank you.
Code:
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Simple board"),
dashboardSidebar(
sidebarMenu(id = "S",
menuItem("Charts", tabName = "V", icon = icon("database","fa-lg")),
h5(div(style="display:inline-block;width:50%;text-align: left;",
selectInput("Q", label = "type", choices = c("A","B","C"))),
div(style="display:inline-block;width:50%;text-align: left;",
numericInput("NT", "Num", 15, min = 10, max = 33)),
selectInput("s2", label = "interval", choices = c("17 mins","38
min","124 mins"))
)
)
),
dashboardBody()
)
server <- function(input, output) {
}
shinyApp(ui, server)
You can achieve this with the help of margin property.
For example, for selectInput, you can set margin-left:5px or so.
Similarly for interval input, you can set margin-bottom:5px for the div just above it.
You can also set negative value for margin property.
I hope this might help.

Shiny R scale down plotOutput

I'm trying to scale down a plotOutput with Shiny R.
I have this plot:
from this code:
#In ui:
fluidRow(
column(width = 12,
h4("Diagrama Persistencia"),
plotOutput("Diagrama")
)
)
#In server
output$Diagrama <- renderPlot({
F_PlotDiag(Diag = isolate(values$data), tipoPlot = input$radioPlot, tamEjeNom = input$sliderTamEjeNom)
}, height = input$sliderHeight, width = input$sliderWidth)
Notice the height and width params. This works because all is in an observeEvent context.
As you can see, the hole plot won't fit in the screen. The problem with reducing height and width is that it looks like this:
But actually, if I right click and save the FIRST image, it looks fine unlike the second image:
Is there a way to show the whole plot in the browser by scaling it down? So that I can see it as if I downloaded the image.
I really don't know much about CSS so I can't really provide any logical attempts, but this is what I've tried for my HTML:
tags$style(type="text/css", ".shiny-bound-output { transform: 'scale(.5)' }")
tags$style(type="text/css", ".shiny-plot-output { transform: 'scale(.5)' }")
tags$style(type="text/css", "#Diagrama { height: 20px }")
With no success.
Since you didn't provide a reproducible example, see if this example helps you. Based on https://stackoverflow.com/a/8839678/4190526
The key is the following line, which finds the image under the div with the id distPlot (i.e. the plot name in ui.R), and define the CSS with a max-height but otherwise auto.
tags$style(HTML("div#distPlot img {width: auto; height: auto; max-width: auto; max-height: 400px;}")),
Full code
library(shiny)
ui <- shinyUI(fluidPage(
tags$style(HTML("div#distPlot img {width: auto; height: auto; max-width: auto; max-height: 400px;}")),
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
)
)
))
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}, width=600, height=1200)
})
shinyApp(ui = ui, server = server)