1

In the minimum application example below the inserted ui appears with fade oppacity (css class .recalculating)

Application UI in web browser

css class applied


library(shiny)

# Define UI
ui <- fluidPage(
  # this is where the UI will be inserted
  uiOutput("filters"),
  actionButton("add_filter", "Add")
)

# Define server logic required to insert UI
server <- function(input, output) {
  # when we click the button the UI is inserted
  observeEvent(input$add_filter,{
    insertUI(
      selector = "#filters",
      ui = div(
        selectInput("test", "test_label", choices = c(1,2))
      )
    )
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

Why isn't is the normal display? How could it work? It probably means that something is recalculating and this isn't a good thing for the performance as well...

1 Answer 1

3

It seems to be occurring because you don't have an output$filters defined in the server, so there is nothing to insert the UI into. You can either create a NULL placeholder for output$filters, use a renderUI instead of insertUI or use selector = "#add_filter" and where = "beforeBegin" to add the UI before the action button.

library(shiny)

ui <- fluidPage(
    uiOutput("filters"),
  uiOutput("filters2"),
  actionButton("add_filter", "Add")
)


server <- function(input, output) {

  output$filters <- renderUI(NULL)
  
  observeEvent(input$add_filter,{
    insertUI(
      selector = "#filters",
      ui = div(
        selectInput("test", "test_label", choices = c(1,2))
      )
    )
  })

  output$filters2 <- renderUI({
    selectInput("test2", "test_label 2", choices = c(1,2))
  }) |> bindEvent(input$add_filter)

  observeEvent(input$add_filter,{
    insertUI(
      selector = "#add_filter",
      where = "beforeBegin",
      ui = div(
        selectInput("test3", "test_label 3", choices = c(1,2))
      )
    )
  })
  
}

shinyApp(ui = ui, server = server)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.