【问题标题】:My first R Shiny , how to am i combine selectinput with renderplot(ggplot)?我的第一个 R Shiny,我如何将 selectinput 与 renderplot(ggplot) 结合起来?
【发布时间】:2020-04-02 08:12:37
【问题描述】:

我想将 selectinput 与 renderplot(ggplot) 结合起来。我想制作我选择的年份和月份的条形图。如果我选择 select year(yil) 2009 并选择 month(ay) 2 ,情节就像必须显示我的选择。这就像过滤器,也许我不知道如何解决这个问题。我的数据网格中的年月值,我分享了我的数据图片

我的 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"))

            )
        )


    )
))

我的服务器.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")


  })



})

闪亮:

我的数据:

我的数据:

    > 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

【问题讨论】:

    标签: r ggplot2 plot shiny bar-chart


    【解决方案1】:

    你可以制作一个反应式数据集

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

    然后通过ggplot函数传递这个

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

    【讨论】:

    • 感谢您的回答,但情节为空。我不知道出了什么问题。感谢您的时间
    • 立即检查就是检查年月的数据类型,使其与input$yliinput$ay一致
    • 年份和月份类型为 "int" 。有什么问题吗?
    • 我认为 as.numeric(input$yli)as.numeric(input$ay) 应该可以正常工作
    • 如果我写 2007 而不是 as.numeric(input$yli) 和 1 而不是 as.numeric(input$ay), ,它的工作
    【解决方案2】:

    首先感谢@Ravi 1。我很感激。那问题解决了。我刚刚更改了 selectInput(choices=)。开始按我的意愿工作

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

    服务器.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(汽车销售)

    '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 ...
    

    【讨论】:

    • 我正在按照您的示例进行操作,我在哪里可以获得 carsales 数据集?我尝试了几个网站,但这些数据集与您的不同。
    猜你喜欢
    • 1970-01-01
    • 2019-05-13
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 2011-01-02
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多