R 传单和闪亮-正式论点“;精选;由多个实际参数匹配
伙计们,我几乎完成了我有史以来第一个闪亮的应用程序,但我绊倒了。 这里都查过了 我的代码:R 传单和闪亮-正式论点“;精选;由多个实际参数匹配,r,dictionary,shiny,leaflet,R,Dictionary,Shiny,Leaflet,伙计们,我几乎完成了我有史以来第一个闪亮的应用程序,但我绊倒了。 这里都查过了 我的代码: # 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
# 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)
#包
图书馆(闪亮)
图书馆(dplyr)
图书馆(单张)
图书馆(rgdal)
#设置工作目录
setwd(“C:/我的闪亮应用程序”)
#阅读专门为此应用程序创建的csv
项目%
addTiles()%>%
setView(4.3419591,19.8764526,缩放=3)%>%
addPolygons(fillColor=~pal(projects.df$Number),
popup=状态\弹出,
填充不透明度=0.8,
color=“#BDBDC3”,
重量=1)
})
}
shinyApp(用户界面=用户界面,服务器=服务器)
此代码为我提供了以下图像:
[![在此处输入图像描述][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)
server%
addTiles()%>%
setView(4.3419591,19.8764526,缩放=3)%>%
addPolygons(fillColor=~pal(projects.df$Number),
popup=状态\弹出,
填充不透明度=0.8,
color=“#BDBDC3”,
重量=1)
})
}
shinyApp(用户界面=用户界面,服务器=服务器)
因此,我有一个错误:
由多个实际参数匹配的形式参数“选定”
不再显示地图:(
我的错在哪里?
非常感谢你
诚恳
Oleksiy在selectInput()
中,您有多个selected=
参数:selected=“塞内加尔”
,selected=“FINCA”
等。这会触发错误消息由多个实际参数匹配的“selected”正式参数
您应该只有一个selected=
参数。
(想象一下为一条绘图线提供多种颜色的绘图功能,color=red
,color=blue
。)谢谢你的回答。所以,我对它做了一点修改,但我仍然无法在地图上显示多个输入。我现在只能按国家切换,我想按每个变量切换。例如,现在我编写了domain=input$countryInput,最后我可以在地图上用addPolygons(fillColor=~pal(projects.df$name)切换它。