首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为Shiny中的Leaflet选择输入

为Shiny中的Leaflet选择输入
EN

Stack Overflow用户
提问于 2017-08-16 04:02:16
回答 1查看 1.3K关注 0票数 1

我正在尝试使用shiny构建一个带有leaflet的地图,但是我的代码不会根据选择的输入更改leaflet地图。有人知道如何让下面的代码react来做select输入吗?我试图用get函数来做这件事,但是不能成功。任何帮助都将不胜感激!

以下是我的数据:

代码语言:javascript
复制
> datamap
         Country.code EVENTONE EVENTTWO EVENTTHREE
1  Bosnia and Herzegovina       11        1          5
2             South Korea        1        4          4
3             Philippines        1        5          6
4               Indonesia        1        6          8
5                Thailand        1        0          9
6                Mongolia        1        0          3
7      Russian Federation        1        0          4
8                 Ukraine        1        0          8
9                Slovenia        1        0          5
10               Mongolia        1        0          0
11               Pakistan        1        0          0
12             Bangladesh        1        0          0

下面是我的代码:

代码语言:javascript
复制
library(shiny)
library(rworldmap) 
library(ggplot2)



shinyUI(fluidPage( 
  fluidRow(h1("Events in the World", align = "center")),
  fluidRow(
    column(2,

       selectInput("var", "Choose the Type Event:",
                   choices=c("Event One" = "EVENTONE", 
                             "Event Two" = "EVETNTWO", 
                             "Event Three" = "EVENTTHREE"))

),

column(10,

       tabsetPanel(
         tabPanel("Map View", leafletOutput("TheMap", width = "100%")
         )

       ) #end tabset panel
    )
  ) 
))

shinyServer(function(input, output) {

  datamap <- read.csv(".../Documents/R Directory/App-4/mapexcel4CSV.csv",             
                  stringsAsFactors=FALSE, header=TRUE)
  sPDF <- joinCountryData2Map(datamap, joinCode='NAME',       
                          nameJoinColumn='Country.code')
  sPDF <- sPDF[sPDF$ADMIN!='Antarctica',]

  output$TheMap <- renderLeaflet({
  mapselect <- get(input$var)
  pal <- colorBin("YlOrRd", domain = mapselect) 
  labels <- sprintf(
    "<strong>%s</strong><br/>Number of events: %g</sup>",
    sPDF$NAME, sPDF$mapselect
  ) %>% lapply(htmltools::HTML)

  TheMap<- leaflet(data = sPDF) %>% addTiles() %>% addPolygons(stroke = FALSE) %>% addPolygons(
  fillColor = ~pal(mapselect),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7,
  highlight = highlightOptions(
    weight = 5,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
  label = labels,
  labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto"))
  }
  )  
})
shinyApp(ui = ui, server = server)

任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-29 19:43:56

我猜这就是你的目标:

代码语言:javascript
复制
library(shiny)
  library(rworldmap) 
  library(ggplot2)
  library(leaflet)



 ui <-  shinyUI(fluidPage( 
    fluidRow(h1("Events in the World", align = "center")),
    fluidRow(
      column(2,

             selectInput("var", "Choose the Type Event:",
                         choices=c("Event One" = "EVENTONE", 
                                   "Event Two" = "EVENTTWO", 
                                   "Event Three" = "EVENTTHREE"))

      ),

      column(10,

             tabsetPanel(
               tabPanel("Map View", leafletOutput("TheMap", width = "100%")
               )

             ) #end tabset panel
      )
    ) 
  ))

  server <- shinyServer(function(input, output) {

    datamap <- read.csv("E:/test.csv",             
                        stringsAsFactors=FALSE, header=TRUE)
    sPDF <- joinCountryData2Map(datamap, joinCode='NAME',       
                                nameJoinColumn='Country.code')
    sPDF <- sPDF[sPDF$ADMIN!='Antarctica',]
    legVal <-  c(min(datamap[,-1]), max(datamap[,-1]) )
    output$TheMap <- renderLeaflet({
      mapselect <- input$var
      pal <- colorBin("YlOrRd", domain = as.numeric(sPDF[[mapselect]]))
      labels <- sprintf(
        "<strong>%s</strong><br/>Number of events: %g</sup>",
        sPDF$NAME, sPDF[[mapselect]]
      ) %>% lapply(htmltools::HTML)

      TheMap<- leaflet(data = sPDF) %>% addTiles() %>% addPolygons(stroke = FALSE) %>% addPolygons(
        fillColor = ~pal(as.numeric(sPDF[[mapselect]])),
        weight = 2,
        opacity = 1,
        color = "white",
        dashArray = "3",
        fillOpacity = 0.7,
        highlight = highlightOptions(
          weight = 5,
          color = "#666",
          dashArray = "",
          fillOpacity = 0.7,
          bringToFront = TRUE),
        label = labels,
        labelOptions = labelOptions(
          style = list("font-weight" = "normal", padding = "3px 8px"),
          textsize = "15px",
          direction = "auto"))%>% addLegend("bottomleft", pal = pal, value = legVal)
    }
    )  
  })
  shinyApp(ui = ui, server = server)

希望它能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45700647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档