Skip to content

Client error console and inserted/removed UI elements #3944

Closed

Description

@gadenbuie

With #3931, when dynamically adding UI to a running app, it seems that the matching IDs checks run on the entire app and flag stable input/output elements as duplicated.

The following app (shinycoreci: 111-insert-ui) reproduces the problem:

library(shiny)

ui <- fluidPage(
  actionButton('insertBtn', 'Insert'),
  actionButton('removeBtn', 'Remove'),
  tags$div(id = 'placeholder') #<< ui elements will be added here
)

server <- function(input, output) {

  ## keep track of elements inserted and not yet removed
  inserted <- c()

  observeEvent(input$insertBtn, {
    count <- input$insertBtn
    # give the inserted element a unique id so we can remove it later
    id <- paste0('txt', count)
    
    # push a new element in to the app
    insertUI(
      selector = '#placeholder',
      ui = tags$div(
        tags$p(paste('Element number', count)),
        id = id
      )
    )
    inserted <<- c(inserted, id)
  })

  observeEvent(input$removeBtn, {
    # pop last inserted element out of the app
    removeUI(
      selector = paste0('#', inserted[length(inserted)])
    )
    # and remove it from the list of inserted elements
    inserted <<- inserted[-length(inserted)]
  })

}

shinyApp(ui, server)

With client error console

Kapture.2023-11-28.at.11.20.36.mp4

shiny 1.8.0

Kapture.2023-11-28.at.11.21.45.mp4

Activity

  1. gadenbuie commented on Nov 28, 2023

    @gadenbuie
    MemberAuthor

    Similar behavior happens for insertTab() and removeTab().

    library(shiny)
    
    ui <- fluidPage(
      actionButton('insertBtn', 'Insert'),
      actionButton('removeBtn', 'Remove'),
      tabsetPanel(
        id = "tabs",
        tabPanel(
          "Base",
          "Base tab content. Other tabs will have their tab-specific text."
        )
      )
    )
    
    server <- function(input, output) {
    
      ## keep track of elements inserted and not yet removed
      inserted <- c()
    
      observeEvent(input$insertBtn, {
        count <- input$insertBtn
        # give the inserted element a unique id so we can remove it later
        new_tab_title <- paste('Tab', count)
    
        # push a new element in to the app
        insertTab(
          inputId = "tabs",
          tabPanel(
            title = new_tab_title,
            paste("Content for", new_tab_title)
          )
        )
        inserted <<- c(inserted, new_tab_title)
      })
    
      observeEvent(input$removeBtn, {
        # pop last inserted tab out of the app
        removeTab(
          inputId = "tabs",
          target = inserted[length(inserted)]
        )
        # and remove it from the list of inserted elements
        inserted <<- inserted[-length(inserted)]
      })
    
    }
    
    shinyApp(ui, server)
    

    With client error console

    Kapture.2023-11-28.at.12.06.49.mp4

    shiny 1.8.0

    Kapture.2023-11-28.at.12.06.08.mp4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions