伙计们,我差点完成了我的第一个Shiny应用,但是跌跌撞撞。 在这里查看https://stackoverflow.com/search?q=shiny+map+r
我的代码:
# 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)
此代码为我提供了这张图片:
[![在此处输入图像说明] [1]] [1]
但幻灯片和地图尚未关联:
然后我尝试将此信息添加到服务器:
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)
结果,我有一个错误: 正式论证&#34;选择&#34;由多个实际参数匹配 地图不再显示:(
我的错误在哪里? 非常感谢你!
此致
奥雷克
答案 0 :(得分:1)
在selectInput()
中,您有多个selected =
个参数:selected = "Senegal"
,selected = "FINCA"
等会触发错误消息formal argument "selected" matched by multiple actual arguments
。
您应该只有一个selected =
参数。
(想象一下,为绘图线colour = red
提供多种颜色的绘图功能,colour = blue
...)