~~编辑~~~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”。我将该代码放在“#地图形成”上方。如果我删除反应({})并将“输入$ SBU”更改为“Ex Y”,它将起作用。另外,我想在R完成gcintermediate的所有计算之后,我会想要输入变量的反应性。我有43,000个观测值,所以需要花费很长时间。随着变数的变化,我不希望等待一分钟我想看到的每一个观点。 – Kevin
我甚至都没有看到你的'数据'在你的闪亮应用中的位置。你唯一的输出是'uiOutput(“Plant”)'输出$ Plant < - renderUI({})'......我认为你有更多的问题,而不仅仅是数据反应。 – CPak
Hi Chi Pak,对不起,代码未更新以在仪表板中生成地图,只是在绘图视图中。我现在更新了它。它使用“map()”绘制地图,然后通过每个长/拉对的For循环运行,并使用“lines()”绘制路线。 – Kevin