0
ユーザーがマップをクリックしてgoを押し、そのポイントの値の表を受け取ることができるアプリケーションを作成しようとしています。私はアクセスし、出力データを取得するためにrasterStacksとして保存Dropboxに格納されている2 netCDFファイルがあります。後のコードでイベント変数を使用する
私のサーバー機能では、マップのクリックイベントを監視していますが、後でサーバー機能で緯度または経度の値を使用すると、「clng」という関数が見つかりませんでした私は経度クリック$を保存するものであること。
任意の助けをいただければ幸いです。
おかげで、 カービィ
library(shiny)
library(leaflet)
library(raster)
library(tidyverse)
library(DT)
library(mailR)
# Read NetCDF Files from public share links
if(!exists("ettemp")){
ettemp <- tempfile("et", fileext = ".nc")
}
if(!exists("preciptemp")){
preciptemp <- tempfile("precip", fileext = ".nc")
}
if(!file.exists(ettemp)){
download.file(url = "https://www.dropbox.com/s/uyq9arqgnprxzv1/ET.nc?raw=1",
destfile = ettemp,
mode = "wb")
}
if(!file.exists(preciptemp)){
download.file(url = "https://www.dropbox.com/s/sgmhq8cmth1jd8h/precip.nc?raw=1",
destfile = preciptemp,
mode = "wb")
}
precip_lowres <- stack(preciptemp)
names(precip_lowres) <- month.name
ET_stack <- stack(ettemp)
names(ET_stack) <- month.name
ui <- fluidPage(
leafletOutput("map"),
actionButton("go", "Go"),
DT::dataTableOutput(outputId = "mytable")
)
server <- shinyServer(function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
setView(lat = 35, lng = -100, zoom = 3) %>%
addTiles(options = providerTileOptions(noWrap = TRUE))
})
observeEvent(input$map_click, {
click <- input$map_click
clat <- click$lat
clng <- click$lng
leafletProxy('map') %>%
clearMarkers() %>%
addMarkers(lng=clng, lat=clat,
popup = paste(round(click$lat, 2), round(click$lng, 2)))
})
coordvals <- eventReactive(input$go, {
coords <- data.frame(cbind(clng(), clat()))
coordinates(coords) <- ~ X1 + X2
precip_vals <- data.frame(raster::extract(precip_lowres, coords)) %>%
gather(key = "Month", value = "Precip") %>%
mutate(Precip = round(Precip, 0))
ET_vals <- data.frame(raster::extract(ET_stack, coords)) %>%
gather(key = "Month", value = "ET")
combo_vals <- precip_vals %>%
left_join(ET_vals) %>%
mutate(ET = ET*3)
combo_vals$Month <- factor(combo_vals$Month, levels = month.name)
})
output$mytable <- DT::renderDataTable({
datatable(coordvals())
})
})
shinyApp(ui, server)