| 1 |
`%||%` <- function(x, y) if (is.null(x)) y else x |
| 2 |
|
| 3 |
icon_list <- list( |
| 4 |
"bsicons" = setNames( |
| 5 |
paste0("bsicons::", bsicons:::icon_info$name), |
| 6 |
bsicons:::icon_info$name |
| 7 |
), |
| 8 |
"fontawesome" = setNames( |
| 9 |
paste0("fontawesome::", fontawesome::fa_metadata()$icon_names), |
| 10 |
fontawesome::fa_metadata()$icon_names |
| 11 |
) |
| 12 |
) |
| 13 |
|
| 14 |
resolve_icon <- function(name) { |
| 15 |
icon <- strsplit(name, "::", fixed = TRUE)[[1]] |
| 16 |
icon_fn <- switch( |
| 17 |
icon[1], |
| 18 |
bsicons = { |
| 19 |
ns <- "bsicons" |
| 20 |
fn <- "bs_icon" |
| 21 |
}, |
| 22 |
{ |
| 23 |
ns <- "fontawesome" |
| 24 |
fn <- "fa_i" |
| 25 |
} |
| 26 |
) |
| 27 |
rlang::call2(.ns = ns, fn, icon[2]) |
| 28 |
} |
| 29 |
|
| 30 |
ui_value_box_options <- function(id) { |
| 31 |
ns <- NS(id) |
| 32 |
|
| 33 |
conditional_panel <- function(condition, ...) { |
| 34 |
conditionalPanel(condition, ..., ns = ns) |
| 35 |
} |
| 36 |
|
| 37 |
init <- random_title_value() |
| 38 |
|
| 39 |
list( |
| 40 |
"title_value" = list( |
| 41 |
textInput(ns("title"), "Title", init$title), |
| 42 |
textInput(ns("value"), "Value", init$value), |
| 43 |
tagAppendAttributes( |
| 44 |
textAreaInput(ns("extra"), "Extra text (markdown allowed)", ""), |
| 45 |
class = "input-text-code" |
| 46 |
), |
| 47 |
actionButton(ns("random_stat"), "Random stat") |
| 48 |
), |
| 49 |
"theme_opts" = list( |
| 50 |
input_switch(ns("use_custom_colors"), "Use custom colors", FALSE), |
| 51 |
conditional_panel( |
| 52 |
"!input.use_custom_colors", |
| 53 |
ui_selextra(ns("theme"), "Theme") |
| 54 |
), |
| 55 |
conditional_panel( |
| 56 |
"input.use_custom_colors", |
| 57 |
colourpicker::colourInput( |
| 58 |
ns("foreground"), |
| 59 |
"Foreground", |
| 60 |
value = "#000000" |
| 61 |
), |
| 62 |
colourpicker::colourInput( |
| 63 |
ns("background"), |
| 64 |
"Background", |
| 65 |
value = "#FFFFFF" |
| 66 |
) |
| 67 |
), |
| 68 |
input_switch(ns("full_screen"), "Allow full screen", value = FALSE), |
| 69 |
input_switch(ns("fill"), "Fill vertical space", value = TRUE), |
| 70 |
input_switch(ns("fixed_height"), "Fixed height", value = FALSE), |
| 71 |
conditional_panel( |
| 72 |
"input.fixed_height", |
| 73 |
sliderInput( |
| 74 |
inputId = ns("height"), |
| 75 |
label = "Height", |
| 76 |
min = 100, |
| 77 |
max = 500, |
| 78 |
value = 150, |
| 79 |
post = "px", |
| 80 |
step = 10 |
| 81 |
) |
| 82 |
) |
| 83 |
), |
| 84 |
showcase = list( |
| 85 |
input_switch(ns("showcase"), "Include showcase", value = TRUE), |
| 86 |
conditional_panel( |
| 87 |
"input.showcase", |
| 88 |
radioButtons( |
| 89 |
ns("showcase_layout"), |
| 90 |
"Showcase Layout", |
| 91 |
c("left center", "top right", "bottom"), |
| 92 |
inline = TRUE |
| 93 |
), |
| 94 |
radioButtons( |
| 95 |
ns("showcase_item"), |
| 96 |
"Showcase Item", |
| 97 |
c("icon", "plot"), |
| 98 |
selected = "plot", |
| 99 |
inline = TRUE |
| 100 |
), |
| 101 |
conditional_panel( |
| 102 |
"input.showcase_item == 'plot'", |
| 103 |
selectInput( |
| 104 |
ns("showcase_plot_type"), |
| 105 |
"Plot Type", |
| 106 |
c("line", "bar", "box") |
| 107 |
), |
| 108 |
radioButtons( |
| 109 |
ns("showcase_plot_color"), |
| 110 |
"Plot Color", |
| 111 |
c("auto", "black", "white"), |
| 112 |
inline = TRUE |
| 113 |
) |
| 114 |
), |
| 115 |
conditional_panel( |
| 116 |
"input.showcase_item == 'icon'", |
| 117 |
ui_selextra(ns("showcase_icon"), "Icon") |
| 118 |
) |
| 119 |
) |
| 120 |
) |
| 121 |
) |
| 122 |
} |
| 123 |
|
| 124 |
ui_value_box_output <- function(id) { |
| 125 |
uiOutput(NS(id)("value_box"), class = "shiny-report-theme", fill = TRUE) |
| 126 |
} |
| 127 |
|
| 128 |
server_value_box <- function(input, output, session, ...) { |
| 129 |
ns <- session$ns |
| 130 |
|
| 131 |
theme <- module_selextra("theme", all_themes) |
| 132 |
showcase_icon <- module_selextra("showcase_icon", icon_list) |
| 133 |
|
| 134 |
random_plot <- reactive({ |
| 135 |
req(isolate(input$showcase), input$showcase_plot_type, plot_color()) |
| 136 |
random_plotly_plot(input$showcase_plot_type, plot_color()) |
| 137 |
}) |
| 138 |
|
| 139 |
plot_color <- reactiveVal("#FFFFFF") |
| 140 |
|
| 141 |
observe({ |
| 142 |
if (input$showcase_plot_color != "auto") { |
| 143 |
plot_color(input$showcase_plot_color) |
| 144 |
return() |
| 145 |
} |
| 146 |
|
| 147 |
if (input$use_custom_colors) { |
| 148 |
plot_color(input$foreground) |
| 149 |
return() |
| 150 |
} |
| 151 |
|
| 152 |
if (!is.null(input$value_box_fg_color)) { |
| 153 |
fg <- input$value_box_fg_color |
| 154 |
fg <- htmltools::parseCssColors(fg) |
| 155 |
plot_color(fg) |
| 156 |
} else { |
| 157 |
plot_color("#808080") |
| 158 |
} |
| 159 |
}) |
| 160 |
|
| 161 |
observe({ |
| 162 |
req(input$showcase, input$showcase_item == "icon") |
| 163 |
showcase_icon$shuffle() |
| 164 |
}) |
| 165 |
|
| 166 |
# ┌─ {bslib} ──────────────────────┐ |
| 167 |
# │ │ |
| 168 |
# │ value_box() │ |
| 169 |
# │ │ |
| 170 |
# └────────────────────────────────┘ |
| 171 |
|
| 172 |
value_box_args_impl <- reactive({ |
| 173 |
req(input$title, input$value) |
| 174 |
|
| 175 |
theme <- |
| 176 |
if (input$use_custom_colors) { |
| 177 |
rlang::call2( |
| 178 |
"value_box_theme", |
| 179 |
bg = input$background, |
| 180 |
fg = input$foreground |
| 181 |
) |
| 182 |
} else { |
| 183 |
if (nzchar(theme$value())) theme$value() |
| 184 |
} |
| 185 |
|
| 186 |
extra <- |
| 187 |
if (nzchar(input$extra)) { |
| 188 |
rlang::call2(.ns = "shiny", "markdown", input$extra) |
| 189 |
} |
| 190 |
|
| 191 |
showcase <- |
| 192 |
if (input$showcase) { |
| 193 |
req(showcase_icon$value()) |
| 194 |
|
| 195 |
switch( |
| 196 |
input$showcase_item, |
| 197 |
icon = resolve_icon(showcase_icon$value()), |
| 198 |
plot = "Your Plot" |
| 199 |
) |
| 200 |
} |
| 201 |
|
| 202 |
rlang::list2( |
| 203 |
title = input$title, |
| 204 |
value = input$value, |
| 205 |
if (!is.null(extra)) extra else rlang::missing_arg(), |
| 206 |
theme = theme, |
| 207 |
showcase = showcase, |
| 208 |
showcase_layout = input$showcase_layout, |
| 209 |
full_screen = input$full_screen, |
| 210 |
fill = input$fill, |
| 211 |
height = if (input$fixed_height) input$height |
| 212 |
) |
| 213 |
}) |
| 214 |
|
| 215 |
value_box_args <- debounce(value_box_args_impl, 250) |
| 216 |
|
| 217 |
value_box_call <- reactive({ |
| 218 |
req(input$title, input$value) |
| 219 |
|
| 220 |
rlang::call2("value_box", !!!value_box_args()) |
| 221 |
}) |
| 222 |
|
| 223 |
output$value_box <- renderUI({ |
| 224 |
req(value_box_call()) |
| 225 |
|
| 226 |
call <- value_box_call() |
| 227 |
|
| 228 |
is_showcase_plot <- isolate(input$showcase && input$showcase_item == "plot") |
| 229 |
|
| 230 |
if (is_showcase_plot) { |
| 231 |
call <- rlang::call_modify(call, showcase = random_plot()) |
| 232 |
} |
| 233 |
|
| 234 |
rlang::eval_bare(call) |
| 235 |
}) |
| 236 |
|
| 237 |
observeEvent(input$showcase_item, { |
| 238 |
updateCheckboxInput( |
| 239 |
session, |
| 240 |
"full_screen", |
| 241 |
value = input$showcase_item == "plot" |
| 242 |
) |
| 243 |
}) |
| 244 |
|
| 245 |
observeEvent(input$random_stat, { |
| 246 |
random <- random_title_value() |
| 247 |
updateTextInput(session, "title", value = random$title) |
| 248 |
updateTextInput(session, "value", value = random$value) |
| 249 |
}) |
| 250 |
|
| 251 |
observeEvent(input$shuffle_showcase_icon, { |
| 252 |
new <- sample(bsicons:::icon_info$name, 1) |
| 253 |
updateSelectInput( |
| 254 |
session, |
| 255 |
"showcase_icon", |
| 256 |
selected = paste0("bsicons::", new) |
| 257 |
) |
| 258 |
}) |
| 259 |
|
| 260 |
list( |
| 261 |
code = value_box_call, |
| 262 |
theme = theme, |
| 263 |
showcase_icon = showcase_icon, |
| 264 |
random_stat = function() { |
| 265 |
random <- random_title_value() |
| 266 |
updateTextInput(session, "title", value = random$title) |
| 267 |
updateTextInput(session, "value", value = random$value) |
| 268 |
}, |
| 269 |
set_showcase_layout = function(layout) { |
| 270 |
updateRadioButtons(session, "showcase_layout", selected = layout) |
| 271 |
}, |
| 272 |
set_showcase_item = function(item) { |
| 273 |
updateRadioButtons(session, "showcase_item", selected = item) |
| 274 |
} |
| 275 |
) |
| 276 |
} |
| 277 |
|
| 278 |
module_value_box <- function(id) { |
| 279 |
moduleServer(id, server_value_box) |
| 280 |
} |