Print dropdown selected value using insertUi and removeUI shiny

I am trying to print the selected value from the dropdown which can be achieved with simple code below:

library(shiny)

ui <- fluidPage(
  tagList(
    selectInput(
      inputId = "test",
      label = "Test",
      choices = c("Test1", "Test2", "Test3"),
      selected = NULL),
    uiOutput("text")
  )
)

server <- function(input, output, session) {
  output$text <- renderText({
    req(input$test)
    
    input$test
  })
}

shinyApp(ui, server)

But I am trying to do the same behavior but using the insertUI and removeUI functions from shiny. Like it will remove the previous selected value and will insert the new value.

Is there any way that this can be done?

library(shiny)

ui <- fluidPage(
  tagList(
    selectInput(
      inputId = "test",
      label = "Test",
      choices = c("Test1", "Test2", "Test3"),
      selected = NULL),
    div(id="some_location")
  )
)

server <- function(input, output, session) {
  observeEvent(input$test,
               {
                 removeUI(selector = "#myspan",
                          immediate = TRUE)
                 insertUI(selector = "#some_location",
                          ui=span(id="myspan",
                                  input$test),
                          immediate = TRUE)
               })
}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.