微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据搜索按钮在 Shiny Dashboard 中过滤 ggplot 图表

如何解决根据搜索按钮在 Shiny Dashboard 中过滤 ggplot 图表

下面是可重现的代码

# DF
branch <- c("north","South","north","north")
cars <- c("Toyota","Nissan","BMW","Ford","Toyota","Nissan")
insured <- c("Yes","Yes","No","No")
price <- c(21000,23400,26800,21000,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)

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

解决方法

也许你正在寻找这个

 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",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),fill=factor(insured))) +
        geom_bar(stat = "identity") + labs(fill="Insured)")
    }else return(NULL)
  })
  
}

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

output

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。