2013-07-04 2 views
6

ggmap을 사용하여 정적지도를 가져 오는 반짝이는 앱을 만들었습니다. 그러나 우편 번호 경계를 오버레이 할 때 ggplot이 데이터 세트를 찾을 수없는 오류가 발생합니다.빛나는 응용 프로그램 - ggplot이 데이터를 찾을 수 없습니다.

데이터 세트 poa는 포스트 코드 경계의 데이터 프레임, 즉 폴리곤 ID가있는 lats 및 lons입니다. 나는 이미 environment = environment()을 추가하려고 시도했지만 문제가 해결되지 않습니다. R 콘솔에 인쇄하는 print(str(poa))으로 데이터가 존재한다는 것을 알고 있습니다.

ggplot이 poa 데이터 프레임에 액세스 할 수 있도록 누군가가 나를 위해 해결책을 제안 할 수 있습니까? 이것이 아주 재현 할 수없는 사례라는 점에 사과드립니다.

업데이트 : ggplot 나는이 코드를 사용할 때 POA의 dataframe에 액세스 할 수 있습니다 :

print(ggmap(map)) + geom_polygon(data = poa, aes(x = LON, y = LAT, group = order), alpha = .5, colour = "black", fill = NA)) 

을하지만 그렇게 할 때 나는지도의 기본 계층에 대한 ggplot하는 중첩 된 전화를 걸 필요 ggplot 여기

내 server.R 코드 데이터를 찾을 수 없습니다 나는 내 ui.R에서 액션 버튼이 있고 난 단지 그것을 클릭했을 때 업데이트 할 음모를 원하는대로 분리 사용하고

.

library(shiny) 
library(ggmap) 
library(RODBC) 

# Define server logic required to summarize and view the selected dataset 
shinyServer(function(input, output) { 

output$searchString <- renderText({ 
    if (input$searchButton == 0) 
     return()   
    isolate({input$searchString}) 
}) 

mapSourceInput <- reactive({ 
    switch(input$mapSource 
      , "google" = "google" 
      , "stamen" = "stamen") 
}) 

mapTypeInput <- reactive({ 
    switch(input$mapType 
      , "terrain" = "terrain" 
      , "satellite" = "satellite" 
      , "roadmap" = "roadmap" 
      , "hybrid" = "hybrid" 
      , "toner" = "toner" 
      , "watercolor" = "watercolor") 
}) 

overlayInput <- reactive({ 
    switch(input$overlay 
      , "postcodes" = "postcodes" 
      , "states" = "states" 
      , "nothing" = "nothing") 
}) 

output$map <- renderPlot({ 
    if (input$searchButton == 0) 
     return() 

    isolate({ 
     if (overlayInput() == "nothing"){ 
      map <- get_map(location = input$searchString, zoom = input$zoom, source = mapSourceInput(), maptype = mapTypeInput()) 
      mapPlot <- ggmap(map) 
      print(mapPlot) 
      #return()    
     } else { 
      if (overlayInput() == "postcodes"){ 
       #postcode boundaries 
       map <- get_map(location = input$searchString, zoom = input$zoom, source = mapSourceInput(), maptype = mapTypeInput()) 
       poa <- structure(list(POAOBS = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L) 
           , COORD_REF = 1:10 
           , COORD_POL = 1:10 
           , POLYGON = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L) 
           , LON = c(144.951431274414, 144.956451416016, 144.95539855957, 144.955993652344, 144.958465576172, 144.956634521484, 144.956817626953, 144.954727172852, 144.957550048828, 144.958831787109) 
           , LAT = c(-37.8131675720215, -37.8117561340332, -37.8094863891602, -37.8058776855469, -37.8061485290527, -37.8021659851074, -37.8010902404785, -37.7994079589844, -37.7997169494629, -37.799861907959) 
           , POA = c("3000", "3000", "3000", "3000", "3000", "3000", "3000", "3000", "3000", "3000")) 
          , .Names = c("POAOBS", "COORD_REF", "COORD_POL", "POLYGON", "LON", "LAT", "POA") 
          , row.names = c(NA, 10L) 
          , class = "data.frame") 

       print(str(poa)) 

       print(ggmap(map, base_layer = ggplot(data = poa, aes(x = LON, y = LAT), environment = environment()), extent = "normal", maprange = FALSE, environment = .GlobalEnv) + 
          geom_polygon(data = poa, aes(x = LON, y = LAT, group = order), alpha = .5, colour = "black", fill = NA) + 
          coord_map(projection = "mercator", 
            xlim = c(attr(map, "bb")$ll.lon, attr(map, "bb")$ur.lon), 
            ylim = c(attr(map, "bb")$ll.lat, attr(map, "bb")$ur.lat))) 

      } else { 
       if(overlayInput() == "states"){ 
        return() 
       }}} 
    }) 
}) 

}) 

편집 : 추가 ui.R sessionInfo의

library(shiny) 
# Define UI for dataset viewer application 
shinyUI(pageWithSidebar(

# Application title. 
headerPanel("The New Map App"), 

# Sidebar with controls 
sidebarPanel(  
    textInput("searchString", "Get Map Of", value = "melbourne, australia") 

    , selectInput("mapSource", "Choose a Map Source", choices = c("google", "stamen")) 

    , numericInput("zoom", "Zoom Level", 10) 

    , helpText("Note: An integer from 3 (continent) to 21 (building), default value 10 (city)") 

    , selectInput("mapType", "Choose a Map Type", choices = c("terrain", "satellite", "roadmap", "hybrid", "toner", "watercolor")) 

    , helpText("Note: Options available are 'terrain', 'satellite', 'roadmap', and 'hybrid' (google maps), 'watercolor', and 'toner' (stamen maps)") 

    , radioButtons("overlay", "Overlay Polygon", 
        list("Postcodes" = "postcodes" 
         , "States" = "states" 
         , "Nothing" = "nothing")) 

    , actionButton("searchButton", "Get Map") 
    , tags$style(type='text/css', "button#searchButton { margin-bottom: 9px; }") 
), 

#output panel 
mainPanel(
    h3(textOutput("searchString")) 
    , plotOutput("map") 

) 
)) 

출력 :

R version 3.0.1 (2013-05-16) 
Platform: x86_64-pc-linux-gnu (64-bit) 

locale: 
[1] LC_CTYPE=en_AU.UTF-8  LC_NUMERIC=C    LC_TIME=en_AU.UTF-8   LC_COLLATE=en_AU.UTF-8  LC_MONETARY=en_AU.UTF-8 
[6] LC_MESSAGES=en_AU.UTF-8 LC_PAPER=C     LC_NAME=C      LC_ADDRESS=C    LC_TELEPHONE=C    
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] RODBC_1.3-7  ggmap_2.3  ggplot2_0.9.3.1 shiny_0.6.0  

loaded via a namespace (and not attached): 
[1] bitops_1.0-5  caTools_1.14  colorspace_1.2-2 dichromat_2.0-0   digest_0.6.3  grid_3.0.1   
[7] gtable_0.1.2  httpuv_1.0.6.3  labeling_0.1   mapproj_1.2-1   maps_2.3-2   MASS_7.3-26   
[13] munsell_0.4   plyr_1.8   png_0.1-5   proto_0.3-10   RColorBrewer_1.0-5 Rcpp_0.10.4   
[19] reshape2_1.2.2  RgoogleMaps_1.2.0.3 rjson_0.2.12  RJSONIO_1.0-3  scales_0.2.3  stringr_0.6.2  
[25] tools_3.0.1   xtable_1.7-1 
+0

ui.R을 게시 할 수 있습니까? 쉽게 테스트 할 수 있습니다. – MadScone

+0

확실한 건 @MadScone ui.R, 감사합니다. – user1414259

+0

이게 도움이 되나요? http://stackoverflow.com/questions/14810409/save-plots-made-in-a-shiny-app –

답변

5

그래서 나는 마침내 이것을 알아 냈습니다. 이 중첩 된 쿼리에서 찾을 수 없습니다 POA를 설정 예를 들어

데이터 -

가 중첩 된 ggplot 쿼리에 설정된 데이터를 찾을 수 ggplot하도록하려면 질문에 설정된 데이터는 < <를 사용하여 할당해야 함수 안에서 호출 될 때.

print(ggmap(map)) + geom_polygon(data = poa, aes(x = LON, y = LAT, group = order), alpha = .5, colour = "black", fill = NA)) 

그래서 당신은 도움에서이 라인

poa <<- poa 

를 사용하는 기능에 POA를 사용해야하기 전에 : "연산자 < < -와 -하는 것은 >> 일반적으로 단지 기능에 사용되며, 할당 된 변수의 기존 정의에 대한 부모 환경을 통한 검색이 이루어집니다. "

2

이 까다로운 일이다. ggplot의 환경 중첩에 대해 좀 더 잘 알게되면이 답변을 편집 할 수 있습니다. 하지만 여기에 나를 위해 작동하도록 만드는 수정 된 덩어리가 있습니다 :

 env <- environment() 

     print(ggmap(map, base_layer = ggplot(data = poa, aes(x = LON, y = LAT), environment=env), extent = "normal", maprange = FALSE, environment=environment()) + 
        geom_polygon(data = poa, aes(x = LON, y = LAT), alpha = .5, colour = "black", fill = NA, environment=env) + 
        coord_map(projection = "mercator", 
          xlim = c(attr(map, "bb")$ll.lon, attr(map, "bb")$ur.lon), 
          ylim = c(attr(map, "bb")$ll.lat, attr(map, "bb")$ur.lat))) 

여기에 몇 가지주의 할 점이 있습니다.

  1. group = order 지시어를 제거하여 "행 번호가 다릅니다"오류가 발생합니다. 당신이 그곳에서 무엇을하고 있는지 확신 할 수 없지만, 제대로 보이지 않습니다.
  2. env이라는 변수에 poa이있는 환경을 저장합니다. poa을 참조하는 모든 기능에서 사용하려는 환경입니다.
  3. geom_polygonggplot으로 전화를 걸어 방금 environment=env 매개 변수를 지정하여 만든 환경을 사용하도록 안내합니다.
  4. environment=environment()을 해당 명령의 마지막 매개 변수로 사용하여 호출 환경에서 외부 호출 ggmap을 실행하도록 지시합니다. 이렇게하지 않으면 우리가 만든 변수 env을 찾을 수 없습니다.

이러한 변경 사항은 제대로 작동하는 것 같습니다.

+0

제프 대답 주셔서 감사, 불행히도 나는 당신이 결과를 얻을하지 않습니다. 오류 'ggplot (data = poa, aes (x = LON, y = LAT), 환경 = env)에서 오류가 발생했습니다 :'poa 'not found'개체. 주요 질문에 sessionInfo의 출력을 추가하여 비슷한 설정을 사용하고 있는지 확인했습니다. – user1414259

0

group = ordergroup = POAOBS으로 변경하면 앱이 작동합니다.

관련 문제