| 1 |
#' Return the URL for a Shiny theme |
| 2 |
#' |
| 3 |
#' The result of this function should be used as the \code{theme} argument for |
| 4 |
#' \code{\link[shiny]{bootstrapPage}}, \code{\link[shiny]{fluidPage}}, |
| 5 |
#' \code{\link[shiny]{navbarPage}}, or \code{\link[shiny]{fixedPage}}. |
| 6 |
#' |
| 7 |
#' |
| 8 |
#' @param theme Name of a theme. |
| 9 |
#' |
| 10 |
#' @examples |
| 11 |
#' \dontrun{ |
| 12 |
#' shinyApp( |
| 13 |
#' ui = fluidPage(theme = shinytheme("united"), |
| 14 |
#' ... |
| 15 |
#' ), |
| 16 |
#' server = function(input, output) { } |
| 17 |
#' ) |
| 18 |
#' } |
| 19 |
#' @seealso The main \link{shinythemes} page for information about available |
| 20 |
#' themes and more detailed examples. |
| 21 |
#' @export |
| 22 |
shinytheme <- function(theme = NULL) { |
| 23 |
# Check that theme exists |
| 24 |
if (is.null(theme) || !theme %in% allThemes()) { |
| 25 |
stop(theme, " is not an available theme. Valid themes are: ", |
| 26 |
paste(allThemes(), collapse = ", "), ".") |
| 27 |
} |
| 28 |
|
| 29 |
paste0("shinythemes/css/", theme, ".min.css") |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
allThemes <- function() { |
| 34 |
themes <- dir(system.file("shinythemes/css", package = "shinythemes"), |
| 35 |
"*.min.css") |
| 36 |
sub(".min.css", "", themes) |
| 37 |
} |
| 38 |
|
| 39 |
#' Add a theme selector widget in a floating panel |
| 40 |
#' |
| 41 |
#' This adds a widget for selecting the theme, in a floating panel. It is meant |
| 42 |
#' for use during the development phase of a Shiny application. |
| 43 |
#' |
| 44 |
#' This can be inserted anywhere inside of the application, although if it is |
| 45 |
#' put inside a tab, it will be visible only when that tab is showing. For it to |
| 46 |
#' show at all times, it must be used outside a tab. |
| 47 |
#' |
| 48 |
#' @examples |
| 49 |
#' |
| 50 |
#' if (interactive()) { |
| 51 |
#' # themeSelector can be inserted anywhere in the app. |
| 52 |
#' shinyApp( |
| 53 |
#' ui = fluidPage( |
| 54 |
#' shinythemes::themeSelector(), |
| 55 |
#' sidebarPanel( |
| 56 |
#' textInput("txt", "Text input:", "text here"), |
| 57 |
#' sliderInput("slider", "Slider input:", 1, 100, 30), |
| 58 |
#' actionButton("action", "Button"), |
| 59 |
#' actionButton("action2", "Button2", class = "btn-primary") |
| 60 |
#' ), |
| 61 |
#' mainPanel( |
| 62 |
#' tabsetPanel( |
| 63 |
#' tabPanel("Tab 1"), |
| 64 |
#' tabPanel("Tab 2") |
| 65 |
#' ) |
| 66 |
#' ) |
| 67 |
#' ), |
| 68 |
#' server = function(input, output) {} |
| 69 |
#' ) |
| 70 |
#' |
| 71 |
#' |
| 72 |
#' # If this is used with a navbarPage() or other type of page where there is not a |
| 73 |
#' # good place to put it where it is outside of all tabs, you can wrap the entire |
| 74 |
#' # page in tagList() and make the themeSelector a sibling of the page. |
| 75 |
#' shinyApp( |
| 76 |
#' ui = tagList( |
| 77 |
#' shinythemes::themeSelector(), |
| 78 |
#' navbarPage( |
| 79 |
#' "Theme test", |
| 80 |
#' tabPanel("Navbar 1", |
| 81 |
#' sidebarPanel( |
| 82 |
#' textInput("txt", "Text input:", "text here"), |
| 83 |
#' sliderInput("slider", "Slider input:", 1, 100, 30), |
| 84 |
#' actionButton("action", "Button"), |
| 85 |
#' actionButton("action2", "Button2", class = "btn-primary") |
| 86 |
#' ), |
| 87 |
#' mainPanel( |
| 88 |
#' tabsetPanel( |
| 89 |
#' tabPanel("Tab 1"), |
| 90 |
#' tabPanel("Tab 2") |
| 91 |
#' ) |
| 92 |
#' ) |
| 93 |
#' ), |
| 94 |
#' tabPanel("Navbar 2") |
| 95 |
#' ) |
| 96 |
#' ), |
| 97 |
#' server = function(input, output) {} |
| 98 |
#' ) |
| 99 |
#' } |
| 100 |
#' @importFrom shiny div tags selectInput |
| 101 |
#' @export |
| 102 |
themeSelector <- function() { |
| 103 |
shiny::fixedPanel( |
| 104 |
top = "15px", |
| 105 |
right = "15px", |
| 106 |
draggable = TRUE, |
| 107 |
style = "width: 250px; z-index: 100000;", |
| 108 |
div(class = "panel panel-danger", |
| 109 |
style = "box-shadow: 5px 5px 15px -5px rgba(0, 0, 0, 0.3);", |
| 110 |
div(class = "panel-heading", "Select theme:"), |
| 111 |
div(class = "panel-body", |
| 112 |
selectInput("shinytheme-selector", NULL, |
| 113 |
c("default", allThemes()), |
| 114 |
selectize = FALSE |
| 115 |
) |
| 116 |
) |
| 117 |
), |
| 118 |
tags$script( |
| 119 |
"$('#shinytheme-selector') |
| 120 |
.on('change', function(el) { |
| 121 |
var allThemes = $(this).find('option').map(function() { |
| 122 |
if ($(this).val() === 'default') |
| 123 |
return 'bootstrap'; |
| 124 |
else |
| 125 |
return $(this).val(); |
| 126 |
}); |
| 127 |
|
| 128 |
// Find the current theme |
| 129 |
var curTheme = el.target.value; |
| 130 |
if (curTheme === 'default') { |
| 131 |
curTheme = 'bootstrap'; |
| 132 |
curThemePath = 'shared/bootstrap/css/bootstrap.min.css'; |
| 133 |
} else { |
| 134 |
curThemePath = 'shinythemes/css/' + curTheme + '.min.css'; |
| 135 |
} |
| 136 |
|
| 137 |
// Find the <link> element with that has the bootstrap.css |
| 138 |
var $link = $('link').filter(function() { |
| 139 |
var theme = $(this).attr('href'); |
| 140 |
theme = theme.replace(/^.*\\//, '').replace(/(\\.min)?\\.css$/, ''); |
| 141 |
return $.inArray(theme, allThemes) !== -1; |
| 142 |
}); |
| 143 |
|
| 144 |
// Set it to the correct path |
| 145 |
$link.attr('href', curThemePath); |
| 146 |
});" |
| 147 |
) |
| 148 |
) |
| 149 |
} |