I am trying to display a raster over my shapefile in my shiny app. Here's my approach.
# Load libraries and create sample data
library(raster)
library(shiny)
library(dplyr)
library(leaflet)
library(ggplot2)
library(ggthemes)
library(tmap)
fr.shp <- getData('GADM', country='FRA', level=1)
coords <- data.frame(coordinates(fr.shp))
names(coords) <- c('lon', 'lat')
dat.df <- expand.grid(lon = coords[, 1],
seasonID = c('winter', 'summer'),
business = c('train', 'bus', 'taxi'),
variable = c('modP', 'modT'),
YearRef = 2001:2003)
dat.df1 <- coords %>% dplyr::left_join(dat.df) %>% dplyr::mutate(value = rnorm(n()))
Below is my shiny app that shows the values for a given combination of seasonID, businees, variable and YearRef. The code that I have commented out was supposed to also plot the shapefile over which the raster should be plotted. However it is not working.
ui <- fluidPage(
titlePanel('my shiny'),
sidebarLayout(position = 'left',
sidebarPanel(
selectInput(inputId = 'seasonRef', label = 'Select season', choices = c('winter','summer'), selected = 'Kharif'),
selectInput(inputId = 'transport', label = 'Select transport',
choices = c('train','bus','taxi'), selected = 'train'),
checkboxGroupInput(inputId = 'type', label = NULL,
choiceNames = c('modP','modT'),
choiceValues = c('modP','modT'),
width = '400px', selected = 'modP'),
sliderInput('yearRef','Select Year',min=2001,max=2003,value=1)
),
mainPanel(
tabsetPanel(
tabPanel('myplots', plotOutput(outputId = 'rast')),
leafletOutput("my_tmap")
)
)
)
)
server <- function(input, output) {
tempI <- reactive({dat.df1 %>% dplyr::filter(seasonID == input$seasonRef &
business == input$transport &
YearRef == input$yearRef &
variable == input$type)})
output$rast <- renderPlot({
ggplot() + geom_raster(data = tempI(), aes(x = lon, y = lat, fill = value)) +
theme_map() + coord_equal() + scale_fill_viridis_c(option = 'C')
})
# tmap_mode("view")
# output$my_tmap = renderLeaflet({
# qtm(fr.shp)})
}
shinyApp(ui, server)
I show this question but this only lets a background shapefile to be used instead of my own shapefile.