Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R闪亮传单为积分添加十字_R_Shiny_Leaflet - Fatal编程技术网

R闪亮传单为积分添加十字

R闪亮传单为积分添加十字,r,shiny,leaflet,R,Shiny,Leaflet,我试图在传单地图上将点表示为十字(+)。我已经开始学习这个例子。有两件事我想解决 1) 为什么所有的分数都没有显示为十字架 2) 我可以固定标记大小,以便在缩小或缩小标记时保持初始创建的大小,即不是动态标记。目前,如果我缩小它们,它们会变大,但我想避免这种情况 可复制代码如下 Poly = data.frame(Strat = c("A","A","A","A","A","B","B","B","B","B"), long = c(174.5012, 174.5026, 174.5026, 17

我试图在传单地图上将点表示为十字(+)。我已经开始学习这个例子。有两件事我想解决

1) 为什么所有的分数都没有显示为十字架

2) 我可以固定标记大小,以便在缩小或缩小标记时保持初始创建的大小,即不是动态标记。目前,如果我缩小它们,它们会变大,但我想避免这种情况

可复制代码如下

Poly = data.frame(Strat = c("A","A","A","A","A","B","B","B","B","B"), long = c(174.5012, 174.5026, 174.5026, 174.5014,174.5012,174.5012 ,174.5020, 174.5020,174.5012,174.5012),lat = c(-35.84014, -35.84018, -35.84137,-35.84138,-35.84014,-35.84014,-35.84014,-35.84197,-35.84197,-35.84014))
Points = data.frame(long = c(174.5014 ,174.5017, 174.5021, 174.5023, 174.5020, 174.5017 ,174.5021 ,174.5017, 174.5021, 174.5019), lat = c(-35.84187, -35.84165, -35.84220 ,-35.84121, -35.84133, -35.84034, -35.84082, -35.84101, -35.84112, -35.84084))


library('leaflet')
library('shiny')
library('webshot')
library('htmlwidgets')


# A function to create png images for each shape and color 
# for the leaflet maps
pchIcons = function(pch = 1, width = 30, height = 30, bg = "transparent", col = "black", ...) {
  n = length(pch)
  files = character(n)
  # create a sequence of png images
  for (i in seq_len(n)) {
    f = tempfile(fileext = '.png')
    png(f, width = width, height = height, bg = bg)
    par(mar = c(0, 0, 0, 0))
    plot.new()
    points(.5, .5, pch = pch[i], col = col[i], cex = min(width, height) / 8, ...)
    dev.off()
    files[i] = f
  }
  return(list("iconUrl" = files, "iconWidth" = width, "iconHeight" = height))
}
##### UI
ui <- fluidPage(
   mainPanel(leafletOutput("map"))
)
##### Server
server = function(input, output){
  output$map <- renderLeaflet({
    mymap()      
  })

  mymap <- reactive({
     leaflet() %>% addTiles(urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", attribution = NULL, layerId = NULL, group = NULL, options = tileOptions()) %>%  
     clearShapes() %>%
     clearMarkers() %>%      
     fitBounds(lng1 = 174.5042, lat1= -35.83814,lng2= 174.5001, lat2 = -35.8424) 
  })   

  myfun <- function(map) {
      print("adding points")
      map %>% clearShapes() %>%
      clearControls() %>% 
      clearMarkers() %>% 
      addCircles(lng = Points$long, lat = Points$lat, color = "blue",fillOpacity = 1,radius = 1) %>%
      addMarkers(lng = Points$long, lat = Points$lat,icon = makeIcon(iconUrl = pchIcons(pch= 3,col="blue", height = 20, width = 20),popupAnchorX = 10, popupAnchorY = 0))            
   }

  AddStrataPoly <- function(map) {
      print("adding polygons")    
      for(i in 1:length(unique(Poly$Strat))) {
        map <- map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1))
      } 
      map
    }

  observe({
    leafletProxy("map") %>% myfun() %>% AddStrataPoly() 
  })

  newmap <- reactive({
    mymap() %>% myfun() %>% AddStrataPoly()
  })
}
shinyApp(ui, server)
Poly=data.frame(Strat=c(“A”,“A”,“A”,“A”,“A”,“A”,“B”,“B”,“B”,“B”,“B”,“B”,“B”),long=c(174.5012,174.5026,174.5026,174.5014174.5012174.5012,174.5020174.5012174.5012),lat=c(-35.84014,-.84018,-.84137,--35.84138,--35.84014,-35.84014,-.84197,-35.84197,--35.8414))
点=数据帧(长=c(174.5014174.5017174.5021174.5023,174.5020,174.5017174.5021,174.5017174.5021,174.5017174.5019),纬度=c(-35.84187,-35.84165,-35.84220,-35.84121,-35.84133,-35.84034,-35.84082,-35.84101,-35.84112,-35.84084))
图书馆(“传单”)
库(‘闪亮’)
库('webshot')
库('htmlwidgets')
#用于为每个形状和颜色创建png图像的函数
#有关单张地图
pchIcons=函数(pch=1,宽度=30,高度=30,bg=“透明”,col=“黑色”,…){
n=长度(pch)
文件=字符(n)
#创建一系列png图像
(i在序号(n)中){
f=tempfile(fileext='.png')
png(f,宽度=宽度,高度=高度,背景=背景)
par(mar=c(0,0,0,0))
plot.new()
点(.5,.5,pch=pch[i],col=col[i],cex=min(宽度、高度)/8,…)
发展主任()
文件[i]=f
}
返回(列表(“iconUrl”=文件,“iconWidth”=宽度,“iconHeight”=高度))
}
#####用户界面
ui%
clearMarkers()%>%
fitBounds(lng1=174.5042,lat1=-35.83814,lng2=174.5001,lat2=-35.8424)
})   
myfun%clearShapes()%%>%
clearControls()%>%
clearMarkers()%>%
添加圆(lng=点$long,lat=点$lat,color=“blue”,fillOpacity=1,radius=1)%>%
添加标记(lng=点$long,lat=点$lat,icon=makeIcon(iconUrl=pchIcons(pch=3,col=“blue”,高度=20,宽度=20),POPUPANCORX=10,POPUPANCORY=0))
}
AddStrataPoly%myfun()%%>%AddStrataPoly()
})
新映射%myfun()%%>%AddStrataPoly()
})
}
shinyApp(用户界面、服务器)
尝试此操作(跳过两个数据帧
Poly
,使其更短):

library(“传单”)
库(‘闪亮’)
库('webshot')
库('htmlwidgets')
#用于为每个形状和颜色创建png图像的函数
#有关单张地图
pchIcons=功能(pch=3,
宽度=30,
高度=30,
bg=“透明”,
col=“黑色”,
...) {
n=长度(pch)
文件=字符(n)
#创建一系列png图像
(i在序号(n)中){
f=tempfile(fileext='.png')
巴布亚新几内亚(f,
宽度=宽度,
高度=高度,
bg=bg)
par(mar=c(0,0,0,0))
plot.new()
要点(
5.
5.
pch=pch[i],
col=col[i],
cex=最小值(宽度、高度)/8,
...
)
发展主任()
文件[i]=f
}
返回(列表(iconUrl=文件,iconWidth=宽度,iconHeight=高度))
}
#####用户界面
ui%
clearMarkers()%>%
菲特邦兹(
lng1=174.5042,
lat1=-35.83814,
lng2=174.5001,
lat2=-35.8424
)
})
myfun%clearShapes()%%>%
clearControls()%>%
clearMarkers()%>%
addCircles(
液化天然气=点数$long,
lat=分数$lat,
color=“蓝色”,
fillOpacity=1,
半径=1
) %>%
添加标记(
液化天然气=点数$long,
lat=分数$lat,
icon=makeIcon(
iconUrl=pchIcons()$iconUrl,
iconWidth=pchIcons()$iconWidth,
iconHeight=pchIcons()$iconHeight,
爆竹x=10,
popupAnchorY=0
)
)
}
AddStrataPoly%myfun()%%>%AddStrataPoly()
})
新映射%myfun()%%>%AddStrataPoly()
})
}
shinyApp(用户界面、服务器)
library('leaflet')
library('shiny')
library('webshot')
library('htmlwidgets')

# A function to create png images for each shape and color
# for the leaflet maps
pchIcons = function(pch = 3,
                    width = 30,
                    height = 30,
                    bg = "transparent",
                    col = "black",
                    ...) {
  n = length(pch)
  files = character(n)
  # create a sequence of png images
  for (i in seq_len(n)) {
    f = tempfile(fileext = '.png')
    png(f,
        width = width,
        height = height,
        bg = bg)
    par(mar = c(0, 0, 0, 0))
    plot.new()
    points(
      .5,
      .5,
      pch = pch[i],
      col = col[i],
      cex = min(width, height) / 8,
      ...
    )
    dev.off()
    files[i] = f
  }
  return(list(iconUrl = files, iconWidth = width, iconHeight = height))
}

##### UI
ui <- fluidPage(mainPanel(leafletOutput("map")))

##### Server
server = function(input, output) {
  output$map <- renderLeaflet({
    mymap()
  })

  mymap <- reactive({
    leaflet() %>% addTiles(
      urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
      attribution = NULL,
      layerId = NULL,
      group = NULL,
      options = tileOptions()
    ) %>%
      clearShapes() %>%
      clearMarkers() %>%
      fitBounds(
        lng1 = 174.5042,
        lat1 = -35.83814,
        lng2 = 174.5001,
        lat2 = -35.8424
      )
  })

  myfun <- function(map) {
    print("adding points")
    map %>% clearShapes() %>%
      clearControls() %>%
      clearMarkers() %>%
      addCircles(
        lng = Points$long,
        lat = Points$lat,
        color = "blue",
        fillOpacity = 1,
        radius = 1
      ) %>%
      addMarkers(
        lng = Points$long,
        lat = Points$lat,
        icon = makeIcon(
          iconUrl = pchIcons()$iconUrl,
          iconWidth = pchIcons()$iconWidth,
          iconHeight = pchIcons()$iconHeight,
          popupAnchorX = 10,
          popupAnchorY = 0
        )
      )
  }

  AddStrataPoly <- function(map) {
    print("adding polygons")
    for (i in 1:length(unique(Poly$Strat))) {
      map <-
        map %>% addPolygons(
          lng = Poly[Poly$Strat == unique(Poly$Strat)[i], ]$long,
          lat = Poly[Poly$Strat == unique(Poly$Strat)[i], ]$lat,
          layerId = unique(Poly$Strat)[i],
          color = 'gray60',
          options = list(fillOpacity = 0.1)
        )
    }
    map
  }

  observe({
    leafletProxy("map") %>% myfun() %>% AddStrataPoly()
  })

  newmap <- reactive({
    mymap() %>% myfun() %>% AddStrataPoly()
  })
}
shinyApp(ui, server)