您好,很抱歉可能是一个基本的 Shiny 问题。
在我的迷你闪亮应用程序中,我希望用户:
使用 selectInput() 从预先存在的名称列表中选择一个名称。
只有当他/她想到的名字不在列表中时,我才希望在他/她应该使用 textInput() 输入名字的地方出现一个新的小部件。
我被卡住了 - 我在 UI 端的 mainPanel 中的逐字文本输出语句不起作用。
谢谢你的提示!
library(shiny)
ui = shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput("chosen_name", label = h4("Select one of the names:"))
),
mainPanel( # Just shows what was selected
# Next two lines don't work if uncommented:
# verbatimTextOutput('chosen_name')
# verbatimTextOutput("name_openend")
)
)
))
server = shinyServer(function(input, output, session) {
# A vector of pre-existing names:
mynames <- c("John", "Mary", "Jim")
# Allow to select a name from the list of pre-existing names:
output$chosen_name <- renderUI({
selectInput('chosen_name',"Select a name:",
choices = c("Name not on our list", mynames),
selected = "Name not on our list")
})
# Open end box to enter name - if the name the user wants to enter is not on the list:
output$name_openend <- renderUI({
if (!output$chosen_name == 'Name not on our list') return(NULL) else {
textInput("If the name you want is not on our list, type it here:")
}
})
})
shinyApp(ui = ui, server = server)