我的迷你应用程序理想上应该像这样工作:
我无法得到最后一点工作:如果所选(或键入的)名称不为空,我怎么能让第二个按钮出现,并且一旦用户碰巧删除了开放式框中的所有内容,它就会消失?
非常感谢你!以下是我的代码:
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)
您可以尝试使用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)