代码之家  ›  专栏  ›  技术社区  ›  J.Sabree

在被动模式中添加if语句或筛选if语句

  •  0
  • J.Sabree  · 技术社区  · 7 年前

    我正在尝试制作一个闪亮的仪表板/应用程序,用户可以根据各种标准过滤数据。应用过滤器后,用户可以选择他/她希望在条形图和过滤数据集中看到的变量。

    对于示例代码,我希望用户能够按位置进行筛选。级别为亚特兰大、芝加哥和西雅图。但是,我也希望用户能够在默认情况下一次按所有城市进行筛选。All不是数据集中的一个选项,所以我想添加一个“All”选项。因此,如果输入$location类型为,我希望人们过滤\u!“好的,但我不能让它工作。下面是我的代码和过滤器-如果你能修改它使过滤器工作,我将非常感激!

    library(tools)
    library(dplyr)
    library(shiny)
    library(ggplot2)
    ui <- fluidPage(
      sidebarLayout(
      sidebarPanel(
    
      selectInput(inputId = "x",
                  label = "Predictor:",
                  choices = c("ID", "location", "title", "job_sat", "motivation", "commitment", "review"),
                  selected = "ID"),
      selectInput(inputId = "y",
                  label = "Outcome:",
                  choices = c("ID", "sales", "location", "title", "job_sat", "motivation", "commitment", "review"),
                  selected = "sales"),
      textInput(inputId = "plot_title",
                label = "Plot Title:",
                placeholder = "Enter text for this plot's title"),
      selectInput(inputId = "location_type",
                  label = "Location:",
                  choices = c("All", levels(fakeshinydata$location)),
                  selected = "All",
                  multiple = TRUE)
    ),
    mainPanel(
      plotOutput(outputId = "scatterplot")
    )
    )
    )
    
    server <- function(input, output) {
    
    
      fake_subset <- reactive({
        req(input$location_type)
        dplyr::filter(fakeshinydata, location %in% input$location_type)
       })
    
       pretty_plot_title <- reactive({toTitleCase(input$plot_title)})
    
      output$scatterplot <- renderPlot({
    ggplot(data = fake_subset(),
           aes_string(x = input$x, y = input$y)) +
      geom_point() +
      labs(title = pretty_plot_title())
        })
    }
    
     shinyApp(ui = ui, server = server)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Shree    7 年前

    这是你需要的-

    fake_subset <- reactive({
      req(input$location_type)
      filter(fakeshinydata, (location %in% input$location_type) | (input$location_type == "All"))
    })
    
    推荐文章