Debian Codegraph @

man/insertUI.Rd

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