我试图用宣传单封装中的R画一个放大器和连接给出下表中的经度和纬度信息的标记.
| Observation | InitialLat | InitialLong | NewLat | NewLong | |-------------|------------|-------------|-----------|-----------| | A | 62.469722 | 6.187194 | 51.4749 | -0.221619 | | B | 48.0975 | 16.3108 | 51.4882 | -0.302621 | | C | 36.84 | -2.435278 | 50.861822 | -0.083278 | | D | 50.834194 | 4.298361 | 54.9756 | -1.62179 | | E | 50.834194 | 4.298361 | 54.9756 | -1.62179 | | F | 50.834194 | 4.298361 | 51.4882 | -0.302621 | | …
我的传单地图看起来像这样:
library(sp)
library(leaflet)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
Sr1 = Polygon(cbind(xx, yy))
Srs1 = Polygons(list(Sr1), "s1")
SpP = SpatialPolygons(list(Srs1), 1:1)
return(SpP)
}
Circle.Town <- circleFun(c(1,-1),2.3,npoints = 100)
df1 <- data.frame(long=c(0.6,1,1.4), lat=c(-2, -.8, -0.2), other=c('a', 'b', 'c'), VAM=c(10,8,6),
type=c('Public', 'Public', 'Private'), id=c(1:3)) %>%
mutate(X=paste0('<strong>id: </strong>',
id,
'<br><strong>type</strong>: ',
type,
'<br><strong>VAM</strong>: ',
VAM)) …
Run Code Online (Sandbox Code Playgroud) 我正在使用r中的传单可视化一些矢量数据,其中包含用户可能感兴趣的多个非空间变量。我想允许用户选择一个确定要素颜色的变量。我通过函数的baseGroups
参数来完成此操作addLayersControl
,其中每个组都是具有不同调色板的相同数据。这工作正常切换的特征本身的颜色,但不允许我之间legends-每一个传说我添加始终显示切换,即使我使用适当的group
范围内addLegend
。请参见下面的示例代码和屏幕截图:
data <- data.frame(long = c(-93.2, -93, -93.5), lat = c(44.9, 45, 44.9),
var1 = c(1,2,3), var2 = c(10, 9, 1))
pal1 <- colorNumeric(palette = "Blues", domain = data$var1)
pal2 <- colorNumeric(palette = "Reds", domain = data$var2)
leaflet(data) %>%
addCircleMarkers(color = ~pal1(var1), group = "var1") %>%
addCircleMarkers(color = ~pal2(var2), group = "var2") %>%
addLegend(pal = pal1, values = ~var1, group = "var1") %>%
addLegend(pal = pal2, values = ~var2, group = "var2") …
Run Code Online (Sandbox Code Playgroud) 我想创建一个Shiny应用程序,其中等值区的着色基于用户可以从中选择的许多可能的定量变量之一的数值.在简单的情况下,这是直截了当的,但是当我们有20多个变量时,我不确定最佳实践,具有非常详细的形状文件(~2300多边形).
变量可能完全相互独立,例如"总人口"或"平均温度"可能相关或不相关,但其中一些变量将具有时间关系,例如3个或更多时间点的"总人口" .
我使用的主要shapefile之一是ABS统计区2.下面我给出澳大利亚的人口密度(总人口/面积)和放大的悉尼视图,以更好地传达我感兴趣的细节水平.
我已将shapefile读入R中,并使用包中的ms_simplify()
函数大大降低了复杂性/点数rmapshaper
.
现在,就Shiny和传单而言,这就是我一直在做的事情:
在server
定义对象之前server.R
,我构建了一个包含所有所需"层"的主要地图对象.也就是说,一个传单,有许多addPolygon()
调用来定义每个"层"(组)的颜色.
# Create main map
primary_map <- leaflet() %>%
addProviderTiles(
providers$OpenStreetMap.BlackAndWhite,
options = providerTileOptions(opacity = 0.60)
) %>%
# Layer 0 (blank)
addPolygons(
data = aus_sa2_areas,
group = "blank"
) %>%
# Layer 1
addPolygons(
data = aus_sa2_areas,
fillColor = ~palette_layer_1(aus_sa2_areas$var_1),
smoothFactor = 0.5,
group = "layer_1"
) %>%
Run Code Online (Sandbox Code Playgroud)
...
# Layer N
addPolygons(
data = aus_sa2_areas,
fillColor …
Run Code Online (Sandbox Code Playgroud)我试图显示给定位置的各个点,就像相当于点图的地图。我尝试使用leaflet
R 中的库,但我只能将标记的大小映射到连续变量。是否可以将各个数据点映射为簇,而不是将标记的大小映射到连续变量?
我的数据看起来像这样
Lat,Lon,Place,People
19.877263,75.3390241,Aurangabad,1
20.2602939,85.8394548,Bhubaneshwar,2
30.7194022,76.7646552,Chandigarh,23
13.0801721,80.2838331,Chennai,25
11.0018115,76.9628425,Coimbatore,2
27.4844597,94.9019447,Dibrugarh,1
16.2915189,80.4541588,Guntur,1
17.3887859,78.4610647,Hyderabad,4
22.5677459,88.3476023,Kolkata,7
15.8309251,78.0425373,Kurnool,1
9.9256493,78.1228866,Madurai,1
Run Code Online (Sandbox Code Playgroud) 每天我需要在地图上绘制路径并添加4,5或8分钟的文本.表示汽车从起点到目的地需要多长时间(见下图).我认为使用R中的Leaflet创建一个Shiny应用会很有帮助(代码如下所示).
我使用了leaflet.extras包中的addDrawToolbar来绘制路径,如附图所示.但我不知道也无法找到如何以与绘制路径相同的方式添加文本.解决方案并不一定要在R中.我的目标是为想要做这些事情并且不知道如何编码的人创建一个应用程序.
library(shiny)
library(leaflet)
library(leaflet.extras)
ui = fluidPage(
tags$style(type = "text/css", "#map {height: calc(100vh - 20px)
!important;}"),
leafletOutput("map")
)
server = function(input,output,session){
output$map = renderLeaflet(
leaflet()%>%
addTiles(urlTemplate = "http://mt0.google.com/vt/lyrs=m&hl=en&x=
{x}&y={y}&z={z}&s=Ga")%>%
addMeasure(
primaryLengthUnit = "kilometers",
secondaryAreaUnit = FALSE
)%>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions =
selectedPathOptions()),
polylineOptions = filterNULL(list(shapeOptions =
drawShapeOptions(lineJoin = "round", weight = 8))),
circleOptions = filterNULL(list(shapeOptions =
drawShapeOptions(),
repeatMode = F,
showRadius = T,
metric = T,
feet = F,
nautic = F))) %>%
setView(lat = 45, lng …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用传单映射 ~8000 个多边形并遇到性能问题。当我在一个闪亮的应用程序中使用地图时,我想知道是否可以以某种方式缓存或预渲染地图。
请注意,在我的情况下,我有不同的多边形层,它们按照这种方法进行交换。
一个小的 MWE 是这样的:
数据可以从这里下载
library(shiny)
library(leaflet)
library(sf)
## Download Shapefile
file <- "plz-gebiete.shp"
if (!file.exists(file)) {
url <- "https://www.suche-postleitzahl.org/download_files/public/plz-gebiete.shp.zip"
zipfile <- paste0(file, ".zip")
download.file(url, zipfile)
unzip(zipfile)
}
df <- st_read(file, options = "ENCODING=UTF-8")
# If possible: pre-render the map here!
library(shiny)
ui <- fluidPage(
leafletOutput("mymap", width = "700px", height = "700px")
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data = df, weight = 1, color …
Run Code Online (Sandbox Code Playgroud) 我想创建一个传单地图,您可以在其中选择多个多边形,这将selectizeInput()
在一个闪亮的应用程序中更新。这将包括删除选定的多边形(当它在selectizeInput()
.
我稍微更改/更新了此处答案中的代码(使用 sf 而不是 sp 和更多 dplyr,我可以在其中计算出基本 R 是什么)。
多边形可能可以通过observeEvent
绑定来更新input$clicked_locations
,但不确定具体如何。
这是代码:
library(shiny)
library(leaflet)
library(sf)
library(dplyr)
#load shapefile
nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
st_transform(4326)
shinyApp(
ui = fluidPage(
"Update selectize input by clicking on the map",
leafletOutput("map"),
"I would like the selectize input to update to show all the locations clicked,",
"but also when items are removed here, they are removed on the map too, so linked to the map.",
selectizeInput(inputId …
Run Code Online (Sandbox Code Playgroud) 在谷歌地图中,搜索输入框会在用户输入时自动完成地址。有没有办法在 R Shiny 中通过访问自动完成值来做到这一点,以便与映射包一起使用?
有一个JavaScript方法在这里。我尝试在下面的代码中在 R Shiny 中使用此方法。SymbolixAU 指出使用google_map( search_box = TRUE )
这是一个简单的解决方案。不幸的是,它在我的代码中不起作用,也因为我希望搜索框与地图分开。
下面的尝试在页面上按此顺序有文本输入my_address
、文本输出copy_of_address
和 googleway 地图my_map
。
预期行为是让用户在 text input 中输入文本my_address
,使用地址自动完成(这是有效的),地址将被复制到文本输出中copy_of_address
(这仅显示输入的内容,而不是自动完成的版本),最后是地图以这个地址为中心。
看到输入框具有自动完成地址,但是地址和地图的副本仅使用用户输入文本。
在下面的代码中,替换MyKey
为您的 google api 密钥(有时可以使用空字符串)。
library(shiny)
library(googleway)
key <- "MyKey"
set_key(key = key)
google_keys()
ui <- shiny::basicPage(
div(
textInput(inputId = "my_address", label = "")
,textOutput(outputId = "copy_of_address")
,HTML(paste0("
<script>
function initAutocomplete() {
new google.maps.places.Autocomplete(
(document.getElementById('my_address')),
{types: ['geocode']}
);
}
</script>
<script src='https://maps.googleapis.com/maps/api/js?key=", key,"&libraries=places&callback=initAutocomplete'
async …
Run Code Online (Sandbox Code Playgroud)