OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Shiny modules - including module CSS and JS without repeating

  • Thread starter Thread starter mkranj
  • Start date Start date
M

mkranj

Guest
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:

Code:
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.
<p>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:</p>
<pre><code>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
}
)
}

</code></pre>
<p>This would work for styling the button appropriately. However, If I were to reuse this module at multiple times in the main app, the <code>special_button.css</code> stylesheet would be added multiple times. If I were to use inline CSS, the specific rules would be multiplied for every button instance.<br />
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.</p>
Continue reading...
 
Top