相关疑难解决方法(0)

R小册子中的标记鼠标点击事件有光泽

如何在R中的传单地图中的标记上接收鼠标单击事件?我正在使用RStudio /传单并通过Shiny运行.

我想获取标记的值(例如,ID)并使用它来更新sidebarPanel.

r markers mouseclick-event leaflet shiny

22
推荐指数
1
解决办法
1万
查看次数

在R中确定小册子中光标上单击的位置

我正在使用rasterR在R leaflet地图上绘制一个大型的Lat-lon NetCDF shinydashboard.当我点击地图时,会弹出一个弹出窗口,显示所点击的栅格点的行,列,纬度位置和值.(参见下面的可重复代码)

问题是如果光栅足够大,我正在经历光栅的移动.例如,在这里我点击了一个应该有一个值的点,但结果是所识别的点是上面的点.

在此输入图像描述

我相信这与leaflet投影使用的光栅的事实有关,而我用来识别点的原始数据是Lat-Lon,因为点击的点被返回为Lat-Lon leaflet.我不能使用投影文件(depth),因为它的单位是米,而不是度数!即使我试图将这些米重新投射到度数,我也有了转变.

这是代码的基本可运行示例:

#Libraries
library(leaflet)
library(raster)
library(shinydashboard)
library(shiny)

#Input data
download.file("https://www.dropbox.com/s/y9ekjod2pt09rvv/test.nc?dl=0", destfile="test.nc")
inputFile    = "test.nc"
inputVarName = "Depth"
lldepth <- raster(inputFile, varname=inputVarName)
lldepth[Which(lldepth<=0, cells=T)] <- NA #Set all cells <=0 to NA
ext <- extent(lldepth)
resol <- res(lldepth)
projection(lldepth) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

#Project for leaflet
depth <- projectRasterForLeaflet(lldepth)

#Prepare UI
sbwidth=200
sidebar <- dashboardSidebar(width=sbwidth)
body <- dashboardBody(
          box( #https://stackoverflow.com/questions/31278938/how-can-i-make-my-shiny-leafletoutput-have-height-100-while-inside-a-navbarpa
            div(class="outer",width = NULL, solidHeader = …
Run Code Online (Sandbox Code Playgroud)

r leaflet shiny r-raster shinydashboard

7
推荐指数
1
解决办法
2152
查看次数

单击地图后,ObserveEvent 不会打印出图层 ID

我试图通过点击地图来获取国家信息,但国家名称或 Long /lat 没有打印出来。

任何人都可以在这个问题上启发我吗?

谢谢

该地图来自自然地球。

library(rgdal)
library(shiny)
library(leaflet)

folder="."
country <- readOGR(dsn = folder, layer = "ne_110m_admin_0_countries", encoding="UTF-8")
country <- spTransform(country, CRS("+proj=longlat +ellps=GRS80"))

ui<-fluidPage(
  leafletOutput('mymap')
)

server <- function(input, output, session){
  # RV<-reactiveValues(Clicks=list())
  output$mymap<- renderLeaflet(
    leaflet() %>%
      addProviderTiles("CartoDB.Positron") %>%
      addPolygons(data = country, layerId= ~ADMIN, fillColor = "red", fillOpacity = 1,  color = "black", 
                  stroke = T,  weight = 1  
      )
  )
  observeEvent(input$map_shape_click, {
    p<- input$map_shape_click 
    print(p$id) # click on map, no response
  }) 
}

shinyApp(ui=ui, server=server)
Run Code Online (Sandbox Code Playgroud)

r leaflet shiny

4
推荐指数
1
解决办法
617
查看次数