R Shiny:如果前一个输入不为空,则显示一个新的操作按钮,如果是,则消失

问题描述 投票:0回答:2

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

  1. 用户从预先存在的名称列表中选择一个名称;
  2. 如果用户的名字不在列表中,则会显示一个开放式框,供用户输入新名称;
  3. 用户点击动作按钮“显示所选名称”,并且在主面板上显示选择或键入的任何名称;
  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)
r shiny
2个回答
1
投票

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

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)

0
投票

您可以尝试使用conditionalPanel,您需要创建另一个observeEvent来控制第二个按钮。

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)
© www.soinside.com 2019 - 2024. All rights reserved.