理想情况下,我的迷你应用应该这样工作:
用户从预先存在的名称列表中选择一个名称;
如果用户记住的名称不在列表中,则会出现一个开放式框,供用户键入新名称;
用户点击操作按钮“显示所选名称”,并且在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)
答案 0 :(得分: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)
答案 1 :(得分: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)