user3245256
user3245256

Reputation: 1948

R Shiny: make a new action button appear if previous input is not empty and disappear if it is

my mini app should ideally work like this:

  1. User selects a name from a list of preexisting names;

  2. If the name user has in mind is not on the list, an open-end box appears for the user to type the new name in;

  3. The user hits an action button "Show chosen name" and whatever name is selected or typed is shown on mainPanel;

  4. Another action button "Show number of characters" appears only after "Show chosen name" button was clicked - but only if the name is selected from the list OR if the open-end box for the user-provided name is not empty. If the user hits this new button, it shows the number of characters in the chosen name.

I cannot get the last point to work: How could I make the second button appear ONLY if the chosen (or typed) name is NOT empty and disappear as soon as the user happens to delete everything in the open-end box?

Thank you very much! Below is my code:

library(shiny)
ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      selectInput("name_fromlist", "Select a name", choices = ""),
      uiOutput("open_end")
    ),
    mainPanel(
      textOutput("name_final"), br(),
      actionButton("button1", label = "Show chosen name"), br(),
      textOutput('final_name'),
      uiOutput("second_button")  # it should show number of characters in the chosen name
    )
  )
))

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

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

  # Pull-down to select one of the names:
  observe({
    updateSelectInput(session, inputId = "name_fromlist", label = "Select a name:", 
                      choices = c(mynames, "Name not on our list"))
  })

  # Open end box to appear only if the name the user wants to enter is not on the list:
  output$open_end <- renderUI({
    if (!input$name_fromlist == '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:")
    }
  })

  # button 1 shows the name selected or typed:
  observeEvent(input$button1, {
    if (input$name_fromlist == 'Name not on our list') selected_name <- input$Not_on_list else {
      selected_name <- input$name_fromlist
    }
    output$final_name <- renderText({paste("Chosen name:  ", selected_name)})
  })

  # # This part is not working:
  # observe({
  #   if (input$name_fromlist == 'Name not on our list' & input$Not_on_list == '') renderUI({NULL}) else {
  #     output$add_user <- renderUI({
  #       actionButton("second_button", label = "Show number of characters")
  #     })
  #   } # end of else
  # }) # end of observe
})

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 1978

Answers (2)

user3245256
user3245256

Reputation: 1948

Looks like I found a solution - without conditional panel. Notice that the second button disappears if the open-end box is empty:

library(shiny)

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

ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      selectInput("name_fromlist", "Select a name", choices = c(mynames, "Name not on our list")),
      uiOutput("open_end")
    ),
    mainPanel(
      textOutput("name_final"), br(),
      actionButton("button1", label = "Show chosen name"), 
      br(),
      textOutput('final_name'), br(),
      uiOutput("button2"),
      br(),
      # Display number of characters for the chosen names
      conditionalPanel(condition = " input.name_fromlist != 'Name not on our list' |
                       input.Not_on_list != '' ", 
                       textOutput("no_of_char")
      )      
    )
  )
))

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

  # Open end box to appear only if the name the user wants to enter is not on the list:
  output$open_end <- renderUI({
    if (!input$name_fromlist == '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:")
    }
  })

  # button 1 shows the name selected or typed:
  observeEvent(input$button1, {
    if (input$name_fromlist == 'Name not on our list') selected_name <- input$Not_on_list else {
      selected_name <- input$name_fromlist
    }
    output$final_name <- renderText({paste("Chosen name:  ", selected_name)})
    output$button2 <- renderUI({
      actionButton("button2", label = "Show number of characters")
    })
  })

  # This observe allows the second button to disappear:
  observe({
    if (!is.null(input$Not_on_list)) {
      if (input$name_fromlist == 'Name not on our list' & input$Not_on_list == '') {
        output$button2 <- renderUI({NULL})
      }
    }
  })

  #### observeEvent for Second Button
  ## This is to display number of charactesr based on chosen/typed names
  observeEvent(input$button2, {
    if (input$name_fromlist == "Name not on our list") {
      selected_name <- input$Not_on_list
    } else {
      selected_name <- input$name_fromlist  
    }

    output$no_of_char <- renderText({paste("Number of Characters:  ", nchar(selected_name))})
  })

})


shinyApp(ui = ui, server = server)

Upvotes: 1

MKa
MKa

Reputation: 2318

You can try using the conditionalPanel and you will need to create another observeEvent to control for this second button.

library(shiny)
ui = shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      selectInput("name_fromlist", "Select a name", choices = ""),
      uiOutput("open_end")
    ),
    mainPanel(
      textOutput("name_final"), br(),
      actionButton("button1", label = "Show chosen name"), br(),
      textOutput('final_name'),

      #### Second Button ####
      #  to only appear if name from the list is chosen or Name not on the list is not empty
      conditionalPanel(condition = "(input.name_fromlist != '' & input.name_fromlist != 'Name not on our list') |input.Not_on_list != ''", 
                       actionButton("button2", label = "Show number of characters")),

      # Display number of characters for the chosen names
      textOutput("no_of_char")

    )
  )
))

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

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

  # Pull-down to select one of the names:
  observe({
    updateSelectInput(session, inputId = "name_fromlist", label = "Select a name:", 
                      choices = c(mynames, "Name not on our list"))
  })

  # Open end box to appear only if the name the user wants to enter is not on the list:
  output$open_end <- renderUI({
    if (!input$name_fromlist == '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:")
    }
  })

  # button 1 shows the name selected or typed:
  observeEvent(input$button1, {
    if (input$name_fromlist == 'Name not on our list') selected_name <- input$Not_on_list else {
      selected_name <- input$name_fromlist
    }
    output$final_name <- renderText({paste("Chosen name:  ", selected_name)})
  })

  #### observeEvent for Second Button
  ## This is to display number of charactesr based on chosen/typed names
  observeEvent(input$button2, {
    if (input$name_fromlist == "Name not on our list") {

      selected_name <- input$Not_on_list
    } else {
      selected_name <- input$name_fromlist  
    }

    output$no_of_char <- renderText({paste("Number of Characters:  ", nchar(selected_name))})
  })

})

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions