redred10
redred10

Reputation: 21

My first R Shiny , how to am i combine selectinput with renderplot(ggplot)?

i want combine selectinput with renderplot(ggplot). I want to make bar (plot)chart the year and month I choose. if i choose select year(yil) 2009 and select month(ay) 2 , plot be like must show my selects. this is like filter maybe i dont know how to solve this problem. year and month value inside my datagrid , i shared my data picture

my ui.R ;

library(shiny)
library(ggplot2)


shinyUI(fluidPage(

    titlePanel(title=h4("Norvec Arac Satıs Verisi 2007-2016",align="center")),

    sidebarLayout(
        sidebarPanel(
            selectInput("yil","1.Yıl Seçiniz",
                        choices = list("2007"=1,"2008"=2,"2009"=3,"2010"=4,"2011"=5,"2012"=6,"2013"=7,"2014"=8,"2015"=9,"2016"=10)),
            sliderInput("ay","2. Ay Seçiniz",min = 1,max = 12,value = 1,step = 1,
                        animate = animationOptions(interval=800,loop = FALSE, playButton = "OYNAT", pauseButton = "DUR"))

            ),

        mainPanel(
            tabsetPanel(type="tab",
                        tabPanel("Grafik",plotOutput("bar"))

            )
        )


    )
))

my server.R ;

library(shiny)
library(ggplot2)
library(dplyr)


shinyServer(function(input,output){

  output$bar <- renderPlot({
    ggplot(data=carsales,aes(x = Brand, y = Quantity, group = Brand, color = Brand, fill=Brand)) +
      geom_bar(stat = "identity")


  })



})

shiny:

shiny

mydata:

mydata

mydata :

    > head(carsales)
  Year Month      Brand Quantity
1 2007     1     Toyota     2884
2 2007     1 Volkswagen     2521
3 2007     1    Peugeot     1029
4 2007     1       Ford      870
5 2007     1      Volvo      693
6 2007     1      Skoda      665

Upvotes: 0

Views: 1130

Answers (2)

redred10
redred10

Reputation: 21

Firstly thank you @Ravi 1. I Appreciated .Thats problem solved. I just changed selectInput(choices=). Started working as I wanted

ui.R ;

    library(shiny)
library(ggplot2)


shinyUI(fluidPage(

    titlePanel(title=h4("Norvec Arac Satis Verisi 2007-2016",align="center")),

    sidebarLayout(
        sidebarPanel(
            selectInput("yil","1.Yil Seciniz",
                        choices = carsales$Year),
            br(),
            br(),
            sliderInput("ay","2. Ay Seciniz",min = 1,max = 12,value = 1,
                        animate = animationOptions(interval = 800 , loop = FALSE , playButton = "OYNAT",pauseButton = "DUR"))

        ),

        mainPanel(
            tabsetPanel(type="tab",
                        tabPanel("Grafik",plotOutput("bar"))

            )
        )


    )
))

server.R ;

library(shiny)
library(ggplot2)
library(dplyr)


shinyServer(function(input,output){

    carsales_subset <- reactive({
        carsales %>% 
            filter(Year==input$yil ,Month==input$ay)
    })

    output$bar <- renderPlot({
        ggplot(carsales_subset(),aes(x = Brand, y = Quantity, group = Brand, color = Brand, fill=Brand)) +
            geom_bar(stat = "identity")


    })




})

str(carsales)

'data.frame':   972 obs. of  4 variables:
 $ Year    : num  2007 2007 2007 2007 2007 ...
 $ Month   : num  1 1 1 1 1 1 1 1 2 2 ...
 $ Brand   : Factor w/ 65 levels "Alfa Romeo","Aston Martin",..: 62 63 46 19 64 53 45 3 62 63 ...
 $ Quantity: int  2884 2521 1029 870 693 665 622 599 1885 1517 ...

Upvotes: 0

Ravi
Ravi

Reputation: 622

You can make a reactive dataset

carsales_subset <- reactive({
                           carsales %>% filter(Year==input$yil, Month==input$ay)
                           })

Then pass this through the ggplot function

output$bar <- renderPlot({
    ggplot(data=carsales_subset(),aes(x = Brand, y = Quantity, group = Brand, color = Brand, fill=Brand)) +
      geom_bar(stat = "identity")


  })

Upvotes: 1

Related Questions