| 1 |
#' Insert and remove UI objects |
| 2 |
#' |
| 3 |
#' These functions allow you to dynamically add and remove arbitrary UI |
| 4 |
#' into your app, whenever you want, as many times as you want. |
| 5 |
#' Unlike [renderUI()], the UI generated with `insertUI()` is persistent: |
| 6 |
#' once it's created, it stays there until removed by `removeUI()`. Each |
| 7 |
#' new call to `insertUI()` creates more UI objects, in addition to |
| 8 |
#' the ones already there (all independent from one another). To |
| 9 |
#' update a part of the UI (ex: an input object), you must use the |
| 10 |
#' appropriate `render` function or a customized `reactive` |
| 11 |
#' function. |
| 12 |
#' |
| 13 |
#' It's particularly useful to pair `removeUI` with `insertUI()`, but there is |
| 14 |
#' no restriction on what you can use it on. Any element that can be selected |
| 15 |
#' through a jQuery selector can be removed through this function. |
| 16 |
#' |
| 17 |
#' @param selector A string that is accepted by jQuery's selector |
| 18 |
#' (i.e. the string `s` to be placed in a `$(s)` jQuery call). |
| 19 |
#' |
| 20 |
#' For `insertUI()` this determines the element(s) relative to which you |
| 21 |
#' want to insert your UI object. For `removeUI()` this determine the |
| 22 |
#' element(s) to be removed. If you want to remove a Shiny input or output, |
| 23 |
#' note that many of these are wrapped in `<div>`s, so you may need to use a |
| 24 |
#' somewhat complex selector --- see the Examples below. (Alternatively, you |
| 25 |
#' could also wrap the inputs/outputs that you want to be able to remove |
| 26 |
#' easily in a `<div>` with an id.) |
| 27 |
#' @param where Where your UI object should go relative to the selector: |
| 28 |
#' \describe{ |
| 29 |
#' \item{`beforeBegin`}{Before the selector element itself} |
| 30 |
#' \item{`afterBegin`}{Just inside the selector element, before its |
| 31 |
#' first child} |
| 32 |
#' \item{`beforeEnd`}{Just inside the selector element, after its |
| 33 |
#' last child (default)} |
| 34 |
#' \item{`afterEnd`}{After the selector element itself} |
| 35 |
#' } |
| 36 |
#' Adapted from <https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML>. |
| 37 |
#' @param ui The UI object you want to insert. This can be anything that |
| 38 |
#' you usually put inside your apps's `ui` function. If you're inserting |
| 39 |
#' multiple elements in one call, make sure to wrap them in either a |
| 40 |
#' `tagList()` or a `tags$div()` (the latter option has the |
| 41 |
#' advantage that you can give it an `id` to make it easier to |
| 42 |
#' reference or remove it later on). If you want to insert raw html, use |
| 43 |
#' `ui = HTML()`. |
| 44 |
#' @param multiple In case your selector matches more than one element, |
| 45 |
#' `multiple` determines whether Shiny should insert the UI object |
| 46 |
#' relative to all matched elements or just relative to the first |
| 47 |
#' matched element (default). |
| 48 |
#' @param immediate Whether the UI object should be immediately inserted |
| 49 |
#' or removed, or whether Shiny should wait until all outputs have been |
| 50 |
#' updated and all observers have been run (default). |
| 51 |
#' @param session The shiny session. Advanced use only. |
| 52 |
#' @examples |
| 53 |
#' ## Only run this example in interactive R sessions |
| 54 |
#' if (interactive()) { |
| 55 |
#' # Define UI |
| 56 |
#' ui <- fluidPage( |
| 57 |
#' actionButton("add", "Add UI") |
| 58 |
#' ) |
| 59 |
#' |
| 60 |
#' # Server logic |
| 61 |
#' server <- function(input, output, session) { |
| 62 |
#' observeEvent(input$add, { |
| 63 |
#' insertUI( |
| 64 |
#' selector = "#add", |
| 65 |
#' where = "afterEnd", |
| 66 |
#' ui = textInput(paste0("txt", input$add), |
| 67 |
#' "Insert some text") |
| 68 |
#' ) |
| 69 |
#' }) |
| 70 |
#' } |
| 71 |
#' |
| 72 |
#' # Complete app with UI and server components |
| 73 |
#' shinyApp(ui, server) |
| 74 |
#' } |
| 75 |
#' |
| 76 |
#' if (interactive()) { |
| 77 |
#' # Define UI |
| 78 |
#' ui <- fluidPage( |
| 79 |
#' actionButton("rmv", "Remove UI"), |
| 80 |
#' textInput("txt", "This is no longer useful") |
| 81 |
#' ) |
| 82 |
#' |
| 83 |
#' # Server logic |
| 84 |
#' server <- function(input, output, session) { |
| 85 |
#' observeEvent(input$rmv, { |
| 86 |
#' removeUI( |
| 87 |
#' selector = "div:has(> #txt)" |
| 88 |
#' ) |
| 89 |
#' }) |
| 90 |
#' } |
| 91 |
#' |
| 92 |
#' # Complete app with UI and server components |
| 93 |
#' shinyApp(ui, server) |
| 94 |
#' } |
| 95 |
#' @export |
| 96 |
insertUI <- function(selector, |
| 97 |
where = c("beforeBegin", "afterBegin", "beforeEnd", "afterEnd"), |
| 98 |
ui, |
| 99 |
multiple = FALSE, |
| 100 |
immediate = FALSE, |
| 101 |
session = getDefaultReactiveDomain()) { |
| 102 |
|
| 103 |
force(selector) |
| 104 |
force(ui) |
| 105 |
force(session) |
| 106 |
force(multiple) |
| 107 |
if (missing(where)) where <- "beforeEnd" |
| 108 |
where <- match.arg(where) |
| 109 |
|
| 110 |
callback <- function() { |
| 111 |
session$sendInsertUI(selector = selector, |
| 112 |
multiple = multiple, |
| 113 |
where = where, |
| 114 |
content = processDeps(ui, session)) |
| 115 |
} |
| 116 |
|
| 117 |
if (!immediate) session$onFlushed(callback, once = TRUE) |
| 118 |
else callback() |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
#' @rdname insertUI |
| 123 |
#' @export |
| 124 |
removeUI <- function(selector, |
| 125 |
multiple = FALSE, |
| 126 |
immediate = FALSE, |
| 127 |
session = getDefaultReactiveDomain()) { |
| 128 |
|
| 129 |
force(selector) |
| 130 |
force(multiple) |
| 131 |
force(session) |
| 132 |
|
| 133 |
callback <- function() { |
| 134 |
session$sendRemoveUI(selector = selector, |
| 135 |
multiple = multiple) |
| 136 |
} |
| 137 |
|
| 138 |
if (!immediate) session$onFlushed(callback, once = TRUE) |
| 139 |
else callback() |
| 140 |
} |