2017-09-06 3 views
2

皆さん、私は初めてのShinyアプリケーションをほぼ完成させましたが、つまずきました。リーフレットとShiny - 正式な議論が複数の実際の議論と一致する "選択された"

# 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]

しかし、すべてここにhttps://stackoverflow.com/search?q=shiny+map+r

マイコードチェック スライドと地図はまだリンクされていません:

次に、この情報をサーバーに追加しようとしました:

その結果、私はエラーを持っている
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) 

仮引数が 地図がもう表示されていない複数の実引数にマッチした「選択」:(

どこに間違いはありますか? ありがとうございました! selectInput()

敬具

オレクシ

答えて

1

、あなたはいくつかのselected =の引数があります:selected = "Senegal"selected = "FINCA"などのエラーメッセージformal argument "selected" matched by multiple actual argumentsをトリガします。

引数はselected =です。 (描画線に複数色のプロット関数を用意すると想像してください)colour = redcolour = blue ...

+0

お返事ありがとうございます。だから少し修正しましたが、地図上に複数の入力を表示することはできません。今は国別にしか切り替えることができず、すべての変数で切り替える必要があります。たとえば、domain = input $ countryInputと書いたので、addPolygons(fillColor =〜pal(projects.df $ name))を使ってマップ上で最後に切り替えられます。 – Oleksiy

関連する問題