Folks, I almost completed my first Shiny app ever, but stumbled. Checked all here https://stackoverflow.com/search?q=shiny+map+r
My code:
# Packages
library(shiny)
library(dplyr)
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 data2.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(
titlePanel("Map"),
sidebarLayout(
sidebarPanel(
selectInput("countryInput", "Country",
choices = c("a",
"b",
"c",
"d",
"e",
"f",
"g")),
selectInput("MFIInput", "MFI",
choices = c("a1",
"b1",
"c1",
"d1",
"e1",
"f1" )),
radioButtons("projectInput1", "Project type 1",
choices = c("Agent banking", "mBanking", "Debit cards"),
selected = "Agent banking"),
radioButtons("projectInput2", "Project type 2",
choices = c("Agent banking", "mBanking", "Debit cards"),
selected = "mBanking"),
radioButtons("projectStatus", "Project status",
choices = c("Launched", "Pilot", "Planning"),
selected = "Launched")
),
mainPanel(leafletOutput(outputId = 'map')
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
pal <- colorFactor(
palette = "Orange",
domain = projects.df$Number,
reverse = FALSE)
# Create a pop-up
state_popup <- paste0("<strong>Country: </strong>",
projects.df$name,
"<br><strong>MFI: </strong>",
projects.df$MFI,
"<br><strong>Number of projects: </strong>",
projects.df$Number,
"<br><strong>Project type 1: </strong>",
projects.df$Project.type.1.,
"<br><strong>Project type 2: </strong>",
projects.df$Project.type.2.,
"<br><strong>Project status: </strong>",
projects.df$Status)
# Create a map
projects.map <- projects.df %>%
leaflet() %>%
addTiles() %>%
setView(4.3419591, 19.8764526, zoom = 3) %>%
addPolygons(fillColor = ~pal(projects.df$Number),
popup = state_popup,
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1)
})
}
shinyApp(ui = ui, server = server)
But slides and map are not linked yet:
Then I tried to add this info to server:
server <- function(input, output) {
output$map <- renderLeaflet({
selectInput("countryInput", "Country",
sort(unique(projects.df$Country)),
selected = "a",
"MFIInput", "MFI",
sort(unique(projects.df$MFI)),
selected = "d1",
"projectInput1", "Project type 1",
sort(unique(projects.df$Project.type.1.)),
selected = "Agent network",
"projectInput2", "Project type 2",
sort(unique(projects.df$Project.type.2)),
selected = "mBanking",
"projectStatus", "Project status",
sort(unique(projects.df$Status)),
selected = "Launched")
pal <- colorFactor(
palette = "Orange",
domain = projects.df$Number,
reverse = FALSE)
# Create a pop-up
state_popup <- paste0("<strong>Country: </strong>",
projects.df$name,
"<br><strong>MFI: </strong>",
projects.df$MFI,
"<br><strong>Number of projects: </strong>",
projects.df$Number,
"<br><strong>Project type 1: </strong>",
projects.df$Project.type.1.,
"<br><strong>Project type 2: </strong>",
projects.df$Project.type.2.,
"<br><strong>Project status: </strong>",
projects.df$Status)
# Create a map
projects.map <- projects.df %>%
leaflet() %>%
addTiles() %>%
setView(4.3419591, 19.8764526, zoom = 3) %>%
addPolygons(fillColor = ~pal(projects.df$Number),
popup = state_popup,
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1)
})
}
shinyApp(ui = ui, server = server)
As a result, I have an error: formal argument "selected" matched by multiple actual arguments Map is not displayed anymore.
Where is my mistake?
In the selectInput()
, you have several selected =
arguments: selected = "Senegal"
, selected = "FINCA"
etc. which triggers the error message formal argument "selected" matched by multiple actual arguments
.
You should have only one selected =
argument.
(Imagine providing a plot function with several colour for a drawing line colour = red
, colour = blue
...)