r - R Shiny:如果先前的输入不为空,则显示一个新的操作按钮,如果为空则消失
问题描述
我的迷你应用程序理想情况下应该像这样工作:
用户从预先存在的名称列表中选择一个名称;
如果用户想的名字不在列表中,则会出现一个开放框供用户输入新名字;
用户点击“显示选择的名称”操作按钮,选择或输入的任何名称都会显示在 mainPanel 上;
另一个操作按钮“显示字符数”仅在单击“显示选择的名称”按钮后出现 - 但仅当从列表中选择名称或用户提供的名称的开放式框不为空时。如果用户点击这个新按钮,它会显示所选名称中的字符数。
我无法获得最后的工作要点:只有只有当所选(或键入)名称不是空的,并且一旦用户碰巧删除了开放式框中的所有内容,才能使第二个按钮出现?
非常感谢!下面是我的代码:
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)
解决方案
看起来我找到了解决方案 - 没有条件面板。请注意,如果开口框为空,则第二个按钮会消失:
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)
推荐阅读
- c# - DataAnnotations 无法提交使用 ajax 调用的部分视图的表单
- java - 使用 REST API 和 Java Jersey 应用程序在 Wordpress 上发布帖子
- reactjs - 想要检索单击按钮的特定行的值。但我得到了最后一个
- forms - Scala - Play:如何将表单字段绑定到 Vector?
- apache - 如何将www重定向到非www https?
- php - 未从 Laravel 事件侦听器内部触发的事件和作业
- python - 如何清理文本块 Python 3 没有外部模块?
- sql-server - 如何在已知 SQL Server 表中创建运行时列
- conv-neural-network - 具有附加对象识别功能的 ARCore
- oauth-2.0 - OAuth 2 授权代码流的 OpenAPI/Swagger 规范