【问题标题】:Filter ggplot charts in Shiny Dashboard based on search button根据搜索按钮在 Shiny Dashboard 中过滤 ggplot 图表
【发布时间】:2021-06-06 14:40:14
【问题描述】:

下面是可重现的代码

# 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)

如何根据对特定汽车的搜索来制作情节过滤器?

【问题讨论】:

    标签: r shiny shinydashboard shinyapps shiny-reactivity


    【解决方案1】:

    也许你正在寻找这个

     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)
    

    【讨论】:

    • 它有效。小调整请求,你知道如何捕捉错误,例如如果汽车模型不存在,它会返回一个文本,表明汽车不存在?另外,你知道如何让搜索不区分大小写吗?
    猜你喜欢
    • 1970-01-01
    • 2019-10-18
    • 2020-04-02
    • 2020-12-13
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2012-12-04
    • 2020-02-21
    相关资源
    最近更新 更多