2016-06-01 5 views
2

Rstudio 사용 리플 렛 출력을 생성하는 반짝이는 앱을 만들려고합니다. shiny는 이전에 사용하지 않은 패키지이므로 스크립트에서 다른 오류가있을 수 있습니다.반짝이는 inputselect에 따라 데이터 프레임과 플롯을 부분 집합하는 방법

입력 선택에 대한 응답으로 한 동물의 트랙을 부분 집합하고 플롯하려는 다른 개인의 트랙이있는 하나의 데이터 프레임을 사용하고 있습니다.

Sample: 

WhaleID  lat  long 
gm08_150c 68,4276 16,5192 
gm08_150c 68,4337 16,5263 
gm08_150c 68,4327 16,5198 
gm08_154d 68,4295 16,5243 
gm08_154d 68,4313 16,5314 
gm08_154d 68,4281 16,5191 

입력 선택의 선택은 열 WhaleID에서 .csv 파일에 사용 된 정확한 이름입니다, 그래서 일부에게 주요 dataframe에서 해당 WhaleID 모든 행을하고 싶습니다.

이 서브 세트 이후에 이전에 서브 세트 된 데이터 프레임의 "long"및 "lat"컬럼 만 서브 세트하고 싶습니다. 그런 다음이 데이터 프레임을 전단지로 읽습니다.

마지막 단계는 이러한 "긴"위치와 "위도"위치를지도에 플로팅하는 것입니다.

Warning: Error in $: object of type 'closure' is not subsettable 
Stack trace (innermost first): 
    82: inherits 
    81: resolveFormula 
    80: derivePolygons 
    79: addPolylines 
    78: function_list[[i]] 
    77: freduce 
    76: _fseq 
    75: eval 
    74: eval 
    73: withVisible 
    72: %>% 
    71: func [#15] 
    70: output$map 
    4: <Anonymous> 
    3: do.call 
    2: print.shiny.appobj 
    1: <Promise> 

스크립트 내가 사용하고 : 나는 그것이 eventreactive 부분과 내부의 부분 집합의 부분과 관련이있다 생각

require(shiny) 
    require(leaflet) 

    ##Main dataframe I want to subset depending on inputselection  
    dataframe <-read.csv2("PW_with_speedbearingturn.csv") 


    ui <- bootstrapPage( 
     tags$style(type = "text/css", "html, body {width:100%;height:100%}"), 
     absolutePanel(top = 10, right = 10, 
     selectInput(inputId = "whaleID", 
        label = "Select a whale", 
        choices = c("gm08_150c","gm08_154d"))), 
     leafletOutput(outputId = "map", width = "100%", height = "700") 
    ) 

    server <- function(input, output, session){ 

     #?observeEvent 
     #observeEvent(input$whaleID,) 
     Start <- makeIcon(
     iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Dark_Green_Arrow_Down.svg/480px-Dark_Green_Arrow_Down.svg.png", 
     iconWidth = 22, iconHeight = 20, 
     iconAnchorX = 11, iconAnchorY = 20) 

     eventReactive(input$whaleID,{ 
        df<-subset(dataframe, WhaleID == "input$whaleID") ## "input$whaleID" should become the WhaleID that is selected from the inputselect. 

        df<-subset(df, select = c("long", "lat")) 
     })     
     output$map <- renderLeaflet({ 
     leaflet() %>% 
     addTiles() %>% 
     addPolylines(df, lng = df$long, lat = df$lat, col = "grey", opacity = 1)%>% 
     addMarkers(df, lng = first(df$long), lat = first(df$lat), icon = Start, popup = "Start") 
     }) 
    } 

    shinyApp(ui = ui, server = server) 

불행하게도 나는 오류 메시지가 표시됩니다. 입력 선택에서 선택한 항목에 대한 응답으로 매번 업데이트해야합니다. 불행히도이 질문에 대한 기존의 해결책을 찾을 수 없었습니다.

이 문제를 해결하는 방법에 대한 조언이 있으십니까? 사전에

감사합니다,

Onno

+0

각 whaleID에 대해 하나의 레이어가 있어야합니까? 그렇다면 얼마나 많은 개별 ID가 있습니까? – TimSalabim

답변

2

는 당신이 완전히 renderLeaflet 이후 input 변경에 반응 할 것이다 eventReactive을 피할 수 있다고 생각합니다. 귀하의 데이터가 없기 때문에 다음은 적응 된 예입니다. 이치에 맞지 않으면 알려주세요.

require(shiny) 
require(leaflet) 

##Main dataframe I want to subset depending on inputselection  
dataframe <- data.frame(
    abb = state.abb, 
    x = state.center$x, 
    y = state.center$y, 
    stringsAsFactors = FALSE 
) 


ui <- bootstrapPage( 
    tags$style(type = "text/css", "html, body {width:100%;height:100%}"), 
    absolutePanel(top = 10, right = 10, 
       selectInput(inputId = "whaleID", 
          label = "Select a state", 
          choices = dataframe[["abb"]])), 
    leafletOutput(outputId = "map", width = "100%", height = "700") 
) 

server <- function(input, output, session){ 

    Start <- makeIcon(
    iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fe/Dark_Green_Arrow_Down.svg/480px-Dark_Green_Arrow_Down.svg.png", 
    iconWidth = 22, iconHeight = 20, 
    iconAnchorX = 11, iconAnchorY = 20) 


    output$map <- renderLeaflet({ 
    df <- subset(dataframe, abb == input$whaleID) 
    leaflet() %>% 
     addTiles() %>% 
     #addPolylines(df, lng = df$long, lat = df$lat, col = "grey", opacity = 1)%>% 
     addMarkers(data = df, lng = ~x, lat = ~y, icon = Start, popup = "Start") 
    }) 
} 

shinyApp(ui = ui, server = server) 
+0

대단히 감사합니다. 문제를 해결하는 데 도움이되었으며 스크립트가 올바르게 작동하고 있습니다. –

관련 문제