r Shiny make textInput conditional on a previous selectInput

shiny conditional select input
renderui
shiny select input dynamic choices
r shiny conditional ui
r shiny hide ui element
conditional input shiny
r shiny dynamic input
r shiny filters

Hello and sorry for what might be a basic Shiny question.

In my mini-Shiny app, I want the user to:

  1. Pick a name from a list of pre-existing names using selectInput().

  2. Only if the name s/he has in mind is not on the list, I'd like a new widget to appear where s/he should enter the name using textInput().

I am stuck - my verbatimTextOutput statements inside the mainPanel on the UI side are not working.

Thank you for your hints!

library(shiny)

ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      uiOutput("chosen_name", label = h4("Select one of the names:"))
    ),
    mainPanel(                       # Just shows what was selected
      # Next two lines don't work if uncommented:
      #  verbatimTextOutput('chosen_name')
      #  verbatimTextOutput("name_openend")
    )
  )
))

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  mynames <- c("John", "Mary", "Jim")

  # Allow to select a name from the list of pre-existing names:
  output$chosen_name <- renderUI({
    selectInput('chosen_name',"Select a name:",
                choices = c("Name not on our list", mynames),
                selected = "Name not on our list")
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:
  output$name_openend <- renderUI({
    if (!output$chosen_name == 'Name not on our list') return(NULL) else {
      textInput("If the name you want is not on our list, type it here:")
    }
  })

})


shinyApp(ui = ui, server = server)

Updated Code

library(shiny)

ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      selectInput("chosen_name", "select name", choices = ""),
      uiOutput("new")
    ),
    mainPanel(
      textOutput("chosen")
    )
  )
))

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  mynames <- c("John", "Mary", "Jim")

  observe({
    updateSelectInput(session, inputId = "chosen_name", label = "Select a name:", choices = c(mynames, "Name not on our list"))
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:
  output$new <- renderUI({
    if (!input$chosen_name == 'Name not on our list') return(NULL) else {
      textInput("Not_on_list", "If the name you want is not on our list, type it here:")
    }
  })
  #
  # 
  # Allow to select a name from the list of pre-existing names:

  output$chosen <- renderText({
    if (!input$chosen_name == 'Name not on our list') {
    return(paste("Chosen name:", input$chosen_name))
    }
    else {
      return(paste("Chosen name:", input$Not_on_list))
    }
  })

})

shinyApp(ui = ui, server = server)

Build a dynamic UI that reacts to user input - R Shiny, The conditionalPanel function, which is used in ui. R , lets you generate calls to UI functions and make the results appear in a But you don't want to simply overwrite the previous model; you want to leave them there and ui = textInput( paste0("txt", input$add), "Insert some text") ) }) } shinyApp(ui, server). To use the standard HTML select input element, use selectInput() with selectize=FALSE. In selectize mode, if the first element in choices has a value of "" , its name will be treated as a placeholder prompt.

You confused a bit the functions on the ui side: If you use renderUI() on the server side, you will have to use uiOutput() on the ui side to make it work. Also you should avoid using the same id twice. Finally, for the textinput i added an id and a label.

Full code reads:

library(shiny)

ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      uiOutput("chosen_nm", label = h4("Select one of the names:"))
    ),
    mainPanel(                       # Just shows what was selected
      # Next two lines don't work if uncommented:
      uiOutput("name_openend")
    )
  )
))

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  mynames <- c("John", "Mary", "Jim")

  # Allow to select a name from the list of pre-existing names:
  output$chosen_nm <- renderUI({
    selectInput('chosen_name',"Select a name:",
                choices = c("Name not on our list", mynames),
                selected = "Name not on our list")
  })

  output$chosen_name2 <- renderText({
    paste("The chosen name is: ", input$chosen_name)
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:
  output$name_openend <- renderUI({
    req(input$chosen_name)
    if (input$chosen_name == 'Name not on our list'){
      textInput("newName", "If the name you want is not on our list, type it here:")
    }else{
      verbatimTextOutput('chosen_name2')
    }
  })

})


shinyApp(ui = ui, server = server)

Combine selectInput and Conditional under the same renderUI in , Google Groups allows you to create and participate in online forums textInput(" invarpdfsd","Please Select Input Variable Standard Deviation", 0.02)) https:// shiny.rstudio.com/articles/modules.html Shiny modules have a bit� You can use updateSelectInput to update your input with the new column names, and you can use get () to fetch a dataframe from the environment.

There were a few things that I needed to cut out to make it work. First was the sidebarLayout. This might have been messing with your mainPanel. I am sure you can get this to work just make sure the mainPanel is in the right place and not being added to the sidebarLayout, which might have been the problem.

There were a few other problems like renderUI and UIOutput. Maybe someone else will add a post on how to use those functions. Also, I added a reactive function so the textOutput would change.

Take a look at how mine works.

mynames <- c("John", "Mary", "Jim")
ui = shinyUI(
  fluidPage(

    sidebarPanel(
      selectInput('chosen_name',"Select a name:",
                  choices = c("Name not on our list", mynames),
                  selected = "Name not on our list")
    ),
    mainPanel(   
      textOutput('chosen_name2')

    )
  )
)

server = shinyServer(function(input, output, session) {

  # A vector of pre-existing names:
  selectedchosen_name <- reactive({
    if (input$chosen_name == 'Name not on our list') {return("Pick a name")} else {
      return("If the name you want is not on our list, type it here:")
    }
  })

  # Allow to select a name from the list of pre-existing names:
  output$chosen_name2 <- renderText({
    selectedchosen_name()
  })

  # Open end box to enter name - if the name the user wants to enter is not on the list:


})

shinyApp(ui = ui, server = server)

I removed some parts that might be redundant, but right now it's simplified so you can add to it. If you have any questions just ask.

Chapter 10 Dynamic UI, In this chapter, you'll learn how to create dynamic user interfaces, changing the UI using Using tabsetPanel() to conditionally show and hide parts of the user interface. Every input control, e.g. textInput() , is paired with an update function, e.g. Extend the previous app so that you can also choose to select all continents,� I am trying to use conditions to show or hide selectinputs in a R shiny app based if the tab is available in the UI or not. So on tabpanel with title product use then all the drop downs under product category should be seen else only the first dropdown under product category should be visible. Below is what I am doing but not getting the condition to work: # This is the server logic for a

Dynamic UI Elements in Shiny, One way to do this would be to add a static textInput or similar to the app. A much more elegant solution would be to conditionally render the� Value. A textarea input control that can be added to a UI definition. Server value. A character string of the text input. The default value is "" unless value is provided.. See also

R Shiny –Task: create an input select box that is dependent on a , Notice that each of the selectInput calls are wrapped in their own renderUI call. The last bit“output$plot1”, uses the info from the previous calls to� inputId: The input slot that will be used to access the value.: label: Display label for the control, or NULL for no label.: value: Initial value. width: The width of the input, e.g. '400px', or '100%'; see validateCssUnit.

Dynamic UI example app for R Shiny � GitHub, The user interface components in this example are generated as HTML on the Each time a new component is sent to the client, it completely replaces the previous component. "text" = textInput("dynamic", "Dynamic", conditionalPanel(condition = "output.file1 == true", selectInput("file2" I am trying to do a similar thing. Shiny comes with a family of pre-built widgets, each created with a transparently named R function. For example, Shiny provides a function named actionButton that creates an Action Button and a function named sliderInput that creates a slider bar. The standard Shiny widgets are:

Comments
  • Thank you, Diego - this is very-very close to the solution I was looking for! Only one question: is it possible to show on the Main Panel only ONE output: if the name is taken from the list, then show it. But if the name was typed, then show the typed name (but separately from the input box)?
  • I think you can. You'll have to render whatever the user types back to mainpanel. But where would you like the input box in? In the sidebar panel or mainpanel?
  • The location of the input box doesn't really matter. The sidebar is good.
  • I updated code. I think this might be the behavior you are going for.
  • Thank you so much, Diego. It works beautifully! May I ask a question? Why do we have to use updateSelectInput here instead of just SelectInput?
  • for me there is only a warning, that existed before. You can add ` req(input$chosen_name)` to avoid that. Do you still have a warning/error after the update?
  • Thank you very much, Kyle, but it's not working - it's showing just the pull-down on the left and nothing else but text on the right - no text box. BTW - the location of the controls (sidebar or main pain) doesn't really matter.
  • Yeah I didn't add a text box to type in. What do you want to happen when you type in the box? Do you want the name to be added to mynames after typing it?
  • If the name the user wants to enter is not among the names in the list, then an open-end box should open where the user types the name. And then the name should be returned on the main panel - no matter how that name was created - using a pull-down or using typing.