Description Usage Arguments Details Examples
These functions allow you to dynamically add and remove arbirary UI
into your app, whenever you want, as many times as you want.
Unlike renderUI()
, the UI generated with insertUI()
is persistent:
once it's created, it stays there until removed by removeUI()
. Each
new call to insertUI()
creates more UI objects, in addition to
the ones already there (all independent from one another). To
update a part of the UI (ex: an input object), you must use the
appropriate render
function or a customized reactive
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
selector |
A string that is accepted by jQuery's selector
(i.e. the string For |
where |
Where your UI object should go relative to the selector:
Adapted from https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML. |
ui |
The UI object you want to insert. This can be anything that
you usually put inside your apps's |
multiple |
In case your selector matches more than one element,
|
immediate |
Whether the UI object should be immediately inserted or removed, or whether Shiny should wait until all outputs have been updated and all observers have been run (default). |
session |
The shiny session. Advanced use only. |
It's particularly useful to pair removeUI
with insertUI()
, but there is
no restriction on what you can use on. Any element that can be selected
through a jQuery selector can be removed through this function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | ## Only run this example in interactive R sessions
if (interactive()) {
# Define UI
ui <- fluidPage(
actionButton("add", "Add UI")
)
# Server logic
server <- function(input, output, session) {
observeEvent(input$add, {
insertUI(
selector = "#add",
where = "afterEnd",
ui = textInput(paste0("txt", input$add),
"Insert some text")
)
})
}
# Complete app with UI and server components
shinyApp(ui, server)
}
if (interactive()) {
# Define UI
ui <- fluidPage(
actionButton("rmv", "Remove UI"),
textInput("txt", "This is no longer useful")
)
# Server logic
server <- function(input, output, session) {
observeEvent(input$rmv, {
removeUI(
selector = "div:has(> #txt)"
)
})
}
# Complete app with UI and server components
shinyApp(ui, server)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.