How to control padding for value boxes created with bslib - css

I thought I could set a class for the value box to remove padding like this:
bslib::value_box("test", "test", class = "p-0")
Or in a minimal Shiny app:
library(shiny)
ui <- fluidPage(
theme = bslib::bs_theme(version = 5),
bslib::value_box("test", "test", class = "p-0")
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
But if I do that, it removes only the padding for the bootstrap card:
I want to remove all the blue padding around the content, including the bootstrap card body.
The problem appears to be that bslib::value_box() function wraps content with bslib::card_body_fill(), and it doesn't look like there is a way to pass arguments through to card_body_fill().
I can go into developer tools and search for "padding" and accomplish this:
Or I can add some CSS:
.card-body {
padding: 0;
}
But this will change the padding for all elements with class card_body. What if I want to target only a few value boxes?

Add a class (suppose nopad) to value_boxes for which you want to remove the padding of the content. Using developer tools, you would see that value_box contents are wrapped within a div with class .value-box-area. So Change the padding for css selector div.nopad .value-box-area.
library(shiny)
ui <- fluidPage(
theme = bslib::bs_theme(version = 5),
# css styles -------------------------------------------
tags$head(
# Note the wrapping of the string in HTML()
tags$style(HTML("
div.nopad .value-box-area {
padding: 0;
}
"))
),
# ------------------------------------------------------
bslib::value_box("test", "test", class = "p-0 nopad"), # with class nopad
bslib::value_box("test", "test", class = "p-0")
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

Related

How to fine-tune the positioning of objects rendered in r shiny?

I'm trying to adjust the positioning of conditionally-rendered objects in R shiny. When running the below skeleton code and clicking the "Delete" action button, I'd like to nudge the conditionally-rendered text ("Select series to delete >>") a bit to the right, and move the little selectInput() box that also conditionally appears on the far right a bit to the left, closer to "Select series to delete >>". I've fiddled with column widths, etc., and I've exhausted all the formatting options which I know of which are limited. Any suggestions for fine-tuning the positioning of these items? My guess is this would entail some CSS which I know almost nothing about.
Skeleton code:
library(dplyr)
library(shiny)
library(shinyjs)
toggleView <- function(input, output_name){
observeEvent(input$delSeries, {show(output_name)})
observeEvent(input$addSeries, {hide(output_name)})
}
ui <- fluidPage(br(),
useShinyjs(),
fluidRow(
column(1,actionButton("addSeries", "Add",width = '70px')),
column(1,actionButton("delSeries","Delete",width = '70px')),
column(3,h5((hidden((textOutput("delFlag")))))),
column(3,hidden(uiOutput("delSeries2")))
)
)
server <- function(input, output, session) {
output$delFlag <- renderText("Select series to delete >>")
output$delSeries2 <-
renderUI(
selectInput("delSeries3",
label = NULL,
choices = c(""),
selected = "",
width = '110px')
)
toggleView(input,"delSeries2")
toggleView(input,"delFlag")
}
shinyApp(ui,server)
You can add some styles to the 2 columns like so:
library(dplyr)
library(shiny)
library(shinyjs)
toggleView <- function(input, output_name){
observeEvent(input$delSeries, {hide(output_name)})
observeEvent(input$addSeries, {show(output_name)})
}
# (0)
css <- HTML("
.row .nudge-right {
padding-right:0;
}
.row .nudge-left {
padding-left:0;
}
")
ui <- fluidPage(
tags$head(tags$style(css)), # (1)
br(),
useShinyjs(),
fluidRow(
column(1,actionButton("addSeries", "Add",width = '70px')),
column(1,actionButton("delSeries","Delete",width = '70px')),
column(3,h5(hidden(textOutput("delFlag"))),
class = c("nudge-right", "text-right")), # (2)
column(3,hidden(uiOutput("delSeries2")), class = "nudge-left") # (2)
)
)
Explanation
The white space you see is partly due to the width of the column and partly due to the so called padding (an additional white space around the element). To bridge this gap you can:
Right align the text. Here you can rely on the already pre-defined (by the underlying bootstrap framework) class text-right.
Further decrease the gap by removing the right padding from the text column and the left padding from the input column. In order to so, you define new classes (I called them .nudge-right and .nudge-left respectively) where you deliberately set the padding to your liking (here I removed it completely, you may want to provide a small offset though - e.g. 5px).
Then all which is left is to
Create some css with the class definitions (#0)
Load the css (#1)
Assign the classes to the columns (#2)

Add some italic text on the right side of the navbarPage in Rshiny

I am building a Shiny app where I want to add some italic text in the navbarPage at the right side. According to the question: Shiny NavBar add additional info I wrote the following code, but it doesn't work out for me:
This is some demo code I have now:
ui <- fluidPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
HTML('<a style="text-decoration:none;cursor:default;color:#FFFFFF;" class="active" href="#">Dashboard</a>'), id="nav",
navbarMenu('Graphs', icon = icon('chart-area'),
tabPanel('One country'),
tabPanel('Two countries')),
tabPanel('Tables'),
tags$script(HTML("var header = $('.navbar> .container-fluid');
header.append('<div style=\"float:right\"><h5>Some very important text</h5></div>');
console.log(header)"))
))
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
This results in:
the following warning message: Warning message:
Navigation containers expect a collection of bslib::nav()/shiny::tabPanel()s and/or bslib::nav_menu()/shiny::navbarMenu()s. Consider using header or footer if you wish to place content above (or below) every panel's contents.
not the desired output. Because, the text is not visible, because it has the same colour as the background, the text is under the Dashboard, Graphs en tables text, but I want them to be on the same line. The text is not in italic.
Output now
This is what I want:
Desired output
After the answer from lz100 it looks very nice on a big screen, but the text is still under the Dashboard, Graphs en tables text. And when I change the format of the Rshiny dashboard to my very small laptopscreen, the output becomes likes this:
Output after answer from lz100
library(shiny)
library(shinythemes)
ui <- fluidPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
HTML('<a style="text-decoration:none;cursor:default;color:#FFFFFF;" class="active" href="#">Dashboard</a>'), id="nav",
navbarMenu('Graphs', icon = icon('chart-area'),
tabPanel('One country',
tags$script(HTML(
"
var header = $('.navbar> .container-fluid');
header.append('<div id=\"my-title\">Some very important text</div>');
")),
tags$style(HTML(
'
.navbar-collapse.collapse {display: inline-block !important;}
#my-title {
float:right;
display: flex;
color: white;
align-items: flex-end;
justify-content: flex-end;
height: 60px;
font-style: italic;
}
'
))
),
tabPanel('Two countries')),
tabPanel('Tables')
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
The reason you have warnings is because you put tags$script under navbarPage. Not a big deal to me, but I relocate your script inside the first tabPanel, warnings are gone.
Some CSS and JS added to create your desired output
Search/Read here if you don't know how these CSS work: https://www.w3schools.com/css/default.asp

Change the color of a shinyalert 'OK' button (confirmButtonCol) using a css stylesheet

I used the className = 'alert' attribute in shinyalert() so that I could format my shinyalerts in a CSS stylesheet. I can change the text and background color of the whole modal (eg tags$style('.alert {background-color: blue}')), so I know that setting a class name is working. I am trying to change the color of the 'OK' button, but I don't know what property to use. I know that I can set the button color using confirmButtonCol in the call to shinyalert(), but I would like to do it with a stylesheet if possible.
My code would look like this:
ui <- fluidPage(
tags$style('.alert {*SOME_PROPERTY*: #2874A6;}'),
actionButton('showalert', 'Show Alert'),
shinyalert::useShinyalert()
)
server <- function(input, output, session) {
observeEvent(input$showalert, {
shinyalert::shinyalert(title = 'Alert', className = 'alert')
})
}
shinyApp(ui = ui, server = server)
I am wondering what I can replace SOME_PROPERTY with, if anything.
Thank you in advance!
So the OK button is actually with a class name of confirm. When you select it with CSS, just add it after your class name like .alert .confirm {...}. Also, it turns out the button color uses in-line style, which has the highest priority in all CSS rules, so we need to forcibly overwrite it with !important.
code
library(shiny)
ui <- fluidPage(
tags$style('.alert .confirm {background-color: #2874A6 !important;}'),
actionButton('showalert', 'Show Alert'),
shinyalert::useShinyalert()
)
server <- function(input, output, session) {
observeEvent(input$showalert, {
shinyalert::shinyalert(title = 'Alert', className = 'alert')
})
}
shinyApp(ui = ui, server = server)

Adjust Padding of Shiny Table Using css

Let's say I want to reduce the padding between the left edge of the screen and the first column to maximize real estate on an iPhone. Here's the table:
library(shiny)
library(reactable)
ui <- fluidPage(
theme = "styles.css",
mainPanel(
align = "left",
reactableOutput("table")
)
)
server <- function(input, output, session) {
output$table <- renderReactable({
reactable(iris,
fullWidth = FALSE
)
})
}
shinyApp(ui, server)
And styles.css is:
.container-fluid {
padding-left: 0 !important;
}
This does not work. However, if I use Chrome Inspector to look at the gap, I can manually set padding-left: 0 and the gap narrows.
How do I narrow the gap from within R/Shiny//css?
Check in the inspector in the sources tab if the file is being correctly included. If it still does not work check if there are errors in your css file, that might be preventing the stylesheet from working. Finally, you might have to set the style inline or in the style tag. If you are using Bootstrap then you can use the ml-0 class in the container.

css styling for select and numeric Input

I am using R shiny and i am trying to set the same heigth for selectInput and numericInput in my ui.
I found this line:
tags$style(type="text/css", ".selectize-input {line-height: 20px;}"),
which changes the heigth for all my selectInputs.
Is there any similar line to numericInput?
Thanks
Well, it does not have a specific class such as selectInput. But you can still get the same result using the more general class form-control. We need to use height instead of line-height though, which will work fine for selectInput anyway.
ui <- fluidPage(
tags$style(type = "text/css", ".form-control.shiny-bound-input,
.selectize-input {height: 70px;}"),
selectInput(inputId = "another_id",
label = "bar",
choices = c(1, 2)),
numericInput(inputId = "some_id",
label = "foo",
value = 1)
)
server <- function(input, output, session) {}
shinyApp(ui, server)
EDIT:
In case you want to apply some CSS to a specific input, you need to use its id, so the style does not apply to all elements sharing the same class. In the example above, if you only want to change the height of the numericInput whose id is some_id, you would use #some_id to select this element, leading to the following code:
tags$style(type = "text/css",
"#some_id.form-control.shiny-bound-input {height: 70px;}")

Resources