2016-12-24 6 views
1

私は飛行データを視覚化するためのRシャイニーツールを構築しています。私は航空機の緯度、経度、速度、方位、高度などを含む観測データテーブルを作成しました。次に、これらの観測値をleafletマップにプロットします。一連のスライダおよびボックスは、所望の観測値のみがプロットされるように(例えば、ある高度の観測値のみ)、これらの観測値をサブセット化することができる。 selectInput()ウィジェットを追加して複数の値を選択できるようになるまで、私は観測をレンダリングするのに問題はありませんでした。以下は、私のコードの最小限で再現可能な例です。Rリーフレットプロットのデータの光反応性サブセット

server.R

library(shiny) 
library(data.table) 
library(leaflet) 

shinyServer(function(input, output){ 

    # sample data set 
    dt <- data.table(altitude = c(1,1,3,3,4,5,6,7,3,2), 
        long = c(-85.2753, -85.4364, -85.5358, -85.6644, -85.8208, 
          -89.9233, -90.0456, -90.2775, -90.5800, -90.8761), 
        lat = c(45.3222, 45.3469, 45.3764, 45.4089, 45.4503, 
          44.0489, 44.1878, 44.3378, 44.4383, 44.5197), 
        origin = c('a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b'), 
        destination = c('c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'd')) 

    # subset the data on various inputs from ui.R 
    subsetData <- reactive({ 
    new_data <- dt[altitude > input$alt_cut[1] & 
        altitude < input$alt_cut[2] & 
        origin %in% input$origin & 
        destination %in% input$dest, ] 
    return(new_data) 
    }) 

    # display the data in real time to identify if the subsetting 
    # is occurring as expected. 
    output$viewData <- renderTable({ 
    subsetData() 
    }) 

    # plot the data points 
    output$mapPlot <- renderLeaflet({ 
    leaflet() %>% 
     fitBounds(-90.8761, 44.0489, -85.2753, 45.4503) 
    }) 

    observe({ 
    leafletProxy('mapPlot') %>% 
     clearGroup('A') %>% # I think this line may not be functioning as I expect... 
     addCircles(data = subsetData(), 
       group = 'A', 
       lng = ~long, 
       lat = ~lat, 
       radius = 2, 
       weight = 2) 
    }) 
}) 

ui.R

shinyUI(fluidPage(
    titlePanel('Aircraft Flights'), 
    sidebarLayout(
    sidebarPanel(
     sliderInput('alt_cut', 
        'Altitude range:', 
        min = 0, 
        max = 10, 
        value = c(0, 10), 
        step = 1), 
     selectInput('origin', 
        'Filter on origin', 
        choices = c('a', 'b'), 
        selected = c('a', 'b'), 
        multiple = TRUE, 
        selectize = FALSE), 
     selectInput('dest', 
        'Filter on destination', 
        choices = c('c', 'd'), 
        selected = c('c', 'd'), 
        multiple = TRUE, 
        selectize = FALSE) 
    ), 
    mainPanel(
     leafletOutput('mapPlot'), # leaflet output for plotting the points 
     tags$hr(), 
     tableOutput('viewData') # table for sanity check 
    ) 
) 
)) 

起点と目的地のいくつかの組み合わせを介してクリックすると、プロットは正しく下記の表に表示されているデータを反映しなくなります地図。たとえば、次の一連の操作を試してみてください。

  1. アプリ
  2. 選択原点実行:(右上系列が表示されている)
  3. 選択先:D(及びdがリンクされていないため、グラフが空である)
  4. 選択先:Cを(右上シリーズは、Cはリンクされているので、
  5. )再表示され、選択先:高度にD(右上系列が誤ったまま)

サブセット使用しますスライダはもう機能しません。テーブル内のデータは変化していますが、プロットは変更されていないため、clearGroup('A')行がサークルを削除していないと考えられます。

なぜ表とプロットが表示されているかに違いがありますか?

Screenshot問題:テーブルにデータはありませんが、ポイントはまだマップ上にプロットされています。

答えて

1

私はこの問題を発見しました。複製のお詫びthis question。どうやら、すべての観測データがデータテーブルからフィルタリングされると、私の質問で説明した予期しない動作が起こる可能性があります。 @ NicEの答えがnrowのチェックを含めて、問題を解決します。

問題の原因は依然として逃げ出していますが、これが現在の回避策です。

2

私はコメントするのに十分な担当者がいない、そうでなければ投稿します。私はそのコードでエラーを再現できませんでした。あなたのテストケースは正常に動作しました。唯一の変更は、私はあなたのコードを1つのファイルに入れます:

library(shiny) 
library(data.table) 
library(leaflet) 

ui <- (fluidPage(
    titlePanel('Aircraft Flights'), 
    sidebarLayout(
    sidebarPanel(
     sliderInput('alt_cut', 
        'Altitude range:', 
        min = 0, 
        max = 10, 
        value = c(0, 10), 
        step = 1), 
     selectInput('origin', 
        'Filter on origin', 
        choices = c('a', 'b'), 
        selected = c('a', 'b'), 
        multiple = TRUE, 
        selectize = FALSE), 
     selectInput('dest', 
        'Filter on destination', 
        choices = c('c', 'd'), 
        selected = c('c', 'd'), 
        multiple = TRUE, 
        selectize = FALSE) 
    ), 
    mainPanel(
     leafletOutput('mapPlot'), # leaflet output for plotting the points 
     tags$hr(), 
     tableOutput('viewData') # table for sanity check 
    ) 
) 
)) 

server <- function(input, output) { 

    # sample data set 
    dt <- data.table(altitude = c(1,1,3,3,4,5,6,7,3,2), 
        long = c(-85.2753, -85.4364, -85.5358, -85.6644, -85.8208, 
          -89.9233, -90.0456, -90.2775, -90.5800, -90.8761), 
        lat = c(45.3222, 45.3469, 45.3764, 45.4089, 45.4503, 
          44.0489, 44.1878, 44.3378, 44.4383, 44.5197), 
        origin = c('a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b'), 
        destination = c('c', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd', 'd')) 

    # subset the data on various inputs from ui.R 
    subsetData <- reactive({ 
    new_data <- dt[altitude > input$alt_cut[1] & 
        altitude < input$alt_cut[2] & 
        origin %in% input$origin & 
        destination %in% input$dest, ] 
    return(new_data) 
    }) 

    # display the data in real time to identify if the subsetting 
    # is occurring as expected. 
    output$viewData <- renderTable({ 
    subsetData() 
    }) 

    # plot the data points 
    output$mapPlot <- renderLeaflet({ 
    leaflet() %>% 
     fitBounds(-90.8761, 44.0489, -85.2753, 45.4503) 
    }) 

    observe({ 
    leafletProxy('mapPlot') %>% 
     clearGroup('A') %>% # I think this line may not be functioning as I expect... 
     addCircles(data = subsetData(), 
       group = 'A', 
       lng = ~long, 
       lat = ~lat, 
       radius = 2, 
       weight = 2) 
    }) 
} 

# Run the application 
shinyApp(ui = ui, server = server) 
+0

さらに、 'clearGroup()'について心配しているなら、 'clearShapes()'を使っていつでもマップ全体をクリアすることができます。 – Jeff

+0

私は状態をシェープファイルからポリゴンとしてプロットするので、グループをクリアしたいだけです。 – 3novak

+0

そして、ポストのおかげで、私はまだ同じファイル内のすべてのコードでも予期しない動作を取得します。私はoldschoolだから私はファイルを別々に保つことを好む。 :) [This](http://i.imgur.com/kjC9f1g.png)は私がまだ観察している問題です。テーブルのデータはありませんが、ポイントはまだマップ上にプロットされています。 – 3novak

関連する問題