0

I developed app to support my hydrological modelling teaching. In it, students can select a gauging station to work with. In order to enhance the amount of information the students get from the app, I decided to update it including a map that shows the station and its associated catchment on top of a national map, together with a more detailed map like this:

Desired output

This is the code I am using (simplified to represent just my problem):

station_data <- read.csv("station_timeseries.csv",na.strings = "NaN",fileEncoding="UTF-8-BOM")
station_data$date <- as.Date(station_data$date,"%d/%m/%Y")

station_lat <- read.csv("station_coord.csv",fileEncoding="UTF-8-BOM")

GB_boundary <- st_read("./maps/GB_boundary.shp")
catchment_boundary <- st_read("./maps/Catchments.shp")

ui <- shinyUI(fluidPage(
  sidebarPanel(
    # Select station
    selectInput(inputId = "station", label = strong("Select station"),
                choices = unique(station_data$gauge_name))
    ),
  tabPanel("Catchment characteristics",plotOutput(outputId = "Map"))
)
)

server <- shinyServer(function(input, output) {
  ## Catchment and station map ----
  output$Map <- renderPlot({
    station <- station_lat[station_lat$gauge_name==input$Station,]$gauge_id
    bbox <- st_bbox(catchment_boundary[catchment_boundary$ID==station,])
    large_map <- ggplot()+
      geom_sf(data=GB_boundary,size=1,color="black",fill="cyan1")+
      geom_sf(data=catchment_boundary[catchment_boundary$ID==station,],size=1,color="black",fill="red")+
      theme_bw()
    detail_map <- ggplot()+
      geom_sf(data=GB_boundary,size=1,color="black",fill="cyan1")+
      geom_sf(data=catchment_boundary[catchment_boundary$ID==station,],size=1,color="black",fill="red")+
      coord_sf(xlim=c(bbox[1]-20000,bbox[3]+20000),ylim = c(bbox[2]-20000,bbox[4]+20000),expand = FALSE)+
      ggtitle("Catchment and station location")+
      theme_bw()
    grid.arrange(large_map,
                 detail_map,
                 ncol = 2,
                 widths = c(2,5))
  })
})

shinyApp(ui = ui, server = server)

I used the ggplot2 code you can see to generate the above image. However, when running the shiny app I get this:

Actual output

It seems to me like shiny only reads the first geom_sf command and disregarding the next two plots. Does anyone have experienced something similar? Any suggestions to solve this?

Edit: I added links to the data used in this example, stations and both maps.

Cheers! David

  • Do you think you could provide the data or chunks of the data used in this project? It's always best to provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Kat Nov 28 '21 at 00:36
  • Hey Kat, I just added links to the data I am using for the example. – David Haro Monteagudo Nov 29 '21 at 11:31
  • 1
    I went through so much to try to figure it out and it turns out it's one of those 'sigh' moments.... you wrote `input$Station`. It should be `input$station`. I can't believe neither one of us noticed that a lot sooner! – Kat Dec 03 '21 at 00:44

0 Answers0