[R-sig-Geo] A leaflet question

Ben Tupper btupper @end|ng |rom b|ge|ow@org
Wed Jun 1 20:45:02 CEST 2022


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