【问题标题】:R: creating a route map with multiple filtersR:创建具有多个过滤器的路线图
【发布时间】:2017-06-18 02:51:09
【问题描述】:

~~编辑~~~

下面的答案有效,请查看我的完整代码并为将来需要帮助的任何人回答问题。

~~~~~~~~

我正在尝试在 R 中创建一个仪表板(第一个!),它将创建一个地图,显示在包裹初始位置和包裹结束位置(环游世界)之间采取的数千条路线。然后,我希望有过滤器,以便根据标准显示不同的路线(如果可能,请提供一个框来说明根据选择有多少路线)。

我能够制作带有所有线条的仪表板和地图,看起来很棒。现在的问题是我似乎无法弄清楚如何创建交互式过滤器。我的问题是我目前正在创建线条并将它们绘制在 For 循环中,因此它们不会被保存在任何地方。

我有三个过滤器:部门(我称之为 SBU)、制造工厂(我称之为工厂,是 SBU 的子集)和客户。

即您可以让 SBU A 与所有与 SBU A 关联的工厂查看客户 Y。然后您将看到与此关联的那些特定路线。

即您可以拥有带有工厂 K 的 SBU B 并查看所有客户

很遗憾,我无法提供原始数据。

非常感谢任何帮助,因为我对 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)

【问题讨论】:

    标签: r shiny shinydashboard jvectormap rmaps


    【解决方案1】:

    您需要使数据集对您的输入具有“反应性”,然后继续使用和引用该反应性数据集,以便每次输入更改时它都会更新。

    这是一个基于 Data 变量的过滤版本创建一个名为 reactive_dat 的新反应变量的示例。

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

    【讨论】:

    • 当我这样做时出现错误: "Error: argument of length 0" 。我将该代码放在“#Map 形成”的正上方。如果我删除 reactive({}) 并将“input$SBU”更改为“Ex Y”,它就可以工作。另外,我想我希望在 R 完成所有 gcintermediate 计算之后输入变量的反应性。我有 43,000 次观察,所以需要一分钟才能完成。随着变量的变化,我不想等待我想看到的每一个观点。
    • 我什至看不到您将Data 绘制到闪亮的应用程序的位置。您唯一的输出是uiOutput("Plant"),其中output$Plant &lt;- renderUI({})...我认为您的问题不仅仅是数据反应性。
    • Hi Chi Pak,抱歉代码没有更新以在仪表板中生成地图,只是在绘图视图中。我现在已经更新了。它使用“map()”绘制地图,然后为每个 Long/Lat 对运行 For 循环,并使用“lines()”绘制路线。
    • 我没有意识到在使用 reactive({}) 时,它会使用新变量创建一个函数。将更新代码以帮助任何人。
    猜你喜欢
    • 1970-01-01
    • 2021-09-09
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多