闪亮的传单地图中的反应数据框架

时间:2015-12-09 21:23:12

标签: r shiny leaflet

我试图在leaflet中创建一个shiny地图,根据selectInput中的ui调整并可能以不同方式重新计算我的数据。

我的数据存储为data.frame,类似于:

data <- data.frame(County.FIPS=c(19191,19191,19013,31055,17161,19019), Profit=c(250000,5000000,150000,300000,2000000,0), Type=c("Marketing", "Sales","Online","Marketing","Marketing","Sales"))

然后我做的是按County.FIPS汇总数据,然后将其geo_join到明尼苏达州,爱荷华州和内布拉斯加州的县界的形状文件。

library(tigris)
library(leaflet)

data <- data[complete.cases(data),]
county <-aggregate(data$Profit~data$County.FIPS,data=data,sum)
colnames(county) <- c("GEOID","Profit")

IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Profit, 0), big.mark=',', format = 'f')))))
popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Profit, 0), big.mark=',', format = 'f')))))
popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Profit, 0), big.mark=',', format = 'f')))))

这是我不确定我是否正确/理想地做事并且我不知道如何从这里继续前进的部分。 我想要做的是创建一个被动的data.frame,只要我的下拉选项从&#34;营销&#34;到&#34;在线&#34;到&#34;销售&#34;或者&#34;全部&#34;。我之前尝试将我的三个不同的data.frames保存为单独的RData(Marketing.RData,Online.RData,Sales.RData,All.RData)来调用,但我认为必须有更好的方法。这是我到目前为止所拥有的......

ui.r

library(shiny)
library(leaflet)
library(tigris)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map",width="100%",height="100%"),
  absolutePanel(top=10,right=10,
                selectInput("type", label = h5("Type of Projects:"), choices=list("Marketing"=1, "Sales"=2, "Online"=3,"All"=4,selected =1)
                ))
)

server.r

server <- function(input, output, session) {

  filteredData <- reactive({
    if(input$type=="1"){
      load("Marketing.RData")
      IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
      MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
      NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
      IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
      MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
      NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
      popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Profit, 0), big.mark=',', format = 'f')))))
      popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Profit, 0), big.mark=',', format = 'f')))))
      popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Profit, 0), big.mark=',', format = 'f')))))
      pal <- colorNumeric(c("gray","dark green"),county$Profit,na.color="white")
    }
    else if(input$type=="2"){
      load("Sales.RData")
      IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
      MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
      NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
      IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
      MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
      NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
      popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Profit, 0), big.mark=',', format = 'f')))))
      popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Profit, 0), big.mark=',', format = 'f')))))
      popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Profit, 0), big.mark=',', format = 'f')))))
      pal <- colorNumeric(c("gray","dark green"),county$Profit,na.color="white")
     }
    else if(input$type=="3"){
      load("Online.RData")
      IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
      MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
      NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
      IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
      MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
      NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
      popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Profit, 0), big.mark=',', format = 'f')))))
      popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Profit, 0), big.mark=',', format = 'f')))))
      popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Profit, 0), big.mark=',', format = 'f')))))
      pal <- colorNumeric(c("gray","dark green"),county$Profit,na.color="white")
     }
else if(input$type=="4"){
          load("All.RData")
          IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
          MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
          NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
          IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
          MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
          NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
          popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Profit, 0), big.mark=',', format = 'f')))))
          popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Profit, 0), big.mark=',', format = 'f')))))
          popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Profit, 0), big.mark=',', format = 'f')))))
          pal <- colorNumeric(c("gray","dark green"),county$Profit,na.color="white")
         }
      })
      output$map <- renderLeaflet({
        leaflet() %>%
          addProviderTiles("Esri.WorldStreetMap") %>%
          addLegend(pal = pal, 
                    values = IA_merged$Profit, 
                    position = "bottomright", 
                    title = "County Projects",
                    labFormat=labelFormat(prefix="$")) %>%
          addCircles(lng=yup$lon, lat=yup$lat,weight=1,fillOpacity=0.05,color="red",
                     radius = 96560) %>%
          addCircles(lng=yup$lon, lat=yup$lat,weight=1,fillOpacity=0.025,color="blue",
                     radius = 193121)
      })

      observe({
        leafletProxy("map",data=filteredData()) %>%
          clearShapes() %>%
          addPolygons(data = IA_counties, 
                      fillColor = ~pal(IA_merged$Profit), 
                      fillOpacity = .25, 
                      weight = 0.05, 
                      popup = popupIA)%>%
          addPolygons(data=MN_counties,
                      fillColor=~pal(MN_merged$Profit),
                      fillOpacity=0.25,
                      weight=0.05, 
                      popup = popupMN) %>%
          addPolygons(data=NE_counties,
                      fillColor=~pal(NE_merged$Profit),
                      fillOpacity=0.25,
                      weight=0.05, 
                      popup = popupNE) 
      })



}

目前,当我运行此地图时,我的地图会正常运行,但地图会慢慢重新加载并重新运行所有counties()个功能,这需要一段时间,而且也不会改变我所在县的利润值。地图。他们都展示了&#34; All.RData&#34;号。

感谢您提供的所有帮助,请告诉我您是否需要澄清或有任何问题!

0 个答案:

没有答案