I'm wondering what are the ways to include some CSS and possibly Javascript that's needed for a Shiny module to work, without duplicating that code when multiple instances of the module are called? For example, say I have a module with this UI:
my_module_UI <- function(id) {
ns <- NS(id)
tagList(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "special_button.css")
),
actionButton(ns("button"), class = "special-button")
)
}
my_module_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
# Server-side functionality
}
)
}
This would work for styling the button appropriately. However, If I were to reuse this module at multiple times in the main app, the special_button.css
stylesheet would be added multiple times. If I were to use inline CSS, the specific rules would be multiplied for every button instance.
Is there a way to provide these stylesheets as a singleton, while still keeping it a part of the module definition? I'd like to have everything related to the module kept inside the module, instead of providing JS and CSS in the main app.
You can wrap the link tag in htmltools::singleton
:
my_module_UI <- function(id) {
ns <- NS(id)
tagList(
tags$head(
htmltools::singleton(
tags$link(rel = "stylesheet", type = "text/css", href = "special_button.css")
)
),
actionButton(ns("button"), label = "button", class = "special-button")
)
}