insertUI: Insert UI objects

Description Usage Arguments Details See Also Examples

View source: R/insert-ui.R

Description

Insert a UI object into the app.

Usage

1
2
3
insertUI(selector, multiple = FALSE, where = c("beforeBegin", "afterBegin",
  "beforeEnd", "afterEnd"), ui, immediate = FALSE, container = if (inline)
  "span" else "div", inline = FALSE, session = getDefaultReactiveDomain())

Arguments

selector

A string that is accepted by jQuery's selector (i.e. the string s to be placed in a $(s) jQuery call). This selector will determine the element(s) relative to which you want to insert your UI object.

multiple

In case your selector matches more than one element, multiple determines whether Shiny should insert the UI object relative to all matched elements or just relative to the first matched element (default).

where

Where your UI object should go relative to the selector:

beforeBegin

Before the selector element itself

afterBegin

Just inside the selector element, before its first child

beforeEnd

Just inside the selector element, after its last child (default)

afterEnd

After the selector element itself

Adapted from here.

ui

The UI object you want to insert. This can be anything that you usually put inside your apps's ui function.

immediate

Whether the UI object should be immediately inserted into the app when you call insertUI, or whether Shiny should wait until all outputs have been updated and all observers have been run (default).

container

A function to generate an HTML element to contain the UI object.

inline

Use an inline (span()) or block container (div(), default) for the output.

session

The shiny session within which to call insertUI.

Details

This function allows you to dynamically add an arbitrarily large UI object into your app, whenever you want, as many times as you want. Unlike renderUI, the UI generated with insertUI is not updatable as a whole: once it's created, it stays there. 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. To remove any part of your UI, use removeUI.

Note that whatever UI object you pass through ui, it is always wrapped in an extra div (or if inline = TRUE, a span) before making its way into the DOM. This does not affect what you mean to do, and it makes it easier to remove the whole UI object using removeUI (if you wish to do so, of course).

See Also

removeUI

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 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)
}

ymd526442121/Rproject_shiny documentation built on May 4, 2019, 5:31 p.m.