闪亮的应用程序根据输入不改变输出

时间:2016-04-01 15:10:40

标签: r ggplot2 shiny

我使用以下代码创建了一个Shiny App:

library(shiny)
ui <- fluidPage(
  selectInput("mr",
              label="Type of Merchandise",
              choices=c("Groceries", "WhiteGoods", "HomeGoods","Clothes"),
              selected="Groceries"),
  plotOutput("curv")
)

server <- function(input, output){
  output$curv <- renderPlot({
    ggplot(new, aes(x=Year, y=Sales, colour = input$mr)) +
      geom_point(alpha=.3) +
      geom_smooth(alpha=.2, size=1) +
      ggtitle("Fitted curve for overall sales")
  })
}

shinyApp(ui = ui, server = server)

当我运行代码时,它给了我一个静态图并且不会根据输入而改变。我想要实现的是当输入是&#34; Groceries&#34;时,图表应该显示该输入的销售数据。但在这种情况下,它没有得到更新。 这是输出的屏幕截图: enter image description here

我使用的数据集是:

structure(list(Merchandise = structure(c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L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 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L), .Label = c("Shop01", "Shop02", "Shop03", "Shop04", 
"Shop05", "Shop06", "Shop07", "Shop08", "Shop09", "Shop10"), class = "factor"), 
    Year = c(2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 
    2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2014L, 2014L, 2014L, 
    2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
    2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
    2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
    2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 
    2014L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
    2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
    2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
    2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 
    2013L, 2013L, 2013L, 2013L, 2013L, 2012L, 2012L, 2012L, 2012L, 
    2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
    2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
    2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
    2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
    2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
    2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
    2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
    2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
    2011L, 2011L, 2011L, 2011L), Total.Sales = c(45547000, 28775000, 
    17029000, 28827000, 38929000, 10740000, 15008000, 10013000, 
    14556000, 10669000, 14334000, 6521000, 30025000, 5899000, 
    5333919, 1915000, 8312000, 4053000, 3506000, 3492000, 11601000, 
    7730000, 7519000, 14426000, 7046000, 1875000, 7902000, 6775000, 
    6063000, 15843000, 19047000, 8250000, 21218000, 7161000, 
    6812000, 1728000, 9807000, 5543000, 5012000, 4790000, 71817000, 
    43940000, 35523000, 56430000, 70254000, 19149000, 28253000, 
    133249000, 27730000, 25198000, 15233000, 10179000, 24703000, 
    9482000, 8850000, 2800000, 11749000, 8734000, 7753000, 7698000, 
    10135000, 9094000, 8669000, 11978000, 8438000, 2391000, 9526000, 
    7978000, 8791000, 13948000, 18087000, 8626000, 21835000, 
    6935000, 6134000, 2380000, 9306000, 5787000, 4388000, 4755000, 
    80738000, 43987000, 51321000, 54873000, 73234000, 18765000, 
    38748000, 103439000, 36943000, 27176000, 12348000, 95600000, 
    18047000, 9104000, 8190000, 2096000, 9719000, 7557000, 7503000, 
    6918000, 7530000, 5456000, 5452000, 7459000, 5218000, 1615000, 
    5613000, 4532000, 4147000, 6534000, 18168000, 7462000, 19321000, 
    5819000, 5733269, 1994284, 8622000, 5745984, 4532426, 4395000, 
    71823000, 38904000, 35842000, 57027000, 52850000, 19012000, 
    28645000, 0, 0, 0, 10408000, 7429000, 10915000, 6997000, 
    5571000, 1559000, 8543000, 0, 0, 0, 7314000, 5396000, 5041000, 
    9620000, 4468000, 1304000, 5426000, 0, 0, 0, 13694000, 7615000, 
    20337000, 7569000, 6201000, 135200, 8673000, 0, 0, 0, 65325000, 
    47179000, 29659000, 37861000, 58183000, 16129000, 23828000, 
    0, 0, 0, 13237000, 8825000, 13981000, 7841000, 4597000, 1155589, 
    13109000, 0, 0, 0, 6412000, 4998000, 4776000, 8306000, 5749000, 
    1539000, 6013000, 0, 0, 0, 16683000, 6223000, 17941000, 5740000, 
    4019000, 1453000, 7130000, 0, 0, 0), Latitude = c(-37.8594, 
    -37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214, 
    -42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697, 
    -34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405, 
    -28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187, 
    -31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594, 
    -37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214, 
    -42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697, 
    -34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405, 
    -28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187, 
    -31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594, 
    -37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214, 
    -42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697, 
    -34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405, 
    -28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187, 
    -31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594, 
    -37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214, 
    -42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697, 
    -34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405, 
    -28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187, 
    -31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594, 
    -37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214, 
    -42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697, 
    -34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405, 
    -28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187, 
    -31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594, 
    -37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214, 
    -42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697, 
    -34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405, 
    -28.0632, -37.8594, -37.8532, -38.3697, -34.3698, -33.4187, 
    -31.0025, -28.8214, -42.8826, -27.405, -28.0632, -37.8594, 
    -37.8532, -38.3697, -34.3698, -33.4187, -31.0025, -28.8214, 
    -42.8826, -27.405, -28.0632, -37.8594, -37.8532, -38.3697, 
    -34.3698, -33.4187, -31.0025, -28.8214, -42.8826, -27.405, 
    -28.0632), Longitude = c(144.7994, 147.5463, 142.4617, 150.6471, 
    149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859, 
    144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511, 
    153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463, 
    142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216, 
    152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471, 
    149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859, 
    144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511, 
    153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463, 
    142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216, 
    152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471, 
    149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859, 
    144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511, 
    153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463, 
    142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216, 
    152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471, 
    149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859, 
    144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511, 
    153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463, 
    142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216, 
    152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471, 
    149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859, 
    144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511, 
    153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463, 
    142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216, 
    152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471, 
    149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859, 
    144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511, 
    153.26, 147.3216, 152.4424, 148.2859, 144.7994, 147.5463, 
    142.4617, 150.6471, 149.5702, 150.1511, 153.26, 147.3216, 
    152.4424, 148.2859, 144.7994, 147.5463, 142.4617, 150.6471, 
    149.5702, 150.1511, 153.26, 147.3216, 152.4424, 148.2859, 
    144.7994, 147.5463, 142.4617, 150.6471, 149.5702, 150.1511, 
    153.26, 147.3216, 152.4424, 148.2859)), .Names = c("Merchandise", 
"Stores", "Year", "Total.Sales", "Latitude", "Longitude"), row.names = c(NA, 
-200L), class = "data.frame")

我错过了代码中的内容吗?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这是一个工作版本。 new[new$Merchandise==input$mr,]根据您的选择过滤数据。还有一些列名不匹配,我已经更改了它们。

library(shiny)
library(ggplot2)

ui <- fluidPage(
    selectInput("mr",
                label="Type of Merchandise",
                choices=c("Groceries", "White goods", "Home goods","Clothes"),
                selected="Groceries"),
    plotOutput("curv")
)

server <- function(input, output){

    output$curv <- renderPlot({
        ggplot(new[new$Merchandise==input$mr,], aes(x=Year, y=Total.Sales)) +
            geom_point(alpha=.3) +
            geom_smooth(alpha=.2, size=1) +
            ggtitle("Fitted curve for overall sales")
    })
}

shinyApp(ui = ui, server = server)