Skip to content

Commit

Permalink
Moves {shiny} to imports and updates examples/vignettes (#546)
Browse files Browse the repository at this point in the history
# Pull Request

- Fixes #529

ℹ️ Note: The package imports all functions from `{shiny}` via `@import
shiny` {roxygen2} tag, however, there's a mixed bag of prefixed
(`shiny::fun(...)`) and non-prefixed calls (`fun(...)`) in the
(functions') code. ~~Those were not modified.~~ (see discussion)

#### Changes description

- Remove occurences of `shiny::` from `R/` code
- Moved `{shiny}` to Imports
- Replaced shiny functions in examples by
  - add `library(shiny)` when building a shiny app to run
  - ~~`fun` -> `shiny::fun` for others~~
- Corrects vignettes accordingly

---------

Signed-off-by: André Veríssimo <[email protected]>
Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Aleksander Chlebowski <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2024
1 parent 3b86f38 commit 875f67e
Show file tree
Hide file tree
Showing 52 changed files with 173 additions and 104 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ URL: https://insightsengineering.github.io/teal.slice/,
https://github.com/insightsengineering/teal.slice/
BugReports: https://github.com/insightsengineering/teal.slice/issues
Depends:
R (>= 4.0),
shiny (>= 1.6.0)
R (>= 4.0)
Imports:
bslib (>= 0.4.0),
checkmate (>= 2.1.0),
Expand All @@ -42,6 +41,7 @@ Imports:
methods,
plotly (>= 4.9.2.2),
R6 (>= 2.2.0),
shiny (>= 1.6.0),
shinycssloaders (>= 1.0.0),
shinyjs,
shinyWidgets (>= 0.6.2),
Expand Down
2 changes: 2 additions & 0 deletions R/FilterPanelAPI.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' filter panel API.
#'
#' @examples
#' library(shiny)
#'
#' fd <- init_filtered_data(list(iris = iris))
#' fpa <- FilterPanelAPI$new(fd)
#'
Expand Down
4 changes: 4 additions & 0 deletions R/FilterState-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#' # use non-exported function from teal.slice
#' init_filter_state <- getFromNamespace("init_filter_state", "teal.slice")
#'
#' library(shiny)
#'
#' filter_state <- init_filter_state(
#' x = c(1:10, NA, Inf),
#' x_reactive = reactive(c(1:10, NA, Inf)),
Expand All @@ -37,6 +39,8 @@
#'
#' isolate(filter_state$get_call())
#'
#' # working filter in an app
#'
#' ui <- fluidPage(
#' filter_state$ui(id = "app"),
#' verbatimTextOutput("call")
Expand Down
24 changes: 12 additions & 12 deletions R/FilterState.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ FilterState <- R6::R6Class( # nolint
private$extract_type <- extract_type

# Set state properties.
if (is.null(shiny::isolate(slice$keep_na)) && anyNA(x)) slice$keep_na <- TRUE
if (is.null(isolate(slice$keep_na)) && anyNA(x)) slice$keep_na <- TRUE
private$teal_slice <- slice
# Obtain variable label.
varlabel <- attr(x, "label")
Expand Down Expand Up @@ -134,7 +134,7 @@ FilterState <- R6::R6Class( # nolint
#' @param ... additional arguments
#'
print = function(...) {
cat(shiny::isolate(self$format(...)))
cat(isolate(self$format(...)))
},

#' @description
Expand All @@ -152,7 +152,7 @@ FilterState <- R6::R6Class( # nolint
logger::log_warn("attempt to set state on fixed filter aborted id: { private$get_id() }")
} else {
logger::log_trace("{ class(self)[1] }$set_state setting state of filter id: { private$get_id() }")
shiny::isolate({
isolate({
if (!is.null(state$selected)) {
private$set_selected(state$selected)
}
Expand Down Expand Up @@ -429,7 +429,7 @@ FilterState <- R6::R6Class( # nolint
private$get_id()
)
)
shiny::isolate({
isolate({
value <- private$cast_and_validate(value)
value <- private$check_length(value)
value <- private$remove_out_of_bounds_values(value)
Expand Down Expand Up @@ -497,29 +497,29 @@ FilterState <- R6::R6Class( # nolint
# Returns dataname.
# @return `character(1)`
get_dataname = function() {
shiny::isolate(private$teal_slice$dataname)
isolate(private$teal_slice$dataname)
},

# @description
# Get variable name.
# @return `character(1)`
get_varname = function() {
shiny::isolate(private$teal_slice$varname)
isolate(private$teal_slice$varname)
},

# @description
# Get id of the teal_slice.
# @return `character(1)`
get_id = function() {
shiny::isolate(private$teal_slice$id)
isolate(private$teal_slice$id)
},

# @description
# Get allowed values from `FilterState`.
# @return
# Vector describing the available choices. Return type depends on the `FilterState` subclass.
get_choices = function() {
shiny::isolate(private$teal_slice$choices)
isolate(private$teal_slice$choices)
},

# @description
Expand Down Expand Up @@ -547,19 +547,19 @@ FilterState <- R6::R6Class( # nolint
# Check whether this filter is fixed (cannot be changed).
# @return `logical(1)`
is_fixed = function() {
shiny::isolate(isTRUE(private$teal_slice$fixed))
isolate(isTRUE(private$teal_slice$fixed))
},

# Check whether this filter is anchored (cannot be removed).
# @return `logical(1)`
is_anchored = function() {
shiny::isolate(isTRUE(private$teal_slice$anchored))
isolate(isTRUE(private$teal_slice$anchored))
},

# Check whether this filter is capable of selecting multiple values.
# @return `logical(1)`
is_multiple = function() {
shiny::isolate(isTRUE(private$teal_slice$multiple))
isolate(isTRUE(private$teal_slice$multiple))
},

# other ----
Expand Down Expand Up @@ -712,7 +712,7 @@ FilterState <- R6::R6Class( # nolint
keep_na_ui = function(id) {
ns <- NS(id)
if (private$na_count > 0) {
shiny::isolate({
isolate({
countmax <- private$na_count
countnow <- private$filtered_na_count()
ui_input <- checkboxInput(
Expand Down
14 changes: 8 additions & 6 deletions R/FilterStateChoices.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' include_js_files <- getFromNamespace("include_js_files", "teal.slice")
#' ChoicesFilterState <- getFromNamespace("ChoicesFilterState", "teal.slice")
#'
#' library(shiny)
#'
#' filter_state <- ChoicesFilterState$new(
#' x = c(LETTERS, NA),
#' slice = teal_slice(varname = "var", dataname = "data")
Expand Down Expand Up @@ -146,7 +148,7 @@ ChoicesFilterState <- R6::R6Class( # nolint
x_reactive = reactive(NULL),
slice,
extract_type = character(0)) {
shiny::isolate({
isolate({
checkmate::assert(
is.character(x),
is.factor(x),
Expand Down Expand Up @@ -333,7 +335,7 @@ ChoicesFilterState <- R6::R6Class( # nolint
sprintf("Selection: %s is not a vector of length one. ", toString(values, width = 360)),
"Maintaining previous selection."
)
values <- shiny::isolate(private$get_selected())
values <- isolate(private$get_selected())
}
values
},
Expand All @@ -359,7 +361,7 @@ ChoicesFilterState <- R6::R6Class( # nolint
ns <- NS(id)

# we need to isolate UI to not rettrigger renderUI
shiny::isolate({
isolate({
countsmax <- private$choices_counts
countsnow <- if (!is.null(private$x_reactive())) {
unname(table(factor(private$x_reactive(), levels = private$get_choices())))
Expand Down Expand Up @@ -444,7 +446,7 @@ ChoicesFilterState <- R6::R6Class( # nolint
}

# update should be based on a change of counts only
shiny::isolate({
isolate({
if (private$is_checkboxgroup()) {
updateCountBars(
inputId = "labels",
Expand Down Expand Up @@ -569,10 +571,10 @@ ChoicesFilterState <- R6::R6Class( # nolint
}
countsmax <- private$choices_counts

ind <- private$get_choices() %in% shiny::isolate(private$get_selected())
ind <- private$get_choices() %in% isolate(private$get_selected())
countBars(
inputId = session$ns("labels"),
choices = shiny::isolate(private$get_selected()),
choices = isolate(private$get_selected()),
countsnow = countsnow[ind],
countsmax = countsmax[ind]
)
Expand Down
8 changes: 5 additions & 3 deletions R/FilterStateDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' include_js_files <- getFromNamespace("include_js_files", "teal.slice")
#' DateFilterState <- getFromNamespace("DateFilterState", "teal.slice")
#'
#' library(shiny)
#'
#' filter_state <- DateFilterState$new(
#' x = c(Sys.Date() + seq(1:10), NA),
#' slice = teal_slice(varname = "x", dataname = "data"),
Expand Down Expand Up @@ -140,7 +142,7 @@ DateFilterState <- R6::R6Class( # nolint
x_reactive = reactive(NULL),
slice,
extract_type = character(0)) {
shiny::isolate({
isolate({
checkmate::assert_date(x)
checkmate::assert_class(x_reactive, "reactive")

Expand Down Expand Up @@ -275,7 +277,7 @@ DateFilterState <- R6::R6Class( # nolint
# @param id (`character(1)`) `shiny` module instance id.
ui_inputs = function(id) {
ns <- NS(id)
shiny::isolate({
isolate({
div(
div(
class = "flex",
Expand Down Expand Up @@ -423,7 +425,7 @@ DateFilterState <- R6::R6Class( # nolint
tagList(
tags$span(
class = "filter-card-summary-value",
shiny::HTML(min, "&ndash;", max)
HTML(min, "&ndash;", max)
),
tags$span(
class = "filter-card-summary-controls",
Expand Down
8 changes: 5 additions & 3 deletions R/FilterStateDatettime.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#' include_js_files <- getFromNamespace("include_js_files", "teal.slice")
#' DatetimeFilterState <- getFromNamespace("DatetimeFilterState", "teal.slice")
#'
#' library(shiny)
#'
#' filter_state <- DatetimeFilterState$new(
#' x = c(Sys.time() + seq(0, by = 3600, length.out = 10), NA),
#' slice = teal_slice(varname = "x", dataname = "data"),
Expand Down Expand Up @@ -150,7 +152,7 @@ DatetimeFilterState <- R6::R6Class( # nolint
x_reactive = reactive(NULL),
extract_type = character(0),
slice) {
shiny::isolate({
isolate({
checkmate::assert_multi_class(x, c("POSIXct", "POSIXlt"))
checkmate::assert_class(x_reactive, "reactive")

Expand Down Expand Up @@ -315,7 +317,7 @@ DatetimeFilterState <- R6::R6Class( # nolint
ui_inputs = function(id) {
ns <- NS(id)

shiny::isolate({
isolate({
ui_input_1 <- shinyWidgets::airDatepickerInput(
inputId = ns("selection_start"),
value = private$get_selected()[1],
Expand Down Expand Up @@ -536,7 +538,7 @@ DatetimeFilterState <- R6::R6Class( # nolint
tagList(
tags$span(
class = "filter-card-summary-value",
shiny::HTML(min, "&ndash;", max)
HTML(min, "&ndash;", max)
),
tags$span(
class = "filter-card-summary-controls",
Expand Down
6 changes: 4 additions & 2 deletions R/FilterStateEmpty.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#' include_js_files <- getFromNamespace("include_js_files", "teal.slice")
#' EmptyFilterState <- getFromNamespace("EmptyFilterState", "teal.slice")
#'
#' library(shiny)
#'
#' filter_state <- EmptyFilterState$new(
#' x = NA,
#' slice = teal_slice(varname = "x", dataname = "data"),
Expand Down Expand Up @@ -58,7 +60,7 @@ EmptyFilterState <- R6::R6Class( # nolint
x_reactive = reactive(NULL),
extract_type = character(0),
slice) {
shiny::isolate({
isolate({
super$initialize(
x = x,
x_reactive = x_reactive,
Expand Down Expand Up @@ -125,7 +127,7 @@ EmptyFilterState <- R6::R6Class( # nolint
#
ui_inputs = function(id) {
ns <- NS(id)
shiny::isolate({
isolate({
div(
tags$span("Variable contains missing values only"),
private$keep_na_ui(ns("keep_na"))
Expand Down
13 changes: 7 additions & 6 deletions R/FilterStateExpr.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#' filter_state$get_call()
#'
#' # working filter in an app
#' library(shiny)
#' library(shinyjs)
#'
#' ui <- fluidPage(
Expand Down Expand Up @@ -99,7 +100,7 @@ FilterStateExpr <- R6::R6Class( # nolint
#' @return `NULL`, invisibly.
#'
print = function(...) {
cat(shiny::isolate(self$format(...)))
cat(isolate(self$format(...)))
},

#' @description
Expand Down Expand Up @@ -136,7 +137,7 @@ FilterStateExpr <- R6::R6Class( # nolint
#' @return `call` or `NULL`
#'
get_call = function(dataname) {
shiny::isolate(str2lang(private$teal_slice$expr))
isolate(str2lang(private$teal_slice$expr))
},

#' @description
Expand Down Expand Up @@ -189,7 +190,7 @@ FilterStateExpr <- R6::R6Class( # nolint
#' id of the `FilterStates` card container.
ui = function(id, parent_id = "cards") {
ns <- NS(id)
shiny::isolate({
isolate({
tags$div(
id = id,
class = "panel filter-card",
Expand Down Expand Up @@ -238,13 +239,13 @@ FilterStateExpr <- R6::R6Class( # nolint
# Get id of the teal_slice.
# @return `character(1)`
get_id = function() {
shiny::isolate(private$teal_slice$id)
isolate(private$teal_slice$id)
},

# Check whether this filter is anchored (cannot be removed).
# @return `logical(1)`
is_anchored = function() {
shiny::isolate(isTRUE(private$teal_slice$anchored))
isolate(isTRUE(private$teal_slice$anchored))
},

# @description
Expand All @@ -268,7 +269,7 @@ FilterStateExpr <- R6::R6Class( # nolint
)
},
content_summary = function() {
shiny::isolate(private$teal_slice$expr)
isolate(private$teal_slice$expr)
}
)
)
Loading

0 comments on commit 875f67e

Please sign in to comment.