2017-06-18 14 views
1

~~ EDIT ~~~働い以下R:複数のフィルタで

答えをルートマップを作成し、完全な質問は将来の助けを必要とする人のための答えであることを私のコードを参照してください。私は、パッケージの初期位置とパッケージの端部との間に取られたルートの数千人を示すマップを作成しますR(最初のもの!)でのダッシュボードを作成しようとしています

~~~~~~~~

場所(世界中を旅する)。次に、条件に基づいて異なるルートを表示するためのフィルタを用意したい(可能であれば、選択に基づいてルートがいくつあるかを示すボックスを用意する)。

ダッシュボードと地図をすべての行で作成できました。問題は今、インタラクティブにフィルタを作成する方法を理解できないようです。私の問題は、私は現在、行を作成し、Forループにプロットして、どこにも保存されていないことです。

私は部門(私はSBUと呼ばれます)、製造工場(私はプラントと呼ばれ、SBUのサブセットです)と顧客の3つのフィルタを持っています。

I.e SBU Aには、SBU Aに関連付けられているすべてのプラントを設定して、顧客Yを見ることができます。これに関連する特定のルートが表示されます。

I.e. SBU BをプラントKで使用し、すべての顧客を見ることができます

残念ながら、私は生データを提供することはできません。

私は非常にRに慣れているので、どんな助力があれば幸いです。

library(shiny) 
    library(shinydashboard) 
    library(maps) 
    library(geosphere) 
    library(maps) 
    library(mapproj) 
    library(geosphere) 
    library(ggrepel) 
    library(scales) 

    ###########################/ui.R/################################## 
    #Setting drive where files are located 
    setwd("C:/R Files") 

    #Pulling in outside Data Files 
    Network <- read.csv("Network Codes.csv") 
    Data <- read.csv("Raw Data2.csv") 

    #Header 
    header <- dashboardHeader(
     title = "Intake Routes") 

    #SideBar 
    sidebar <- dashboardSidebar(

     #SBU Selection List 
     selectInput(inputId = "SBU", "SBU:", selected="ALL", 
        choices = unique(as.character(Network$SBU))), 

     #Plant Selection List 
     uiOutput("Plant"), 

     #Customer Selection List 
     selectInput(inputId = "Customer", "Customer:", multiple = TRUE, selected="ALL", 
      choices = unique(as.character(Data$Customer.Name.Standard)))) 

    #Body 
    body <- dashboardBody(
     plotOutput(outputId = "map") 
    ) 

    #Builds Dashboard Page 
    ui <- dashboardPage(header, sidebar, body) 

    ###########################/server.R/############################### 
    server <- function(input, output) { 

    ##INPUT## 
     #Dependant Plant SideBar List Dependant on SBU 
     output$Plant <- renderUI({ 
     selectInput(inputId = "Plant", "Plant:", multiple = TRUE, 
      choices = as.character(Network[Network$SBU == input$SBU, "Plant.Name"])) 
     }) 

     #Reactive data set based on inputs 
     Reactive_Data1 <- reactive({ 
     if (input$SBU == "ALL") {Data} 
     else {Data[Data$SBU == input$SBU,]} 
     }) 

     Reactive_Data2 <- reactive({ 
     if (input$Plant == "ALL") {Reactive_Data1()} 
     else {Reactive_Data1()[Reactive_Data1()$Plant == (input$Plant),]} 
     }) 

    Reactive_Data3 <- reactive({ 
     if (input$Customer == "ALL") {Reactive_Data2()} 
     else {Reactive_Data2()[Reactive_Data2()$Customer.Name.Standard == input$Customer,]} 
     }) 

    output$map <- renderPlot({ 

     #Map coordinates 
     xlim <- c(-170,170) 
     ylim <- c(-55,75) 

     map("world", col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05, xlim=xlim, ylim=ylim) 

     npoints <- 20 
     nroutes <- nrow(Data) 

     for(i in 1:nroutes){ 

     inter <- gcIntermediate(c(Data$Ship.From.Longitude[i], 
          Data$Ship.From.Latitude[i]), 
         c(Data$Ship.To.Longitude[i], 
          Data$Ship.To.Latitude[i]), 
         n=npoints, addStartEnd = T, breakAtDateLine = T) 

     if (is.list(inter)) { 
      inter1 <- inter[[1]] 
      inter2 <- inter[[2]] 
      lines(inter1, col = "green", lwd=0.50) 
      lines(inter2, col = "blue", lwd=0.50)} 
     else { 
      lines(inter, col = "grey", lwd=0.50)} 
     } 
     }) 
    } 

    #Combines Dashboard and Data together 
    shinyApp(ui, server) 

答えて

0

あなたの入力にデータセットが「反応」にすると、それはあなたの入力が変化するたびに更新されますので、その反応性データセットを使用して参照する維持する必要があります。

Data変数のフィルターを適用したバージョンに基づいて、reactive_datという新しいリアクティブ変数を作成する例を示します。

reactive_dat <- reactive({ 
    Data[Data$SBU == input$SBU, ] 
}) 
+0

「エラー:長さ0の引数」というエラーが表示されます。私はそのコードを "#Map formation"の真上に置きます。リアクティブ({})を取り除いて "input $ SBU"を "Ex Y"に変更すると動作します。また、Rがgcintermediateのすべての計算を終えた後で、入力変数の反応性が欲しいと思います。私は43,000回の観察をしているので、しっかりしています。変数を変更すると、私が見たいすべての視点を1分待たずにすむようになります。 – Kevin

+0

私はあなたの光り輝くアプリにあなたの 'データ 'をプロットする場所を見ていません。あなたが持っている唯一の出力は 'uiOutput(" Plant ")'出力$ Plant < - renderUI({}) 'です...私はデータの反応性だけでなく、もっと多くの問題があると思います。 – CPak

+0

こんにちはチーパック、残念ですが、プロットビューのダッシュボードに地図を表示するようにコードが更新されていませんでした。私は今それを更新しました。 "map()"を使用してマップをプロットし、各Long/LatペアのForループを実行し、 "lines()"を使用してルートをプロットします。 – Kevin