Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add class to <html> tag at beginning of page in Shiny App

Tags:

r

shiny

I'd like to add a class to the tag at the beginning of the HTML rendered in the shiny application.

For example, use this code to generate a basic app:

library(shiny)
ui <- basicPage()
server <- function(input, output){}
shinyApp(ui, server)

If you inspect the page, you see the html tag at the beginning as:

<html style="overflow: hidden;">

I'd like to make it something like this:

<html class="myclass" style = "overflow: hidden;">

Is there a way to do this without using javascript?

like image 205
Sam Helmich Avatar asked Oct 15 '25 04:10

Sam Helmich


2 Answers

You can use tags$html for this. tags contains many more other HTMLtags, you can look for them ?shiny::tags

library(shiny)
ui <- basicPage(
  tags$html(class="myclass", style = "overflow: hidden;")
)
server <- function(input, output){}
shinyApp(ui, server)

In answer to a comment:

This doesn't quite do what I want it to. This will add a new html tag inside the page, whereas I want to edit the one that's automatically generated

Are you sure? When I inspect the HTML code I can see that it modified the < html > tag:

enter image description here

like image 105
Michal Majka Avatar answered Oct 17 '25 19:10

Michal Majka


You can use the shinyjs package, it has a function called addClass(). It does use javascript under the hood so I'm not sure if this is fine for you or not. It'd be like this:

library(shiny)
ui <- basicPage(shinyjs::useShinyjs())
server <- function(input, output){
  shinyjs::addClass(class = "myclass", selector = "html")
}
shinyApp(ui, server)
like image 27
DeanAttali Avatar answered Oct 17 '25 18:10

DeanAttali