0

I have been trying to visualise the point value from the raster that is in the leaflet map in a text output in shiny when someone clicks on the map. I was able to get the points to visualise as a popup in the map itself using leafem addimagequery but I would like to see the points in a box adjacent to the map. I was also able to visualise the latitude and longitude of the clicked point value but for some reason, I can't visualise the actual point value. The code for the app with a reproducible example is:

library(shiny)
library(shinydashboard)
library(leaflet)
library(sp)
library(raster)
library(leafem)


#example raster  
rs <- raster::raster(nrows=180, 
                    ncols=360, 
                    xmn=181823.6, 
                    xmx=281823.6, 
                    ymn=5603540, 
                    ymx=5703540, 
                    resolution=270,
                    vals = 0.5,
                    crs = CRS('+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0'))

#Color pallet for Leaflet
pal <- colorBin("PuOr", values(rs), pretty = TRUE,)


################## Defining UI for application ############################
ui <- shinyUI(dashboardPage(
                            dashboardHeader(title = "Sample dash",titleWidth = 750),
                            dashboardSidebar(collapsed = TRUE,
                                             sidebarMenu( menuItem("Map Dashboard", tabName = "datavis",  icon = icon("map", verify_fa = FALSE)))),
                            dashboardBody(
                              tabItems(
                                tabItem(tabName = "datavis",
                                        h4("Locations"),
                                        fluidRow(box(width = 4, htmlOutput('mytext')),
                                                 box(width= 8,  leafletOutput("map"))),
                                        fluidRow(box("Score (CS)",width = 4, amChart4Output("myplot")))
                                )
                              )
                            )
)
)
################## Defining Server for application ############################
server<- shinyServer(function(input,output, session){
  
  output$map <- renderLeaflet({
    leaflet() %>% 
      addProviderTiles('OpenStreetMap') %>%
      addRasterImage(rs[[1]], 
                     colors = pal, 
                     opacity = 0.8,
                     group = 'rs',
                     layerId = 'rs') %>%
      addLegend(pal = pal, values = values(rs),
                title = "Points")%>%
      leafem::addImageQuery(rs[[1]],
                            layerId = 'rs',
                            type='click',
                            prefix='Point Value For Location')
    
  })
  observe({
    click = input$map_click
    if(is.null(click))
      return()
    text<-paste("Latitude: ", click$lat)
    text1<-paste("Longtitude: ", click$lng)
    text2<-paste("Point: ", click$rs[[1]])
    text3<-paste("You've selected point:", text, text1,text2, sep="<br/>")
    output$mytext<-renderUI({
      HTML(text3)
    })
  })
  
})

shinyApp(ui=ui, server = server)

My goal is to eventually plot the point but I am not sure how to get from clicking the point to plotting it.

0 Answers0