2017-10-22 1 views
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) 

答えて

0

私はを初期化するためにreactiveValues()を使用下210、clat、およびclngである。それらを個別のreactive()式として定義するときれいになるかもしれません。いずれにしても、observeEvent(handlerExpr={})引数の外でそれらを使用する場合は、おそらくobserveEvent()の外部に定義する必要があります。 handlerExprを使用して値を更新します。

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)) 
    }) 

    clickValues <- reactiveValues(
    click=NULL, clat=NULL, clng=NULL) 

    observeEvent(input$map_click, { 
    clickValues$click <- input$map_click 
    clickValues$clat <- click$lat 
    clickValues$clng <- click$lng 

    leafletProxy('map') %>% 
     clearMarkers() %>% 
     addMarkers(lng=clickValues$clng, lat=clickValues$clat, 
       popup = paste(round(clickValues$click$lat, 2), 
           round(clickValues$click$lng, 2))) 

    }) 


    coordvals <- eventReactive(input$go, { 

    coords <- data.frame(cbind(clickValues$clng, clickValues$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) 
関連する問題