当从下拉菜单中选择年份时,R闪亮图不变

时间:2018-09-11 04:52:45

标签: r shiny

我使用R Shiny软件包创建了从2008年到2018年的下拉列表。我在数据库中绘制了一个图表,该图表比较了美国汽车制造商的车型数量和汽车制造商的车型数量。使用plotly包和数据库连接的图形。我想逐年进行模型比较。但是当我从下拉列表中选择年份时,图形没有变化。每年的选择都没有变化。R代码在下面给出

library(shiny)
library(plotly)
library(data.table)
library(DBI)
library(RMySQL)
library(webshot)
dealerinventory1<-dbConnect(RMySQL::MySQL(), user='gbfb', 
                            password='gfhgfhh!', 
                            host='ffdhgfh', 
                            dbname='gdfhgfhn')

param_modelyear=c(2008:2018)
for ( i in modelyr){ 
  print(i)
}


modelyearquery=paste0(" (SELECT A.makename as AutonginMake,Case when B.makename is null Then A.makename
                      Else B.makename end As USMakename
                      ,A.AutonginModelcount,Case when B.USModelcount is null Then 0
                      Else B.USModelcount end as USModelcount
                      FROM (select count(*) as AutonginModelcount,makename as makename from
                      (select distinct makename ,modelname  from dealer_inventory.car_inventory_json_lookup where modelyear=",param_modelyear," order by makename) as C
                      group by makename) A Left JOIN (select count(*) as USModelcount,makename as makename from
                      (select distinct makename ,modelname  from dealer_inventory.CarQuery where modelyear=",param_modelyear," order by makename) as D
                      group by makename) B ON A.makename=B.makename
                      ORDER BY A.makename)
                      UNION
                      (SELECT  Case when A.makename is null Then B.makename
                      Else A.makename end As AutonginMake,B.makename as USMakename,Case when A.AutonginModelcount is null Then 0
                      Else A.AutonginModelcount end as AutonginModelcount,B.USModelcount
                      FROM (select count(*) as AutonginModelcount,makename as makename from
                      (select distinct makename ,modelname  from dealer_inventory.car_inventory_json_lookup where modelyear=",param_modelyear," order by makename) as C
                      group by makename) A Right JOIN (select count(*) as USModelcount,makename as makename from
                      (select distinct makename ,modelname  from dealer_inventory.CarQuery where modelyear=",param_modelyear," order by makename) as D
                      group by makename) B ON A.makename=B.makename
                      ORDER BY A.makename);")
makemodel1=dbGetQuery(dealerinventory1,modelyearquery) 
plot5 <- plot_ly(makemodel1, x = ~AutonginMake, y = ~USModelcount, type = 'bar', name = 'USModelcount') %>%
  add_trace(y = ~AutonginModelcount, name = 'AutonginModelcount') %>%
  layout(
    title="Yearwise comparison of Models of each Makes in Autongin and US market",
    yaxis = list(title = 'modelcount'), 
    barmode = 'group')

k <- shinyUI(fluidPage(selectInput("my_dropdown","Choose Column", 
                                   param_modelyear),
                       plotlyOutput("plot")))

server5<- function(input,output){
  output$plot=renderPlotly({
    plot_ly(makemodel1, x = ~AutonginMake, y = ~USModelcount, type = 'bar', name = 'USModelcount') %>%
      add_trace(y = ~AutonginModelcount, name = 'AutonginModelcount') %>%
      layout(
        title="Yearwise comparison of Models of each Makes in Autongin and US market",
        yaxis = list(title = 'modelcount'), 
        barmode = 'group')
  })
}

shinyApp(k,server5)

1 个答案:

答案 0 :(得分:0)

作为绘图代码的一部分,您正在使用原始数据集本身。相反,您应该根据所选年份过滤数据集,然后在绘图中使用修改后的数据集。这是您的代码的外观:

server <- function(input,output) {

                ## Reactive function which subsets original data according to 
                ## selected year

                df <- reactive {
                   subset(makemodel1, makemodel1$year == input$my_dropdown)
                          }

               output$plot=renderPlotly({
               data <- data.frame(df())
               plot_ly(data, x = ~AutonginMake, y = ~USModelcount, type = 'bar', 
                       name = 'USModelcount') %>%
               add_trace(y = ~AutonginModelcount, name = 'AutonginModelcount') %>%
               layout(
               title="Yearwise comparison of Models of each Makes in Autongin and 
               US market",
               yaxis = list(title = 'modelcount'), 
               barmode = 'group')
                })

}

希望这会有所帮助!