【问题标题】:R Shiny: "All" Radio ButtonR Shiny:“全部”单选按钮
【发布时间】:2017-04-24 16:29:08
【问题描述】:

我成功启动了以下代码,它为 RadioButtons 输出了两个选项:“否”和“是”。但是,我想添加第三个单选按钮,称为“全部”,在一个输出中同时显示“否”和“是”的值。

我该怎么做?

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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我没有运行代码,因为我没有数据文件,但是这样的东西应该适合你。 在服务器函数中,您可以添加如下内容:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 2020-09-14
      • 2022-01-11
      • 2014-10-27
      • 2017-01-24
      相关资源
      最近更新 更多