rshinymapsdata-analysis

How to display a map with interactive time series data?


I want to make a shiny app which displays the prevalence of infection in cattle in the Brazilian state of Rio Grande do Sul over time.

I can get the plot to work, and filter it manually outside of a shiny app. When I try to create a reactive data set and use it in the rendered plot I get a blank output. The month and year picker is present, just no plot. No error is presented.

Does anyone know what I am doing wrong?

Code in use:

library(geobr)
library(sf)
library(ggplot2)
library(shiny)

#get data to map
ts_brazil <- read.csv("Time_series_PI.csv")
ts_brazil$Date <- dmy(ts_brazil$Date)

#read in municipality data for Brazil map

all_muni <- read_municipality(
  code_muni = "RS", 
  year= 2010,
  showProgress = FALSE
  )

#join the data sets 
TS_PI <- left_join(x=ts_brazil, y= all_muni, by = 'code_muni')

#make the app
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("year", "Year", c(2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010)),
      selectInput("month", "Month", c(1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)),
    ),
    mainPanel(
      plotlyOutput("plot")
    )
  )
)

server <- function(input, output) {
  df <- reactive({
    d %>% filter(month == input$month,
                 year == input$year)
  })

  output$plot <- renderPlot({
    ggplot(data = df()) +
              geom_sf(aes(fill=Pi, geometry = geom), color=NA, size=.15, show.legend = TRUE) +
              scale_fill_distiller(palette = "Reds", direction = 1, 
                                   name='Infection Rate') + theme_void() 
  })
}

shinyApp(ui = ui, server = server)

sample data:

Date,Municipalities,Infected (Ni),Slaughtered (Ns),Pi,month,year,code_muni,La_nina
01/01/02,Aceguá,101,184,0.5489130435,1,2002,4300034,1
01/01/02,Alegrete,240,2590,0.0926640927,1,2002,4300406,1
01/02/02,Aceguá,78,229,0.3406113537,2,2002,4300034,1
01/02/02,Alegrete,288,1821,0.15815486,2,2002,4300406,1
01/03/02,Aceguá,6,113,0.0530973451,3,2002,4300034,1
01/03/02,Alegrete,224,1707,0.1312243702,3,2002,4300406,1
01/04/02,Aceguá,36,257,0.140077821,4,2002,4300034,1
01/04/02,Alegrete,185,4348,0.0425482981,4,2002,4300406,1
01/05/02,Aceguá,91,281,0.3238434164,5,2002,4300034,1
01/05/02,Alegrete,156,2173,0.0717901519,5,2002,4300406,1
01/06/02,Água Santa,0,31,0,6,2002,4300059,1
01/06/02,Alegrete,103,1204,0.0855481728,6,2002,4300406,1
01/07/02,Aceguá,26,175,0.1485714286,7,2002,4300034,1
01/07/02,Alegrete,104,1505,0.06910299,7,2002,4300406,1
01/08/02,Aceguá,86,303,0.2838283828,8,2002,4300034,1
01/08/02,Agudo,4,37,0.1081081081,8,2002,4300109,1
01/08/02,Alegrete,77,1355,0.0568265683,8,2002,4300406,1
01/09/02,Aceguá,81,246,0.3292682927,9,2002,4300034,1
01/09/02,Água Santa,0,38,0,9,2002,4300059,1
01/09/02,Alegrete,28,940,0.029787234,9,2002,4300406,1
01/10/02,Alegrete,41,562,0.0729537367,10,2002,4300406,1
01/11/02,Aceguá,0,108,0,11,2002,4300034,1
01/11/02,Água Santa,0,43,0,11,2002,4300059,1
01/11/02,Agudo,5,58,0.0862068966,11,2002,4300109,1
01/11/02,Alecrim,5,84,0.0595238095,11,2002,4300307,1
01/11/02,Alegrete,128,1495,0.0856187291,11,2002,4300406,1
01/12/02,Aceguá,1,82,0.012195122,12,2002,4300034,1
01/12/02,Alecrim,15,79,0.1898734177,12,2002,4300307,1
01/12/02,Alegrete,272,4047,0.0672102792,12,2002,4300406,1
01/01/03,Aceguá,414,1051,0.3939105614,1,2003,4300034,1
01/01/03,Alecrim,2,73,0.0273972603,1,2003,4300307,1
01/01/03,Alegrete,263,3077,0.0854728632,1,2003,4300406,1
01/02/03,Aceguá,389,976,0.3985655738,2,2003,4300034,1
01/02/03,Alecrim,0,97,0,2,2003,4300307,1
01/02/03,Alegrete,380,3847,0.0987782688,2,2003,4300406,1
01/03/03,Aceguá,551,997,0.5526579739,3,2003,4300034,1
01/03/03,Alecrim,3,82,0.0365853659,3,2003,4300307,1
01/03/03,Alegrete,293,2747,0.1066618129,3,2003,4300406,1
01/04/03,Aceguá,281,859,0.3271245634,4,2003,4300034,1
01/04/03,Alecrim,0,29,0,4,2003,4300307,1
01/04/03,Alegrete,691,4912,0.1406758958,4,2003,4300406,1
01/05/03,Aceguá,377,926,0.4071274298,5,2003,4300034,1
01/05/03,Alecrim,0,56,0,5,2003,4300307,1
01/05/03,Alegrete,378,2622,0.1441647597,5,2003,4300406,1
01/06/03,Aceguá,586,1120,0.5232142857,6,2003,4300034,1
01/06/03,Alecrim,0,15,0,6,2003,4300307,1
01/06/03,Alegrete,335,3226,0.1038437694,6,2003,4300406,1
01/07/03,Aceguá,603,1369,0.4404674945,7,2003,4300034,1
01/07/03,Água Santa,0,22,0,7,2003,4300059,1
01/07/03,Alecrim,2,108,0.0185185185,7,2003,4300307,1
01/07/03,Alegrete,320,3002,0.1065956029,7,2003,4300406,1
01/09/03,Aceguá,203,792,0.2563131313,9,2003,4300034,1
01/09/03,Água Santa,24,100,0.24,9,2003,4300059,1
01/09/03,Alecrim,0,39,0,9,2003,4300307,1
01/09/03,Alegrete,96,1707,0.0562390158,9,2003,4300406,1
01/10/03,Aceguá,502,1228,0.4087947883,10,2003,4300034,1
01/10/03,Alecrim,0,26,0,10,2003,4300307,1
01/10/03,Alegrete,220,1989,0.1106083459,10,2003,4300406,1
01/11/03,Aceguá,695,1560,0.4455128205,11,2003,4300034,1
01/11/03,Alecrim,0,37,0,11,2003,4300307,1
01/11/03,Alegrete,330,3889,0.0848547184,11,2003,4300406,1
01/12/03,Aceguá,1253,2664,0.4703453453,12,2003,4300034,1
01/12/03,Água Santa,0,75,0,12,2003,4300059,1
01/12/03,Alecrim,0,19,0,12,2003,4300307,1
01/12/03,Alegrete,516,4545,0.1135313531,12,2003,4300406,1
01/01/04,Aceguá,964,1922,0.5015608741,1,2004,4300034,1
01/01/04,Alecrim,6,73,0.0821917808,1,2004,4300307,1
01/01/04,Alegrete,563,3375,0.1668148148,1,2004,4300406,1
01/02/04,Aceguá,793,1820,0.4357142857,2,2004,4300034,1
01/02/04,Alecrim,0,65,0,2,2004,4300307,1
01/02/04,Alegrete,747,3564,0.2095959596,2,2004,4300406,1
01/03/04,Aceguá,1108,2603,0.4256626969,3,2004,4300034,1
01/03/04,Alegrete,730,4780,0.1527196653,3,2004,4300406,1
01/04/04,Aceguá,783,1732,0.4520785219,4,2004,4300034,1
01/04/04,Alegrete,623,3595,0.1732962448,4,2004,4300406,1
01/05/04,Aceguá,389,887,0.4385569335,5,2004,4300034,1
01/05/04,Alecrim,9,101,0.0891089109,5,2004,4300307,1
01/05/04,Alegrete,737,4489,0.1641791045,5,2004,4300406,1
01/06/04,Aceguá,512,1365,0.3750915751,6,2004,4300034,1
01/06/04,Alecrim,23,90,0.2555555556,6,2004,4300307,1
01/06/04,Alegrete,984,5380,0.1828996283,6,2004,4300406,1
01/07/04,Aceguá,425,1242,0.3421900161,7,2004,4300034,1
01/07/04,Alecrim,12,59,0.2033898305,7,2004,4300307,1
01/07/04,Alegrete,650,5116,0.1270523847,7,2004,4300406,1
01/08/04,Aceguá,637,1385,0.4599277978,8,2004,4300034,1
01/08/04,Água Santa,1,50,0.02,8,2004,4300059,1
01/08/04,Alecrim,0,19,0,8,2004,4300307,1
01/08/04,Alegrete,726,4665,0.1556270096,8,2004,4300406,1
01/09/04,Aceguá,430,1352,0.3180473373,9,2004,4300034,1
01/09/04,Água Santa,0,35,0,9,2004,4300059,1
01/09/04,Alegrete,657,4048,0.1623023715,9,2004,4300406,1
01/10/04,Aceguá,559,1649,0.3389933293,10,2004,4300034,1
01/10/04,Alegrete,669,4588,0.14581517,10,2004,4300406,1
01/11/04,Aceguá,941,2707,0.3476172885,11,2004,4300034,1
01/11/04,Alegrete,1101,6165,0.1785888078,11,2004,4300406,1
01/12/04,Aceguá,1626,4119,0.3947560087,12,2004,4300034,1
01/12/04,Água Santa,8,99,0.0808080808,12,2004,4300059,1
01/12/04,Alecrim,14,78,0.1794871795,12,2004,4300307,1
01/12/04,Alegrete,906,7207,0.1257111142,12,2004,4300406,1
01/01/05,Aceguá,735,2594,0.2833461835,1,2005,4300034,1
01/01/05,Água Santa,0,5,0,1,2005,4300059,1
01/01/05,Alegrete,1184,6029,0.1963841433,1,2005,4300406,1
01/02/05,Aceguá,488,1538,0.3172951886,2,2005,4300034,1
01/02/05,Alecrim,14,147,0.0952380952,2,2005,4300307,1
01/02/05,Alegrete,874,5539,0.1577902148,2,2005,4300406,1
01/03/05,Aceguá,430,1377,0.3122730574,3,2005,4300034,1
01/03/05,Alecrim,11,71,0.1549295775,3,2005,4300307,1
01/03/05,Alegrete,832,5831,0.1426856457,3,2005,4300406,1
01/04/05,Aceguá,495,1095,0.4520547945,4,2005,4300034,1
01/04/05,Alecrim,16,199,0.0804020101,4,2005,4300307,1
01/04/05,Alegrete,683,5110,0.1336594912,4,2005,4300406,1
01/05/05,Aceguá,348,1259,0.2764098491,5,2005,4300034,1
01/05/05,Alecrim,6,86,0.0697674419,5,2005,4300307,1
01/05/05,Alegrete,737,5573,0.1322447515,5,2005,4300406,1
01/06/05,Aceguá,565,1689,0.334517466,6,2005,4300034,1
01/06/05,Alecrim,4,47,0.085106383,6,2005,4300307,1
01/06/05,Alegrete,1100,5746,0.1914375218,6,2005,4300406,1
01/07/05,Aceguá,864,2370,0.364556962,7,2005,4300034,1
01/07/05,Água Santa,18,116,0.1551724138,7,2005,4300059,1
01/07/05,Agudo,1,10,0.1,7,2005,4300109,1
01/07/05,Alecrim,8,61,0.131147541,7,2005,4300307,1
01/07/05,Alegrete,821,4846,0.1694180768,7,2005,4300406,1
01/08/05,Aceguá,1132,3265,0.3467075038,8,2005,4300034,1
01/08/05,Alecrim,0,54,0,8,2005,4300307,1
01/08/05,Alegrete,967,4403,0.2196229843,8,2005,4300406,1
01/09/05,Aceguá,862,2249,0.3832814584,9,2005,4300034,1
01/09/05,Alecrim,0,52,0,9,2005,4300307,1
01/09/05,Alegrete,568,3946,0.1439432337,9,2005,4300406,1
01/10/05,Aceguá,1259,4139,0.3041797536,10,2005,4300034,1
01/10/05,Alecrim,6,44,0.1363636364,10,2005,4300307,1
01/10/05,Alegrete,817,4689,0.1742375773,10,2005,4300406,1
01/11/05,Aceguá,1137,3596,0.3161846496,11,2005,4300034,1
01/11/05,Água Santa,27,243,0.1111111111,11,2005,4300059,1
01/11/05,Alegrete,1027,6594,0.1557476494,11,2005,4300406,1
01/12/05,Aceguá,1052,3294,0.3193685489,12,2005,4300034,1
01/12/05,Agudo,0,20,0,12,2005,4300109,1
01/12/05,Alegrete,1333,9596,0.1389120467,12,2005,4300406,1

Solution

  • Change renderPlot to renderPlotly.

    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          selectInput("year", "Year", c(2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010)),
          selectInput("month", "Month", c(1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)),
        ),
        mainPanel(
          plotlyOutput("plot")
        )
      )
    )
    
    server <- function(input, output) {
      df <- reactive({
        d %>% filter(month == input$month,
                     year == input$year)
      })
    
      output$plot <- renderPlotly({
        ggplot(data = df()) +
                  geom_sf(aes(fill=Pi, geometry = geom), color=NA, size=.15, show.legend = TRUE) +
                  scale_fill_distiller(palette = "Reds", direction = 1, 
                                       name='Infection Rate') + theme_void() 
      })
    }
    
    shinyApp(ui = ui, server = server)