我有一个很大的数据集,但我提供了 3 条记录,以使这个示例可以用 最小数据量。我可以同时在传单上展示所有 3 个网站,但我想 也一次显示一条记录。我的问题是,我怎样才能在地图上显示一个站点 单击输入下拉菜单的时间。例如,如何仅显示 site2 而不是显示 所有 3 条记录?我知道“开关”,但不知道如何在这里实现它。 我下面有一些代码和可重现的数据。预先感谢您的任何建议。
Location geometry
* <chr> <POINT [°]>
1 site1 (-115.1682 44.3943)
2 site2 (-114.0403 44.6845)
3 site3 (-114.8841 44.1533)
library(shiny)
library(mapview)
library(writexl)
library(tidyverse)
library(leaflet)
locations <- c("site1", "site2", "site3")
ui <- fluidPage(
#titlePanel("Screwtraps and temp logger in Idaho"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "site",
label = "Select Site",
choices = locations)),
mainPanel(leafletOutput("map",width = "100%", height="87vh")
)
)
)
server <- function(input, output, session) {
session$onSessionEnded(function() {
stopApp()
})
output$map<-renderLeaflet({
#Select all sites
final <- mapview(site, col.regions='red', alpha=1)
final@map
#How can I render my map with only the selected site from the userInput dropdown?
})
}
shinyApp(ui, server)
Data is here:
site <- structure(list(Location = c("site1", "site2", "site3"), geometry = structure(list(
structure(c(-115.1682, 44.3943), class = c("XY", "POINT",
"sfg")), structure(c(-114.0403, 44.6845), class = c("XY",
"POINT", "sfg")), structure(c(-114.8841, 44.1533), class = c("XY",
"POINT", "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"Horizontal component of 3D system.\"],\n AREA[\"World.\"],\n BBOX[-90,-180,90,180]],\n ID[\"EPSG\",4326]]"), class = "crs"), bbox = structure(c(xmin = -115.1682,
ymin = 44.1533, xmax = -114.0403, ymax = 44.6845), class = "bbox"), class = c("sfc_POINT",
"sfc"))), row.names = c(NA, 3L), sf_column = "geometry", agr = structure(c(Location = NA_integer_), class = "factor", .Label = c("constant",
"aggregate", "identity")), class = c("sf", "tbl_df", "tbl", "data.frame"
))
site
如果您想选择一个站点、多个站点或所有站点,您可以使用shinyWidgets包中的
pickerInput
。要显示单个(或多个)站点,您只需对数据进行子集化即可。使用 pickerInput
,可以更轻松地选择一些或不选择。希望这对您有帮助。
library(shiny)
library(shinyWidgets)
library(mapview)
library(leaflet)
locations <- c("site1", "site2", "site3")
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput(inputId = "site",
label = "Select Site",
choices = locations,
selected = locations,
options = list(`actions-box` = TRUE),
multiple = TRUE)),
mainPanel(leafletOutput("map",width = "100%", height="87vh")
)
)
)
server <- function(input, output, session) {
output$map<-renderLeaflet({
req(input$site)
final <- mapview(site[site$Location %in% input$site, ],
col.regions = 'red',
layer.name = "Site",
alpha = 1)
final@map
})
}
shinyApp(ui, server)