Home > Mobile >  Why is removeUI leaving behind the UI label?
Why is removeUI leaving behind the UI label?

Time:02-22

I am trying to increase and decrease the number of UI elements based on user input. This MRE code kind of works but it is leaving behind the UI label when I use removeUI, which I did not expect. Any ideas on how to make the label go away along with the input box?

## Only run this example in interactive R sessions
if (interactive()) {
    # Define UI
    ui <- fluidPage(
        numericInput(inputId = "assessors",label = "Number of Assessors",value = 1,min = 1,step = 1),
        textInput(inputId = "assessor1",label = "Assessor 1 Columns")
    )
    
    # Server logic
    server <- function(input, output, session) {
        tot_app<-0
 
        observeEvent(input$assessors, {
            num<-input$assessors

            if(num>tot_app){#add
                adds<-seq(tot_app 1,num)
                for(i in adds){
                    here<-paste0("#assessor",i-1)
                    insertUI(
                        selector = here,
                        where = "afterEnd",
                        ui = textInput(paste0("assessor", i),
                               paste0("Assessor ",i," columns"))
            )
                }
                tot_app<<-num
                
            } else if(num<tot_app){#subtract
                subs<-seq(num 1,tot_app)
                for(i in subs){
                    removeUI(selector = paste0("#assessor",i))
                }
                
                tot_app<<-num
            }
            
            
        })
    }
    
    # Complete app with UI and server components
    shinyApp(ui, server)
}

CodePudding user response:

As in the help example, it works if you use use this syntax:

removeUI(selector = paste0("div:has(> #assessor",i,")"))
  • Related