如何在浏览器中的R Shiny中更改Leaflet中的光标

时间:2018-04-03 02:16:32

标签: r shiny leaflet

我有一个使用Shiny的交互式R Markdown文档,其中包含一个Leaflet地图控件。当我在本地运行应用程序时它很好,但是当我在浏览器中运行它时,光标变为“抓手” - 我希望它保持为指针。我该怎么做呢?我已经找到了关于修改CSS文件或运行Javascript或JQuery的各种建议,但是我没有成功地让它们在R中工作。谢谢。

---
title: "Leaflet Reprex"
runtime: shiny
output: html_document
---

```{r echo=FALSE, eval=TRUE}
library(leaflet) 

my <- reactiveValues(long=175.619105, lat=-40.386396)
v <- reactiveValues(zoom=5, minzoom=5, maxzoom=15, long=NA, lat=NA) 

isolate({
  cat(file=stderr(), paste("initialise location"), "\n")
  v$long <- my$long
  v$lat <- my$lat
})

#### make initial map ####
output$map <- renderLeaflet({
  cat(file=stderr(), paste("render leaflet"), "\n")

    isolate({ # prevent redraw if arguments change

      leaflet(options=leafletOptions(minZoom=v$minzoom, maxZoom=v$maxzoom)) %>%
        setView(v$long, v$lat, zoom=v$zoom)  %>%
        addTiles() %>% # default map
        addMarkers(my$long, my$lat, "layer1", options=pathOptions(clickable=FALSE)) 

    })

  }) # end renderLeaflet

#### ui ####
shinyUI(fluidPage(
      leafletOutput("map", width="100%", height=480) # can manipulate size here
)) # end fluidPage

#### react to mouse clicks ####
observeEvent(input$map_click, {

  cat(file=stderr(), "\n")
  cat(file=stderr(), paste("observed map_click"), "\n")    

  click <- input$map_click
  my$long <- click$lng
  my$lat <- click$lat

  # mark map
  leafletProxy("map", deferUntilFlush=FALSE) %>%
    addMarkers(my$long, my$lat, "layer1", options=pathOptions(clickable=FALSE)) 

}) # end observe mouse click

```

1 个答案:

答案 0 :(得分:2)

你走在正确的轨道上!您可以在传单块之前添加css:

```{css, echo = FALSE}
.leaflet-container {
  cursor: auto !important;
}
```

我从this answer获得了css,从this thread获得了chunk位置。如果您有兴趣,会列出不同的光标选项here

enter image description here

---
title: "Leaflet Reprex"
runtime: shiny
output: html_document 
---

```{css, echo = FALSE}
.leaflet-container {
  cursor: auto !important;
}
```

```{r echo=FALSE, eval=TRUE}
library(leaflet) 

my <- reactiveValues(long=175.619105, lat=-40.386396)
v <- reactiveValues(zoom=5, minzoom=5, maxzoom=15, long=NA, lat=NA) 

isolate({
  cat(file=stderr(), paste("initialise location"), "\n")
  v$long <- my$long
  v$lat <- my$lat
})

#### make initial map ####
output$map <- renderLeaflet({
  cat(file=stderr(), paste("render leaflet"), "\n")

    isolate({ # prevent redraw if arguments change

      leaflet(options=leafletOptions(minZoom=v$minzoom, maxZoom=v$maxzoom)) %>%
        setView(v$long, v$lat, zoom=v$zoom)  %>%
        addTiles() %>% # default map
        addMarkers(my$long, my$lat, "layer1", options=pathOptions(clickable=FALSE)) 

    })

  }) # end renderLeaflet

#### ui ####
shinyUI(fluidPage(
      leafletOutput("map", width="100%", height=480) # can manipulate size here
)) # end fluidPage

#### react to mouse clicks ####
observeEvent(input$map_click, {

  cat(file=stderr(), "\n")
  cat(file=stderr(), paste("observed map_click"), "\n")    

  click <- input$map_click
  my$long <- click$lng
  my$lat <- click$lat

  # mark map
  leafletProxy("map", deferUntilFlush=FALSE) %>%
    addMarkers(my$long, my$lat, "layer1", options=pathOptions(clickable=FALSE)) 

}) # end observe mouse click

```