Time series plot on shinyapp

I am very new to shiny app and leaflet.
I am trying to visualise a series of datasets (1-10 Data.nc, from 1981-2021, monthly). I managed to plot and click and view the value on the map but i couldnt manage to add adtional map (time series plot), to the left or right side of the map. I tried to use the cliked lat and lon values to extract the values from the selected raster but failed to proporly plot on the map. the click and view only prints the value for the current map/time.

r_list <- list(
  stack("data.nc"),
  stack("data2.nc"),
  stack("data3.nc")
)

ui <- fluidPage(
  tags$head(tags$style(HTML('
    #map {
      cursor: pointer;
    }

  '))),
  leafletOutput("map", width = "100%", height = "800px"),
  absolutePanel(bottom = 160, left = 10, 
                selectInput("file_select", "Select file", choices = c("data.nc", "data2.nc", "data3.nc"), width = "150px")),
  absolutePanel(bottom  = 100, left = 10, selectInput("layer_select", "Select date", choices = NULL, width = "150px")),
  verbatimTextOutput("click_info")
)
# Define server
server <- function(input, output, session) {
  ####
  observeEvent(input$file_select, {
    file_name <- input$file_select
    r <- r_list[[which(file_name == c("data.nc", "data2.nc", "data3.nc"))]]
    updateSelectInput(session, "layer_select", choices = names(r))
  })
  
  output$click_info <- renderPrint({
    click <- input$map_click
    if (is.null(click))
      return(NULL)
    file_name <- input$file_select
    r <- r_list[[which(file_name == c("data.nc", "data2.nc", "data3.nc"))]]
    layer_name <- input$layer_select
    clicked_point <- SpatialPoints(data.frame(lon = click$lng, lat = click$lat), proj4string = CRS(proj4string(r[[1]])))
    value <- extract(r[[1]], clicked_point)
    value_str <- ifelse(is.na(value), "NA", sprintf("%.2f", value))
    cat("Information: ", "Lat:", round(click$lat, 2), "Lon:", round(click$lng, 2), "Value:", value_str)
  })
  
  
  output$map <- renderLeaflet({

    file_name <- input$file_select
    r <- r_list[[which(file_name == c("data.nc", "data2.nc", "data3.nc"))]]
    r_subset <- r[[input$layer_select]]
    
    color_pal <- colorNumeric(c("#67001f", "#a50026","#d73027","#f46d43", "#fdae61","#fee090","#ffffbf","#e0f3f8","#abd9e9",
                                "#74add1","#4575b4","#313695", "#313695"), domain = c(-3.01, 3.01),
                              na.color = "transparent")
    
    # Create leaflet map
    leaflet() %>% 
      addTiles() %>%
      addRasterImage(r_subset, colors = color_pal, opacity = .7) %>%
      addLegend(pal = color_pal, values = values(r_subset),
                title = "temp") %>%
      addControl(htmltools::HTML(paste("<div style='position: fixed; bottom: 10px; left: 10px; color: blue; background-color: #f8f8f8; padding: 5px;'>This is ",
                                       tags$a(href = "", ""),".</div>")))
    
    
  })
}

shinyApp(ui, server)



This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.