2017-06-18 32 views
1

~~編輯~~~R:創建一個路線圖以多個濾波器

以下工作答案,請參閱我的代碼是完整的和回答過的問題的人誰需要在未來幫助。

~~~~~~~~

我想創建R中的儀表盤(第一個!),這將創建一個地圖,顯示數千路線的包初始位置和封裝端之間取位置(遍佈世界各地)。然後,我想要有過濾器,以便根據標準顯示不同的路線(如果可能,請根據所選內容給出一個框以指示有多少路線)。

我能夠使儀表板和地圖上的所有線條都看起來很棒。現在的問題是我似乎無法弄清楚如何創建交互式過濾器。我的問題是我目前正在創建線條並將它們繪製在For循環中,因此它們沒有被保存在任何地方。

我有三個過濾器:部門(我稱之爲SBU),製造工廠(我稱之爲工廠,是SBU的子集)和客戶。

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」。我將該代碼放在「#地圖形成」上方。如果我刪除反應({})並將「輸入$ SBU」更改爲「Ex Y」,它將起作用。另外,我想在R完成gcintermediate的所有計算之後,我會想要輸入變量的反應性。我有43,000個觀測值,所以需要花費很長時間。隨着變數的變化,我不希望等待一分鐘我想看到的每一個觀點。 – Kevin

+0

我甚至都沒有看到你的'數據'在你的閃亮應用中的位置。你唯一的輸出是'uiOutput(「Plant」)'輸出$ Plant < - renderUI({})'......我認爲你有更多的問題,而不僅僅是數據反應。 – CPak

+0

Hi Chi Pak,對不起,代碼未更新以在儀表板中生成地圖,只是在繪圖視圖中。我現在更新了它。它使用「map()」繪製地圖,然後通過每個長/拉對的For循環運行,並使用「lines()」繪製路線。 – Kevin