2017-04-24 117 views
2

我成功啓動了以下代碼,輸出RadioButton的兩個選項:「否」和「是」。但是,我想添加第三個單選按鈕,將其稱爲「全部」,在一個輸出中顯示「否」和「是」的值。R Shiny:「All」Radio Button

我該如何去做這件事?

library(shiny) 
library(ggplot2) 
library(dplyr) 

bcl <- read.csv("testd10Apr2017V4.csv", sep = ",", stringsAsFactors = FALSE) 

ui <- fluidPage(
    titlePanel("Attrition VS Age"), 
    sidebarLayout(
    sidebarPanel(
#  sliderInput("priceInput", "Price", 0, 100, c(25, 40), pre = "$"), 
     radioButtons("typeInput", "Type", 
        choices = c("No", "Yes", "All"), 
        selected = "Yes", inline = TRUE, width = "200px"), 
     hr(), 
     helpText("Attrition no means there is no attrition") 
#  selectInput("countryInput", "Country", 
#     choices = c("CANADA", "FRANCE", "ITALY")) 
    ), 
    mainPanel(
     plotOutput("coolplot") 
#  br(), br(), 
#  tableOutput("results") 
    ) 
) 
) 

server <- function(input, output) { 
    output$coolplot <- renderPlot({ 
    filtered <- 
     bcl %>% 
     filter(#Price >= input$priceInput[1], 
      #Price <= input$priceInput[2], 
      Type == input$typeInput 
#    Country == input$countryInput 
    ) 
    g <- ggplot(filtered, aes(Age)) + 
     geom_histogram(color = "white", fill = "maroon") 
    g + ylim(0,30) + xlim(0,60) 
    g 
    }) 
} 

shinyApp(ui = ui, server = server) 

回答

1

我還沒有運行代碼,因爲我沒有數據文件,但這樣的事情應該爲你工作。 在服務器功能裏面,可以添加如下內容:

if (input$typeInput == "All"){ 
    filtered <- bcl 
} else { 
    filtered <- bcl %>% 
     filter(Type == input$typeInput) 
}