Skip to content

Commit

Permalink
adding custom loader, fixing dechal/rechal wording issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nhall6 committed Aug 2, 2024
1 parent a7b6685 commit f8b96db
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 6 deletions.
8 changes: 4 additions & 4 deletions R/characterization-dechallengeRechallenge.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ characterizationDechallengeRechallengeServer <- function(
),
pctDechallengeSuccess = reactable::colDef(
header = withTooltip("% of Dechallenge Success",
"Among people with challenge outcomes, the percent of people with outcomes during dechallengeEvaluationWindow"),
"Among people with challenge outcomes, the percent of people without outcomes during the dechallengeEvaluationWindow"),
filterable = T,
format = reactable::colFormat(digits = 2, percent = T)
),
pctDechallengeFail = reactable::colDef(
header = withTooltip("% of Dechallenge Fail",
"Among people with challenge outcomes, the percent of people without outcomes during the dechallengeEvaluationWindow"),
"Among people with challenge outcomes, the percent of people with outcomes during dechallengeEvaluationWindow"),
filterable = T,
format = reactable::colFormat(digits = 2, percent = T)
),
Expand All @@ -231,13 +231,13 @@ characterizationDechallengeRechallengeServer <- function(
),
pctRechallengeSuccess = reactable::colDef(
header = withTooltip("% of Rechallenge Success",
"Percent of people with a new exposure era during which an outcome occurred, after the occurrence of an outcome during a prior exposure era"),
"Percent of people with a new exposure era during which an outcome did not occur, after the occurrence of an outcome during a prior exposure era"),
filterable = T,
format = reactable::colFormat(digits = 2, percent = T)
),
pctRechallengeFail = reactable::colDef(
header = withTooltip("% of Rechallenge Fail",
"Percent of people with a new exposure era during which an outcome did not occur, after the occurrence of an outcome during a prior exposure era"),
"Percent of people with a new exposure era during which an outcome occurred, after the occurrence of an outcome during a prior exposure era"),
filterable = T,
format = reactable::colFormat(digits = 2, percent = T)
)
Expand Down
10 changes: 8 additions & 2 deletions R/components-data-viewer.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,17 @@ resultTableViewer <- function(
shinycssloaders::withSpinner(
reactable::reactableOutput(
outputId = ns("resultData"),
width = "100%")
width = "100%"),
type = 3, size = 3, color.background = "#f19119", color = "#003142",
caption = shiny::div(
shiny::strong("Loading", style = "font-size: 20px; color: #333333; display: block; margin-bottom: 1px;"),
shiny::em("Please wait, results may take a few minutes...",
style = "font-size: 16px; color: #666666; display: block; margin-top: 0px;")
)
)
)
)
))
)
}


Expand Down
179 changes: 179 additions & 0 deletions R/helpers-customCSSLoader.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
buildSpinnerCustom <- function(
spinner_type = c("output", "page"),
ui_element,
type,
color,
size,
color.background,
custom.css,
proxy.height,
id,
image,
image.width,
image.height,
hide.ui,
caption
) {
spinner_type <- match.arg(spinner_type)
output_spinner <- (spinner_type == "output")

if (!is.null(image)) {
type <- 0
}
if (!type %in% 0:8) {
stop("shinycssloaders: `type` must be an integer between 0 and 8.")
}
if (grepl("rgb", color, fixed = TRUE)) {
stop("shinycssloaders: `color` should be given in hex format (#XXXXXX).")
}
if (is.character(custom.css)) {
stop("shinycssloaders: It looks like you provided a string to `custom.css`, but it needs to be either `TRUE` or `FALSE`. ",
"The actual CSS needs to added to the app's UI.")
}

if (is.null(id)) {
id <- paste0("spinner-", digest::digest(ui_element))
}

css_rules_tag <- get_spinner_css_tag(type, color, size, color.background, custom.css, id, image, caption, output_spinner)

if (!is.null(caption)) {
caption <- shiny::div(
id = paste0(id, "__caption"),
class = "shiny-spinner-caption",
caption
)
}

if (output_spinner) {
proxy_element <- get_proxy_element(ui_element, proxy.height, hide.ui)
} else {
proxy_element <- NULL
}

parent_cls <- "shiny-spinner-output-container"
if (hide.ui) {
parent_cls <- paste(parent_cls, "shiny-spinner-hideui")
}

child_cls <- "load-container"
if (output_spinner) {
child_cls <- paste(child_cls, "shiny-spinner-hidden")
}
if (is.null(image)) {
child_cls <- paste(child_cls, paste0("load", type))
}

if (is.null(image)) {
spinner_el <- shiny::div(id = id, class = "loader",
(if (type == 0) "" else "Loading..."))
} else {
spinner_el <- shiny::tags$img(id = id, src = image, alt = "Loading...",
width = image.width, height = image.height)
}

shiny::div(
css_rules_tag,
`data-spinner-id` = id,
class = parent_cls,
shiny::div(
class = child_cls,
spinner_el,
caption
),
proxy_element,
ui_element
)
}

#' Add a spinner that shows while an output is recalculating
#'
#' Add a spinner that automatically shows while an output is recalculating. You can also manually trigger the spinner
#' using [showSpinner()] and [hideSpinner()].\cr\cr
#' Most parameters can be set globally in order to use a default setting for all spinners in your Shiny app.
#' This can be done by setting an R option with the parameter's name prepended by `"spinner."`. For example, to set all spinners
#' to type=5 and color=#0dc5c1 by default, use `options(spinner.type = 5, spinner.color = "#0dc5c1")`. The following parameters
#' cannot be set globally: `ui_element`, `id`.\cr\cr
#' Use [showPageSpinner()] to show a spinner on the entire page instead of individual outputs.
#' @param ui_element A UI element that should be wrapped with a spinner when the corresponding output is being calculated.
#' @param type The type of spinner to use. Valid values are integers between 0-8 (0 means no spinner). Check out
#' You can also use your own custom image using the `image` parameter.
#' @param color The color of the spinner in hex format. Ignored if `image` is used.
#' @param size The size of the spinner, relative to its default size (default is 1, a size of 2 means twice as large).
#' Ignored if `image` is used.
#' @param color.background For certain spinners (type 2-3), you will need to specify the background color of the spinner.
#' Ignored if `image` is used.
#' @param custom.css Set to `TRUE` if you have your own custom CSS that you defined and you don't want the automatic CSS applied to the spinner.
#' Ignored if `image` is used.
#' @param proxy.height If the output UI doesn't specify the output height, you can set a proxy height. It defaults to "400px"
#' for outputs with undefined height. Ignored if `hide.ui` is set to `FALSE`.
#' @param id The HTML ID to use for the spinner. If you don't provide one, it will be generated automatically.
#' @param image The path or URL of the image to use if you want to use a custom image instead of a built-in spinner.
#' If `image` is provided, then `type` is ignored.
#' @param image.height The height for the custom image spinner, in pixels. If not provided, then the original
#' size of the image is used. Ignored if not using `image`.
#' @param image.width The width for the custom image spinner, in pixels. If not provided, then the original
#' size of the image is used. Ignored if not using `image`.
#' @param hide.ui By default, while an output is recalculating, the output UI is hidden and the spinner is visible instead.
#' Setting `hide.ui = FALSE` will result in the spinner showing up on top of the previous output UI.
#' @param caption Caption to display below the spinner or image (text or HTML). The caption's font color is determined
#' by the `color` parameter. Ignored if `type` is 1.
#' @seealso [showSpinner()], [hideSpinner()], [showPageSpinner()]
#' @examples
#' if (interactive()) {
#' library(shiny)
#'
#' shinyApp(
#' ui = fluidPage(
#' actionButton("go", "Go"),
#' withSpinner(plotOutput("plot"))
#' ),
#' server = function(input, output) {
#' output$plot <- renderPlot({
#' input$go
#' Sys.sleep(1.5)
#' plot(runif(10))
#' })
#' }
#' )
#' }
#' @export
withSpinnerCustom <- function(
ui_element,
type = getOption("spinner.type", default = 1),
color = getOption("spinner.color", default = "#0275D8"),
size = getOption("spinner.size", default = 1),
color.background = getOption("spinner.color.background"),
custom.css = getOption("spinner.custom.css", default = FALSE),
proxy.height = getOption("spinner.proxy.height"),
id = NULL,
image = getOption("spinner.image"),
image.width = getOption("spinner.image.width"),
image.height = getOption("spinner.image.height"),
hide.ui = getOption("spinner.hide.ui", default = TRUE),
caption = getOption("spinner.caption")
) {

if (!inherits(ui_element, "shiny.tag") && !inherits(ui_element, "shiny.tag.list")) {
stop("`ui_element` must be a Shiny tag", call. = FALSE)
}

spinner <- shinycssloaders::buildSpinner(
spinner_type = "output",
ui_element = ui_element,
type = type,
color = color,
size = size,
color.background = color.background,
custom.css = custom.css,
proxy.height = proxy.height,
id = id,
image = image,
image.width = image.width,
image.height = image.height,
hide.ui = hide.ui,
caption = caption
)

htmltools::attachDependencies(spinner, getDependencies())
}
Binary file added inst/images/download.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions man/withSpinnerCustom.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8b96db

Please sign in to comment.