[R-sig-Geo] A leaflet question

Erin Hodgess er|nm@hodge@@ @end|ng |rom gm@||@com
Thu Jun 2 05:10:13 CEST 2022


Ben,

Your suggestions were totally awesome!  They worked like a charm.  Thank
you very much!

Here is the final version, just in case anyone is interested:

library(tigris)
library(leaflet)
library(sf)

#Getting the data
tarrant <- tracts(state="TX",county="Tarrant",cb=TRUE)



#Set up ui
ui <- fluidPage(
  leafletOutput("plot"),
  verbatimTextOutput("data"),

 actionButton("goButton","Calculate",class="btn-success"),
 conditionalPanel(condition="input.goButton==1",
 verbatimTextOutput("text1")
 )


)

#Set up server
server <- function(input, output, session) {

  .x5 <- reactiveValues()
  .x5$df <- numeric(0)



  output$plot <- renderLeaflet({
  leaflet(tarrant) %>% addTiles() %>%
  addPolygons(label=~TRACTCE)
  })

output$data <- renderText({
# print(tarrant$TRACTCE[.x5$df])
  paste("tract = ", tarrant$TRACTCE[.x5$df],"\n",sep="")
})

observeEvent(input$goButton, {
        lil.tarr <- tarrant[.x5$df,]

mean_land <- mean(lil.tarr$ALAND)
output$text1 <- renderText({
  paste0("The mean from this selection is: ",round(mean_land,1),sep="")
  })

})


observeEvent(input$plot_click, {
 p <- input$plot_click
    if(!is.null(input$plot_click)) {
    .x6 <- st_point(c(input$plot_click$lng,input$plot_click$lat))
    xtemp <- which(st_contains(tarrant,.x6,sparse=FALSE))
    .x5$df <- c(.x5$df,xtemp)
 }
 })

}
shinyApp(ui,server)


Again, many thanks.

Sincerely,
Erin

Erin Hodgess, PhD
mailto: erinm.hodgess using gmail.com


On Wed, Jun 1, 2022 at 2:45 PM Ben Tupper <btupper using bigelow.org> wrote:

> Hmmm.  Are you looking to see which polygon the user clicks within?  If
> so, perhaps look at sf::st_contains().  If that would do the trick then you
> could skip casting `tarrant` to sp (leave it as sf object).  Then cast the
> contents of click to to sf POINT using sf::st_as_sf().
>
> On Wed, Jun 1, 2022 at 2:22 PM Erin Hodgess <erinm.hodgess using gmail.com>
> wrote:
>
>> Thank you so much!
>>
>> Here is the error that I get from nearPoints:
>> nearPoints requires a click/hover/double-click object with x and y values.
>>
>> The "click" from the leaflet does not have the same components as the
>> "click" from a regular plot.
>>
>> Erin Hodgess, PhD
>> mailto: erinm.hodgess using gmail.com
>>
>>
>> On Wed, Jun 1, 2022 at 2:16 PM Ben Tupper <btupper using bigelow.org> wrote:
>>
>>> Hi Erin,
>>>
>>> I think it has to do with the naming convention.  If your map object is
>>> called "plot", then you should expect the input to have the name
>>> "plot_click" which you can access with `input$plot_click` You have looked
>>> for input$plot_marker_click which hasn't been defined.
>>>
>>> I'm not sure why nearPoints() throws an error in this case.
>>>
>>>
>>> ```
>>> library(tigris)
>>> library(leaflet)
>>> library(sf)
>>> options(tigris_use_cache = TRUE)
>>> #Getting the data
>>> tarrant <- tracts(state="TX",county="Tarrant",cb=TRUE)
>>> tarrant_sp <- as(tarrant,"Spatial")
>>>
>>> #Set up ui
>>> ui <- fluidPage(
>>>   leafletOutput("plot"),
>>>   tableOutput("data")
>>> )
>>> #Set up server
>>> server <- function(input, output, session) {
>>>   #Draw map
>>>   output$plot <- renderLeaflet({
>>>     leaflet(tarrant_sp) %>% addTiles() %>%
>>>       addPolygons(label=~TRACTCE)
>>>   })
>>>   #allegedly grab market clicks
>>>   output$data <- renderTable({
>>>     click <- input$plot_click
>>>     cat("click class = ", class(click), "\n")
>>>     cat(str(click), "\n")
>>>     print(nearPoints(tarrant_sp, click))
>>>   })
>>> }
>>> shinyApp(ui,server)
>>> ```
>>>
>>> On Wed, Jun 1, 2022 at 10:15 AM Erin Hodgess <erinm.hodgess using gmail.com>
>>> wrote:
>>>
>>>> Thank you for your kind response.
>>>>
>>>> Here is my code:
>>>>
>>>> library(tigris)
>>>> library(leaflet)
>>>> library(sf)
>>>>
>>>> #Getting the data
>>>> tarrant <- tracts(state="TX",county="Tarrant",cb=TRUE)
>>>> tarrant_sp <- as(tarrant,"Spatial")
>>>>
>>>> #Set up ui
>>>> ui <- fluidPage(
>>>>   leafletOutput("plot"),
>>>>   tableOutput("data")
>>>> )
>>>> #Set up server
>>>> server <- function(input, output, session) {
>>>>   #Draw map
>>>>   output$plot <- renderLeaflet({
>>>>   leaflet(tarrant_sp) %>% addTiles() %>%
>>>>   addPolygons(label=~TRACTCE)
>>>> })
>>>>   #allegedly grab market clicks
>>>>   output$data <- renderTable({
>>>>     nearPoints(tarrant_sp, input$plot_marker_click)
>>>>     print(input$plot_marker_click)
>>>>   })
>>>> }
>>>> shinyApp(ui,server)
>>>>
>>>> I would like to get the values from the click and make calculations
>>>> based on the tracts selected.  However, nothing is appearing in the
>>>> input$plot_marker_click.
>>>>
>>>> Any suggestions much appreciated.
>>>>
>>>> Sincerely,
>>>> Erin
>>>>
>>>>
>>>> Erin Hodgess, PhD
>>>> mailto: erinm.hodgess using gmail.com
>>>>
>>>>
>>>> On Wed, Jun 1, 2022 at 9:03 AM Ben Tupper <btupper using bigelow.org> wrote:
>>>>
>>>>> Hi Erin,
>>>>>
>>>>> This is a great place to ask questions - especially if you provide a
>>>>> reproducible example.
>>>>>
>>>>> Cheers,
>>>>> Ben
>>>>>
>>>>> On Wed, Jun 1, 2022 at 8:24 AM Erin Hodgess <erinm.hodgess using gmail.com>
>>>>> wrote:
>>>>>
>>>>>> Hello everyone;
>>>>>>
>>>>>> Can I ask a leaflet question here, please, or is that a topic for
>>>>>> RStudio
>>>>>> Community?
>>>>>>
>>>>>> Thanks,
>>>>>> Erin
>>>>>> --
>>>>>> Erin Hodgess, PhD
>>>>>> mailto: erinm.hodgess using gmail.com
>>>>>>
>>>>>>         [[alternative HTML version deleted]]
>>>>>>
>>>>>> _______________________________________________
>>>>>> R-sig-Geo mailing list
>>>>>> R-sig-Geo using r-project.org
>>>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Ben Tupper (he/him)
>>>>> Bigelow Laboratory for Ocean Science
>>>>> East Boothbay, Maine
>>>>> http://www.bigelow.org/
>>>>> https://eco.bigelow.org
>>>>>
>>>>>
>>>
>>> --
>>> Ben Tupper (he/him)
>>> Bigelow Laboratory for Ocean Science
>>> East Boothbay, Maine
>>> http://www.bigelow.org/
>>> https://eco.bigelow.org
>>>
>>>
>
> --
> Ben Tupper (he/him)
> Bigelow Laboratory for Ocean Science
> East Boothbay, Maine
> http://www.bigelow.org/
> https://eco.bigelow.org
>
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list