互斥单选按钮 R Shiny 的文本输出

Text output for mutually exclusive radio buttons R Shiny

总体背景:我正在尝试用 shiny 构建一个决策树应用程序,之前从未使用过 shiny。我试图在选择某个单选按钮时显示文本。经过反复试验,我仍然无法通过不同的输入显示任何内容。下面是基本代码。

library(shiny)

priceChange <- "Does it change the price?"
zpInfer1 <- "Can zero-price-change be inferred?"

ui <- fluidPage(
  radioButtons("exist", "Does it exist?",
               c("Yes" = "existYes",
                 "No" = "existNo"),
               textOutput("a")
  )
  
)


server <-   function(input, output){
  output$a <- renderText({ 
    existAnswer <- switch(input$exist,
                    existYes = priceChange,
                    existNo = zpInfer1)
    
    
  })
}

shinyApp(ui, server)

正如@Phil 所说,这似乎是一个简单的语法错误。这个应该可以工作:

library(shiny)

priceChange <- "Does it change the price?"
zpInfer1 <- "Can zero-price-change be inferred?"

ui <- fluidPage(
  radioButtons("exist", "Does it exist?",
               c("Yes" = "existYes",
                 "No" = "existNo")),
               textOutput("a")
  
)


server <-   function(input, output){
  output$a <- renderText({ 
    existAnswer <- switch(input$exist,
                          existYes = priceChange,
                          existNo = zpInfer1)
    
    
  })
}

shinyApp(ui, server)