1

~~EDIT~~~

The answer below worked, please see my code to be complete and question answered for anyone who needs the help in the future.

~~~~~~~~

I am trying to create a dashboard in R (first one!) that will create a map that shows thousands of routes taken between the package initial location and the package end location (travelling all over the world). I would then like to have filters in order to show different routes based on criteria (and if possible, give a box to tell how many routes are there based on the selection).

I was able to make the dashboard and the map with all the lines and it looks great. The issue now is I cannot seem to figure out how to create the filters to be interactive. My problem is I am currently creating the lines and plotting them in the For Loop so they are not being saved anywhere.

I have three filters: Department (which I call SBU), Manufacturing plant (which I call Plant and is a sub set of SBU), and Customer.

I.e You can have SBU A with all Plants associated with SBU A and look at Customer Y. You will then see those specific routes associated with this.

I.e. You can have SBU B with Plant K and look at all Customers

Unfortunately, I cannot give out the raw data.

Any help would be greatly appreciated as I am very new to 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)
Kevin
  • 1,974
  • 1
  • 19
  • 51

1 Answers1

0

You need to make the dataset "reactive" to your inputs and then keep using and referring to that reactive dataset so it updates each time your inputs change.

Here is an example of making a new reactive variable called reactive_dat based on a filtered version of your Data variable.

reactive_dat <- reactive({
  Data[Data$SBU == input$SBU, ]
})
Steven M. Mortimer
  • 1,618
  • 14
  • 36
  • I am getting an error when I do that: "Error: argument of length 0" . I put that code right above "#Map formation". If I remove reactive({}) and change "input$SBU" to "Ex Y" it works. Also, I think I would want the reactivity of the input variables after R has done all of the calculations of gcintermediate. I have 43,000 observations so it takes a solid minute to do. With changing variables, I don't want to be waiting a minute every viewpoint I want to see. – Kevin Jun 18 '17 at 16:51
  • I don't even see where you plot your `Data` to your shiny app. The only output you have is `uiOutput("Plant")` where `output$Plant <- renderUI({})`...I think you have more issues than just data reactivity. – CPak Jun 18 '17 at 17:20
  • Hi Chi Pak, sorry the code wasn't updated to produce the map in the dashboard, just in the plot view. I have updated it now. It plots the map using "map()" and then runs through a For loop for each Long/Lat pair and plots the route using "lines()". – Kevin Jun 18 '17 at 20:18
  • I did not realize that when using reactive({}), it creates a function with the new variable. Code will be updated to help anyone. – Kevin Jun 19 '17 at 14:33