So, the code was rewritten differently
# Packages
library(shiny)
library(shinythemes)
library(leaflet)
library(rgdal)
# Set working directory
setwd("C: /My Shiny apps")
# Read csv, which was created specifically for this app
projects <- read.csv("sample data10.csv", header = TRUE)
# 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)
# Shiny code
# UI
ui <- fluidPage(theme = shinytheme("united"),
titlePanel("Map sample"),
sidebarLayout(
sidebarPanel(
selectInput("countryInput", "Country",
choices = c("Choose country", "Country 1","Country 2","Country 3","Country
4","Country 5","Country 6", "Country 7"),
selected = "Choose country"),
selectInput("clientInput", " Client",
choices = c("Choose Client", "Client 1","Client 2","Client 3","Client 4","Client
5","Client 6"),
selected = "Choose Client"),
selectInput("channeInput", "Channel",
choices = c("Choose Channel", "Agent network", "M-banking", "Debit cards"),
selected = "Choose Channel"),
selectInput("statusInput", "Status",
choices = c("Choose status", "Launched", "Pilot", "Planning"),
selected = "Choose status")
),
mainPanel(
leafletOutput('map'),
plotOutput('country_output'),
plotOutput('client_output'),
plotOutput('channel_output'),
plotOutput('status_output')
)
)
)
server <- function(input, output) {
pal1 <- colorFactor(palette = "Blues", domain = c(0, 100))
pal2 <- colorFactor(palette = "Blues", domain = c(0, 100))
pal3 <- colorFactor(palette = "Blues", domain = c(0, 100))
pal4 <- colorFactor(palette = "Blues", domain = c(0, 100))
output$map <- renderLeaflet({
leaflet('map') %>%
addTiles("Stamen.Watercolor") %>%
setView(11.0670977,0.912484, zoom = 4)
})
output$country_output <- renderPlot({
if("Country 1" %in% input$"countryInput") { # sample conditional statement
leafletProxy('map') %>% # initalize the map
clearGroup("polys") %>% # clear any previous polygons
addPolygons(fillColor = ~pal1(projects.df$name),
popup = paste0("<strong>Country: </strong>",projects.df$name,"<br><strong> Client:
</strong>",projects.df$ Client,"<br><strong> Channel: </strong>",projects.df$Channel,"<br>
<strong>Status: </strong>", projects.df$Status),
color = "#BDBDC3", fillOpacity = 1, weight = 1, group = "polys")
}
})
output$client_output <- renderPlot({
leafletProxy('map') %>% # initalize the map
clearGroup("polys") %>% # clear any previous polygons
addPolygons(fillColor = ~pal2(projects.df$Client),
popup = paste0("<strong>Country: </strong>",projects.df$name,"<br><strong> Client:
</strong>",projects.df$ Client,"<br><strong> Channel: </strong>",projects.df$Channel,"<br>
<strong>Status: </strong>", projects.df$Status),
color = "#BDBDC3", fillOpacity = 1, weight = 1, group = "polys")
})
output$channel_output <- renderPlot({
leafletProxy('map') %>% # initalize the map
clearGroup("polys") %>% # clear any previous polygons
addPolygons(fillColor = ~pal3(projects.df$Channel),
popup = paste0("<strong>Country: </strong>",projects.df$name,"<br><strong> Client:
</strong>",projects.df$ Client,"<br><strong> Channel: </strong>",projects.df$Channel,"<br>
<strong>Status: </strong>", projects.df$Status),
color = "#BDBDC3", fillOpacity = 1, weight = 1, group = "polys")
})
output$status_output <- renderPlot({
leafletProxy('map') %>% # initalize the map
clearGroup("polys") %>% # clear any previous polygons
addPolygons(fillColor = ~pal4(projects.df$Status),
popup = paste0("<strong>Country: </strong>",projects.df$name,"<br><strong> Client:
</strong>",projects.df$ Client,"<br><strong> Channel: </strong>",projects.df$Channel,"<br>
<strong>Status: </strong>", projects.df$Status),
color = "#BDBDC3", fillOpacity = 1, weight = 1, group = "polys")
})
}
shinyApp(ui = ui, server = server)
However, problems with the map remain the same, even filters stop to work now 