Skip to content

Instantly share code, notes, and snippets.

@jtrive84
Created August 14, 2024 18:53
Show Gist options
  • Save jtrive84/aac672a49dc2632f4c82f665210edcf9 to your computer and use it in GitHub Desktop.
Save jtrive84/aac672a49dc2632f4c82f665210edcf9 to your computer and use it in GitHub Desktop.
Function to change shiny bootstrap theme
sfThemeSelector = function() {
# --------------------------------------------------------------------------
# Allow dynamic update of bootstrap theme.
# --------------------------------------------------------------------------
div(
div(
selectInput(
"shinytheme-selector", "Select theme:",
c("default", shinythemes:::allThemes()),
selectize=FALSE
)
),
tags$script(
"$('#shinytheme-selector')
.on('change', function(el) {
var allThemes = $(this).find('option').map(function() {
if ($(this).val() === 'default')
return 'bootstrap';
else
return $(this).val();
});
// Find the current theme
var curTheme = el.target.value;
if (curTheme === 'default') {
curTheme = 'bootstrap';
curThemePath = 'shared/bootstrap/css/bootstrap.min.css';
} else {
curThemePath = 'shinythemes/css/' + curTheme + '.min.css';
}
// Find the <link> element with that has the bootstrap.css
var $link = $('link').filter(function() {
var theme = $(this).attr('href');
theme = theme.replace(/^.*\\//, '').replace(/(\\.min)?\\.css$/, '');
return $.inArray(theme, allThemes) !== -1;
});
// Set it to the correct path
$link.attr('href', curThemePath);
});"
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment