Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny overriding CSS file in header

Tags:

css

r

shiny

I'm including a CSS file in a shiny app. I include it in the ui() function prior to including a header that attempts to override the CSS file. Everything I've read indicates that the header CSS declaration should override the CSS file. However it is not overriding it unless I use the "!important" rule after the header CSS declaration. Here's the CSS file (named temp.css):

 .btn-default {
    color: #ffffff;
    background-color: #464545;
    border-color: #464545;
 }

and here is the R shiny file (named app.R):

library(shiny) 

shinyApp(
   ui <- shinyUI(
      fluidPage( 

         # # Set up the basic CSS styling.
         includeCSS("temp.css"),

         # HTML header with style specifications.
         tags$head(
            tags$style(

               # Colorize the actionButton.
               HTML(".btn-default {
                         background-color: rgb(40,110,5);
                         color: rgb(199,207,111);
                     }"
               )
            )
         ),
         fluidRow(
            sidebarPanel(

               # Insert a button.
               actionButton(
                  inputId = "testButton", 
                  label = "Click Here"
               )
            )
         )
      )
   ),

   server <- shinyServer(function(input, output, session) {
   })
)

The CSS declarations in the header for .btn-default take effect for the actionButton if any of the following are done:

  1. the !important rule follows the CSS declarations in the header;
  2. the temp.css file is not included; or
  3. the header declaration is for the button name, using:

               HTML("#testButton {
                         background-color: rgb(40,110,5);
                         color: rgb(199,207,111);
                     }"
    

I've also tried including other selectors in the header CSS, such as .btn and .action-button.

Is there something else I should be doing? Is this a shiny problem?

like image 314
datatoolbox Avatar asked Oct 31 '22 11:10

datatoolbox


1 Answers

It will work the way you expect it if you put includeCSS("temp.css"), in your head tag. I am no expert but I think everything in tag$head is evaluated first and is overwritten by everything else.

like image 96
Alex Avatar answered Nov 15 '22 07:11

Alex