简体   繁体   中英

Filter ggplot charts in Shiny Dashboard based on search button

Below is the reproducible code

# DF
branch <- c("North", "South","South","North","North","South","North")
cars <- c("Toyota","Nissan","BMW","Nissan","Ford","Toyota","Nissan")
insured <- c("Yes","Yes","No","Yes","Yes","Yes","No")
price <- c(21000, 23400, 26800,21000, 23400, 26800,21000)
salesDF <- data.frame(branch, cars,insured, price)
carBranch <- unique(salesDF$branch)



library(shiny)
library(DT)
library(shinydashboard)
library(plotly)
library(tidyverse)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Car Sales"),
  
  # Sidebar with the selectInput Slider
  sidebarLayout(
    
    sidebarMenu(
      sidebarSearchForm(textId = "Search", buttonId = "search Car",
                        label = "Search Town")
    ),    
    
    # Show the DataTable
    mainPanel(
      box(title = "Car Sales", width = 7, height=NULL, solidHeader = T, status = "warning",
          plotlyOutput("carBranch"))
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$carBranch <- renderPlotly({
    ggplot(salesDF, aes(branch, insured)) + 
      geom_bar(stat = "identity")
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

How do I make the plot filter based on a search of a particular car?

Perhaps you are looking for this

 Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Car Sales"),

  # Sidebar with the selectInput Slider
  sidebarLayout(

    sidebarMenu(
      sidebarSearchForm(textId = "Search", buttonId = "search Car",
                        label = "Search Town"),
      selectInput("mycar", "Choose a Car" , choices=unique(salesDF$cars))
    ),

    # Show the DataTable
    mainPanel(
      box(title = "Car Sales w/ selectInput", width = 6, height=NULL, solidHeader = T, status = "warning",
          plotlyOutput("carBranch", width=400)),
      box(title = "Car Sales w/ SearchForm", width = 6, height=NULL, solidHeader = T, status = "success",
          plotlyOutput("carBranch2", width=400))
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$carBranch <- renderPlotly({
    ggplot(salesDF[salesDF$cars %in% input$mycar,], aes(x=branch, y=price, fill=factor(insured))) +
      geom_bar(stat = "identity") + labs(fill="Insured)")
  })
  output$carBranch2 <- renderPlotly({
    req(input$Search)
    df <- salesDF
    df$ucars <- toupper(df$cars)
    if (sum(df$ucars %in% toupper(input$Search))>0) {
      ggplot(df[df$ucars %in% toupper(input$Search),], aes(x=branch, y=price, fill=factor(insured))) +
        geom_bar(stat = "identity") + labs(fill="Insured)")
    }else return(NULL)
  })
  
}

# Run the application
shinyApp(ui = ui, server = server)

输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM