首页 > 解决方案 > R Shiny:如果先前的输入不为空,则显示一个新的操作按钮,如果为空则消失

问题描述

我的迷你应用程序理想情况下应该像这样工作:

  1. 用户从预先存在的名称列表中选择一个名称;

  2. 如果用户想的名字不在列表中,则会出现一个开放框供用户输入新名字;

  3. 用户点击“显示选择的名称”操作按钮,选择或输入的任何名称都会显示在 mainPanel 上;

  4. 另一个操作按钮“显示字符数”仅在单击“显示选择的名称”按钮后出现 - 但仅当从列表中选择名称或用户提供的名称的开放式框不为空时。如果用户点击这个新按钮,它会显示所选名称中的字符数。

我无法获得最后的工作要点:只有只有当所选(或键入)名称不是空的,并且一旦用户碰巧删除了开放式框中的所有内容,才能使第二个按钮出现?

非常感谢!下面是我的代码:

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)

标签: rshiny

解决方案


看起来我找到了解决方案 - 没有条件面板。请注意,如果开口框为空,则第二个按钮会消失:

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)

推荐阅读