Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions R/mock-session.R
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,29 @@ MockShinySession <- R6Class(
})
private$flush()
},

#' @description Removes inputs from the `session$inputs` object and flushes
#' the reactives.
#' @param inputIds Character vector of input ids to remove.
#' @examples
#' \dontrun{
#' session$setInputs(x=1, y=2)
#' session$removeInputs("x")
#' }
removeInputs = function(inputIds) {
is_clientdata <- grepl("^.clientdata_", inputIds)
if (any(is_clientdata)) {
abort(
"Cannot remove clientData inputs: ",
paste(inputIds[is_clientdata], collapse = ", ")
)
}

for (inputId in inputIds) {
private$.input$remove(inputId)
}
private$flush()
},

#' @description An internal method which shouldn't be used by others.
#' Schedules `callback` for execution after some number of `millis`
Expand Down
87 changes: 65 additions & 22 deletions R/reactives.R
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,6 @@ ReactiveValues <- R6Class(
# invalidate all deps of `key`

domain <- getDefaultReactiveDomain()
hidden <- substr(key, 1, 1) == "."

key_exists <- .values$containsKey(key)

if (key_exists && !isTRUE(force) && .dedupe && identical(.values$get(key), value)) {
Expand All @@ -420,26 +418,15 @@ ReactiveValues <- R6Class(
.dependents$get(key)$invalidate()
}

# only invalidate if there are deps
if (!key_exists && isTRUE(.hasRetrieved$names)) {
rLog$valueChangeNames(.reactId, .values$keys(), domain)
.namesDeps$invalidate()
# invalidate names() or toList() if needed
if (!key_exists) {
private$invalidateNames(domain)
}

if (hidden) {
if (isTRUE(.hasRetrieved$asListAll)) {
rLog$valueChangeAsListAll(.reactId, .values$values(), domain)
.allValuesDeps$invalidate()
}
} else {
if (isTRUE(.hasRetrieved$asList)) {
react_vals <- .values$values()
react_vals <- react_vals[!grepl("^\\.", base::names(react_vals))]
# leave as is. both object would be registered to the listening object
rLog$valueChangeAsList(.reactId, react_vals, domain)
.valuesDeps$invalidate()
}
}
private$invalidateAsListAny(
all.names = substr(key, 1, 1) == ".",
domain = domain
)

invisible()
},
Expand All @@ -451,6 +438,21 @@ ReactiveValues <- R6Class(
})
},

remove = function(key) {
stopifnot(rlang::is_string(key))

if (!self$.values$containsKey(key)) {
return(invisible())
}

value <- self$.values$get(key)
self$.values$remove(key)
self$.nameOrder <- setdiff(self$.nameOrder, key)
private$invalidateNames()
private$invalidateAsListAny(all.names = substr(key, 1, 1) == ".")
invisible(value)
},

names = function() {
if (!isTRUE(.hasRetrieved$names)) {
domain <- getDefaultReactiveDomain()
Expand Down Expand Up @@ -529,7 +531,47 @@ ReactiveValues <- R6Class(

return(listValue)
}
),
private = list(
invalidateNames = function(domain = getDefaultReactiveDomain()) {
if (!isTRUE(self$.hasRetrieved$names)) {
return(invisible())
}
rLog$valueChangeNames(self$.reactId, self$.values$keys(), domain)
self$.namesDeps$invalidate()
},

invalidateAsListAny = function(
all.names,
domain = getDefaultReactiveDomain()
) {
if (isTRUE(all.names)) {
private$invalidateAsListAll(domain)
} else {
private$invalidateAsList(domain)
}
},

invalidateAsListAll = function(domain = getDefaultReactiveDomain()) {
if (!isTRUE(self$.hasRetrieved$asListAll)) {
return(invisible())
}

rLog$valueChangeAsListAll(self$.reactId, self$.values$values(), domain)
self$.allValuesDeps$invalidate()
},

invalidateAsList = function(domain = getDefaultReactiveDomain()) {
if (!isTRUE(self$.hasRetrieved$asList)) {
return(invisible())
}

react_vals <- self$.values$values()
react_vals <- react_vals[!grepl("^\\.", base::names(react_vals))]
# leave as is. both object would be registered to the listening object
rLog$valueChangeAsList(self$.reactId, react_vals, domain)
self$.valuesDeps$invalidate()
}
)
)

Expand Down Expand Up @@ -599,14 +641,15 @@ checkName <- function(x) {
# @param ns A namespace function (either `identity` or `NS(namespace)`)
.createReactiveValues <- function(values = NULL, readonly = FALSE,
ns = identity) {

structure(
list(
impl = values,
readonly = readonly,
ns = ns
),
class='reactivevalues'
class='reactivevalues',
remove = function(key) values$remove(key)
)
}

Expand Down
13 changes: 13 additions & 0 deletions R/shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -2159,6 +2159,19 @@ ShinySession <- R6Class(
self$cycleStartAction(doManageInputs)
}
},
removeInputs = function(inputIds) {
is_clientdata <- grepl("^.clientdata_", inputIds)
if (any(is_clientdata)) {
abort(
"Cannot remove clientData inputs: ",
paste(inputIds[is_clientdata], collapse = ", ")
)
}

for (inputId in inputIds) {
private$.input$remove(inputId)
}
},
outputOptions = function(name, ...) {
# If no name supplied, return the list of options for all outputs
if (is.null(name))
Expand Down
39 changes: 39 additions & 0 deletions man/MockShinySession.Rd

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

Loading