我正在创建一个闪亮的应用程序,显示华盛顿州贝灵汉周围的公共果树的位置。到目前为止,我已经使它,使用户可以输入纬度/经度坐标和树木类型,并绘制在Map上的位置。最后我想让它这样的用户可以点击交互式Map视图,并添加一个点到Map。我不知道这是否可能,如果可能的话,如何创建此功能。我从我的应用程序中添加了一些代码。
library(dplyr)
library(shiny)
library(leaflet)
library(tmap)
library(sf)
dat <- data.frame(lat = c(48.7323,48.7308,
48.7301,48.7276,
48.7246,48.7323,
48.7211),
long = c(-122.4928,-122.4940,
-122.4942,-122.4939,
-122.4958,-122.4975,
-122.4946),
species = c("Apple", "Apple",
"Pear", "Plum",
"Fig", "Plum",
"Pear"),
status = rep("Confirmed",7))
ui <- fluidPage(
titlePanel("Public Fruit Trees"),
sidebarLayout(
sidebarPanel(
numericInput(inputId = "lat",
label = "Latitude DD",
value = 48.7211,step = 1e-3),
numericInput(inputId = "long",
label = "Longitude DD",
value=-122.4942,step = 1e-3),
selectInput(inputId = "species", label = "Species",
choices = c("Apple","Pear","Fig","Peach","Other")),
actionButton(inputId = "addObservation",
label = "Add tree to database")
),
mainPanel(
tmapOutput(outputId = "tmapMap"),
DT::dataTableOutput("updatedData"),
)
)
)
server <- function(input, output, session) {
theData <- reactiveValues()
theData$dat <- dat
output$tmapMap <- renderTmap({
dat2plot <- theData$dat
dat2plot <- st_as_sf(dat2plot, coords = c("long","lat"), crs=st_crs("EPSG:4326"))
map1 <- tm_shape(dat2plot) +
tm_dots(col= "species", alpha=0.8,size = 0.1) +
tm_legend(show = TRUE) +
tm_view(set.zoom.limits = c(14,16))
map1
})
observeEvent(input$addObservation,{
to_add <- data.frame(lat = input$lat,
long = input$long,
species = input$species,
status = "Uncomfirmed"
)
theData$dat <- rbind(theData$dat,to_add) # adding new data
})
output$updatedData <- DT::renderDataTable(
theData$dat
)
}
shinyApp(ui = ui, server = server)
就像我之前说的,我想知道这是否可能。是否有某种函数允许观察Map上的点击,然后返回纬度/经度坐标,允许我将这些添加到数据集?谢谢!
1条答案
按热度按时间3htmauhk1#
这是可以做到的,你正走在正确的道路上。该Map是单张Map,并且存在与之相关联的事件列表器,如用点击坐标的lng和lat更新输入的点击事件。
只需将这一行添加到您的应用程序服务器,当您单击
lng
和lat
时,输入将更新为单击位置的坐标。要了解有关此的更多信息,请查看leaflet with shiny documentation