성가신 코드는 미리 작성해야하지만 유익한 도움이 필요합니다.빈 SpatialPolygonsDataFrame 객체가 전달되어 Shiny에서 건너 뜁니다.
내가
그래서 printscreens에서 시작됩니다 5 완벽하게 내 반짝 응용 프로그램 작업에서 6 개 필터 아웃 :
나는 여섯 번째 필터를 만들려고 할 때문제가 발생합니다 - 표시했다. 동일한 데이터를 3 개의 마지막 필터와 동일한 형식으로 표시하십시오. 기본적으로, 나는 표시기을 3 개의 이전 표시기에 필터하고 싶습니다. 세 가지 지표 중 하나를 기준으로 필터링하여 7 가지 프로젝트 모두에 대해 원을 표시 할 수 있습니다 (하나의 지표를 기반으로 함). 그것은 의미가 있습니까?
하지만 마지막 코드는 작동하지 않습니다. 통과 빈 SpatialPolygonsDataFrame 객체와 불행히도, 난 그것에 대해 너무 많이 찾지 못했습니다
를 건너 뜁니다 : 그것은 나를 polygonData.SpatialPolygonsDataFrame에서
경고 (데이터) 오류 보여줍니다. 표시기이라고 불리는 코드의 끝 부분을 살펴보고 도움을 주시겠습니까? 나는 적어도 데이터의 적절한 부분 집합에 문제가 있다고 생각한다.
데이터는 here
모양 파일 그래서
here를 취할 수 있습니다 R 커뮤니티의 도움으로 해결 코드 자체
# 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)