Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS style from bootswatch is not working on Shiny R

Tags:

css

shiny

I want to change the style of my Shiny app. I went here https://bootswatch.com/solar/ and downloaded the style .css file: "Solar A spin on Solarized".

library(shiny)

ui <- fluidPage(
    titlePanel(tags$i(h1(strong("My Panel Title"),style = "font-family: 'times'; font-size: 82px"))),align="center",    
    navbarPage(theme="bootstrap.min.css",title = 'Methods',
               tabPanel('One'),
               tabPanel('Two'),
               tabPanel('Three'),
               tabPanel('Four'))
)    

server <- function(input, output) {    

}   

shinyApp(ui = ui, server = server)

But as you can see the navigation bar looks weird:

nav bar with oversized title and small stacked links

How can I fix it?

like image 621
Laura Avatar asked Nov 03 '19 18:11

Laura


People also ask

How do I use Bootswatch themes?

The quickest way to use a Bootswatch theme is to use the BootstrapCDN. The CDN hosts Bootstrap as well as Bootswatch. Instead of having to download and store a copy of Bootstrap locally, you can use the CDN to host the CSS.

How do I change the theme on my shiny app?

If you want to quickly test out different themes with an application, you can simply add themeSelector() somewhere to the UI. This will add a select box which lets you choose the theme. It will change the theme without having to reload or restart your app. You can see the theme selector in action here.

What is R shiny without CSS?

So, R Shiny provides scalability, fast prototyping, and power, but without CSS, it looks like this: Are you convinced? Good! Let’s take a look at the possibilities. So what is CSS? It’s basically as old as HTML itself.

How to give your shiny app a special look with CSS?

You can give your Shiny app a special look with cascading style sheets (CSS). CSS is a style language which gives HTML documents a sophisticated look. Since the Shiny app user-interface (UI) is an HTML document, you can use CSS to control how you want your Shiny app to look.

How do I enable shiny theme in RStudio?

That context may be your current RStudio theme, an RMarkdown document, or a Shiny app. All that’s needed to enable this themeing is adding thematic_shiny () to our app… Note that we’re using the theme “darkly” here so difference is more obvious.

How to add custom CSS to plain tag objects in shiny?

When dealing with plain tag objects in Shiny, such as is we had declared the title of the app with an h2 () instead of titlePanel () you can place any custom CSS you want in the style argument. These styles just apply to that specific element. ...


1 Answers

The theme that you link to is a Bootstrap 4 theme, but Shiny uses Bootstrap 3. For compatible Bootswatch themes, see their v3 collection: https://bootswatch.com/3/.

For example, using the v3 Flatly theme via a CDN:

library(shiny)

ui <- fluidPage(
  titlePanel(tags$i(
    h1(strong("My Panel Title"), style = "font-family: 'times'; font-size: 82px")
  )),
  align = "center",
  navbarPage(
    theme = "https://stackpath.bootstrapcdn.com/bootswatch/3.4.1/flatly/bootstrap.min.css",
    title = 'Methods',
    tabPanel('One'),
    tabPanel('Two'),
    tabPanel('Three'),
    tabPanel('Four')
  )
)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

enter image description here

like image 138
Mikko Marttila Avatar answered Oct 19 '22 18:10

Mikko Marttila