空 SpatialPolygonsDataFrame 对象已传递,将被跳过

问题描述 投票:0回答:1

我很抱歉提前编写了繁琐的代码,但需要一些好的帮助。

我将从打印屏幕开始

所以,我的 Shiny 应用程序中的 6 个过滤器中有 5 个完美工作:

Filtering by country

Filtering by country

Filtering by indicator 1

Filtering by indicator 2

Filtering by indicator 3

当我尝试创建第六个过滤器 - 称为 Indicator 时,问题就出现了。 并以与最后三个过滤器相同的格式呈现相同的数据。基本上,我想在 Indicator 过滤器中组合之前的三个指标。并按三个指标之一进行过滤,以显示所有 7 个项目的圆圈(基于一项指标)。有道理吗?

但是最后的代码不起作用。它显示我的错误

polygonData.SpatialPolygonsDataFrame(data) 中的警告: 空 SpatialPolygonsDataFrame 对象已传递,将被跳过

不幸的是,我没有找到太多相关信息。您能否看一下名为 Indicator 的代码末尾并提供帮助。我怀疑我至少在数据的正确子集化方面存在问题。

可以在这里获取数据

Shapefiles 可以在这里

所以,代码本身

# Projects and Results Dashboard

# Packages (I played with different)
library(shiny)
library(shinythemes)
library(leaflet)
library(rgdal)
library(tidyverse)
library(geojsonio)
library(RColorBrewer)
library(highcharter)
library(plotly)
library(ggplot2)
library(xlsx)

# Set directory
setwd("C:~/App Projects and Results")

# Read csv, which was created specifically for this app
projects <- read.csv("Sample data3.csv", header = TRUE) 
names(projects)

# Read a shapefile
countries <- readOGR(".","ne_50m_admin_0_countries")

# Merge data
projects.df <- merge(countries, projects, by.x = "name", by.y = "Country")
class(projects.df)

# UI code
ui <- shinyUI(fluidPage(theme = shinytheme("united"),
                    titlePanel(HTML(# "<h1><center><font size=14> 
                      "Projects and Results Dashboard"
                    #</font></center></h1>"
                    )), 
                    sidebarLayout(
                      sidebarPanel(
                        selectInput("countryInput", "Country",
                                    choices = c("Choose country", "Senegal",
                                                "Nigeria",
                                                "Cameroon",
                                                "Dem. Rep. Congo",
                                                "Rwanda",
                                                "Tanzania", 
                                                "Madagascar"),
                                    selected = "Choose country"),
                        selectInput("projectInput", "Project",
                                    choices = c("Choose Project",  
                                                "Project 1",
                                                "Project 2",
                                                "Project 3",
                                                "Project 4",
                                                "Project 5",
                                                "Project 6",
                                                "Project 7"),
                                    selected = "Choose Project"),
                        selectInput("totalInput", "Total Funds",
                                    choices = c("Choose Project",  
                                                "Project 1",
                                                "Project 2",
                                                "Project 3",
                                                "Project 4",
                                                "Project 5",
                                                "Project 6",
                                                "Project 7"),
                                    selected = "Choose Project"),
                        selectInput("cashInput", "Client Cash",
                                    choices = c("Choose Project",  
                                                "Project 1",
                                                "Project 2",
                                                "Project 3",
                                                "Project 4",
                                                "Project 5",
                                                "Project 6",
                                                "Project 7"),
                                    selected = "Choose Project"),
                        selectInput("clientInput", "Total Client",
                                    choices = c("Choose Project",  
                                                "Project 1",
                                                "Project 2",
                                                "Project 3",
                                                "Project 4",
                                                "Project 5",
                                                "Project 6",
                                                "Project 7"),
                                    selected = "Choose Project"),
                        selectInput("indicatorInput", "Indicator",
                                    choices = c("Choose indicator",  
                                                "Total Funds ",
                                                "Client Cash",
                                                "Total Client"
                                                ),
                                    selected = "Choose indicator")
                      ),

                      mainPanel(leafletOutput(outputId = 'map', height = 800) 
                      )
                    )
))

# SERVER

server <- shinyServer(function(input, output) {
output$map <- renderLeaflet({
leaflet(projects.df) %>% 
  addProviderTiles(providers$Stamen.TonerLite) %>% 
  setView(11.0670977,0.912484, zoom = 4) 

})
# observers

# selected country
selectedCountry <- reactive({
projects.df[projects.df$name == input$countryInput, ] 
})
observe({
state_popup <- paste0("<strong>Country: </strong>", 
                      selectedCountry()$name, 
                      "<br><strong> Project: </strong>", 
                      selectedCountry()$Project,
                      "<br><strong> Total Funds: </strong>", 
                      selectedCountry()$Total.Funds,
                      "<br><strong>Client Cash: </strong>", 
                      selectedCountry()$Client.Cash,
                      "<br><strong>Total Client: </strong>", 
                      selectedCountry()$Total.Client)


leafletProxy("map", data = selectedCountry()) %>%
  clearShapes() %>%
  addPolygons(fillColor =  "blue",
              popup = state_popup,
              color = "#BDBDC3",
              fillOpacity = 0.5,
              weight = 1 
              )
})

# selected project
selectedProject <- reactive({
tmp4 <- projects.df[!is.na(projects.df$Project),]
tmp4[tmp4$Project == input$projectInput, ] 
})
observe({
state_popup4 <- paste0("<strong>Country: </strong>", 
                       selectedProject()$name, 
                      "<br><strong> Project: </strong>", 
                       selectedProject()$Project,
                      "<br><strong> Total Funds: </strong>", 
                       selectedProject()$Total.Funds,
                      "<br><strong>Client Cash: </strong>", 
                       selectedProject()$Client.Cash,
                      "<br><strong>Total Client: </strong>", 
                       selectedProject()$Total.Client)


 leafletProxy("map", data = selectedProject()) %>%
  clearShapes() %>%
  addPolygons(fillColor = "blue",
              popup = state_popup4,
              color = "#BDBDC3",
              fillOpacity = 0.5,
              weight = 1
              )
})

# Total Funds
selectedTotal <- reactive({
tmp <- projects.df[!is.na(projects.df$Project),]
tmp[tmp$Project == input$totalInput, ] 
})
observe({
state_popup1 <- paste0("<strong>Country: </strong>", 
                       selectedTotal()$name, 
                       "<br><strong> Project: </strong>", 
                       selectedTotal()$Project,
                       "<br><strong> Total Funds </strong>", 
                       selectedTotal()$Total.Funds)

leafletProxy("map", data = selectedTotal()) %>%
  clearShapes() %>%
   addCircles(lng = ~selectedTotal()$long, lat = ~selectedTotal()$lat, 
weight = 1, fillOpacity = 0.5, color = "darkorange", 
             radius = ~Total.Funds*500, popup = state_popup1

   )
})

# Cash Funds
selectedCash <- reactive({
tmp1 <- projects.df[!is.na(projects.df$Project),]
tmp1[tmp1$Project == input$cashInput, ] 
})
observe({
state_popup2 <- paste0("<strong>Country: </strong>", 
                       selectedCash()$name, 
                       "<br><strong>Project: </strong>", 
                       selectedCash()$Project,
                      "<br><strong>Client Cash: </strong>", 
                       selectedCash()$Client.Cash)

leafletProxy("map", data = selectedCash()) %>%
  clearShapes() %>%
  addCircles(lng = ~selectedCash()$long, lat = ~selectedCash()$lat, weight = 
1, fillOpacity = 0.5, color = "darkred", 
             radius = ~Client.Cash*500, popup = state_popup2)
})

# Total Client
selectedClient <- reactive({
tmp2 <- projects.df[!is.na(projects.df$Project),]
tmp2[tmp2$Project == input$clientInput, ] 
})
observe({
state_popup3 <- paste0("<strong>Country: </strong>", 
                       selectedClient()$name, 
                       "<br><strong>Project: </strong>", 
                       selectedClient()$Project,
                        "<br><strong>Total Client: </strong>", 
                       selectedClient()$Total.Client)

leafletProxy("map", data = selectedClient()) %>%
  clearShapes() %>%
  addCircles(lng = ~selectedClient()$long, lat = ~selectedClient()$lat, 
weight = 1, fillOpacity = 0.5, color = "darkgreen", 
             radius = ~Total.Client*500, popup = state_popup3)
})

# Indicator
selectedIndicator <- reactive({
tmp5 <- projects.df[!is.na(projects.df$Project),]
tmp5[tmp5$Total.Funds == input$indicatorInput | tmp5$Client.Cash == 
input$indicatorInput | tmp5$Total.Client == input$indicatorInput, ] 
})
observe({
state_popup5 <- paste0("<strong>Country: </strong>", 
                       selectedIndicator()$name, 
                       "<br><strong>Project: </strong>", 
                       selectedIndicator()$Project,
                       "<br><strong> Total Funds: </strong>", 
                       selectedIndicator()$Total.Funds,
                       "<br><strong>Client Cash: </strong>", 
                       selectedIndicator()$Client.Cash,
                       "<br><strong>Total Client: </strong>", 
                       selectedIndicator()$Total.Client
)

leafletProxy("map", data = selectedIndicator()) %>%
  clearShapes() %>%
  addCircles(lng = ~selectedIndicator()$long, lat = 
~selectedIndicator()$lat, weight = 1, fillOpacity = 0.5, color = 
"darkorange", 
             radius = ~Total.Client*500, popup = state_popup5)***


})

})

shinyApp(ui = ui, server = server)
r shiny geospatial r-leaflet
1个回答
1
投票

在 R 社区的帮助下解决了。

最后一个过滤器将与此代码一起使用。

# Indicator
selectedIndicator <- reactive({switch(input$indicatorInput, 
                               "Total Funds " = projects.df$Total.Funds, 
                               "Client Cash" = projects.df$Client.Cash, 
                               "Total Client" = projects.df$Total.Client)
})

observe({
state_popup5 <- paste0("<strong>Country: </strong>", 
                       projects.df$name, 
                       "<br><strong>Project: </strong>", 
                       projects.df$Project,
                       "<br><strong> Total Funds: </strong>", 
                       projects.df$Total.Funds,
                       "<br><strong>Client Cash: </strong>", 
                       projects.df$Client.Cash,
                       "<br><strong>Total Client: </strong>", 
                       projects.df$Total.Client
 )

 leafletProxy("map", data = projects.df) %>%
  clearShapes() %>%
  addCircles(lng = projects.df$long, lat = 
               projects.df$lat, weight = 1, fillOpacity = 0.5, color = 
               "darkorange", 
             radius = ~selectedIndicator()*500, popup = state_popup5) 
 })

结果:

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.