由selectInput驱动的传单app中的圆的半径

时间:2016-08-20 17:41:51

标签: r shiny leaflet

我试图通过selectInput变量驱动此Shiny宣传单应用中的圆圈标记半径。

下拉列表有三个值,“第1周”,“第2周”和“第3周”,它们都是这个问题底部d中的数字向量(以百万美元为单位的大额收益值)

selectInput("weekView", "Week's Revenue:",
          c("1" = "Week.1",
            "2" = "Week.2",
            "3" = "Week.3")),

我目前正在加载Week.1并获得thr结果。我将输入更改周并与地图上的相应$做出反应。 当我尝试使用输入$ weekView而不是静态~(Week.1)时,我得到错误,因为它是“Week.1”。在尝试计算半径之前,我尝试使用quote = False来删除它们,但是我找不到合适的位置...

leaflet(data = P) %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addCircleMarkers(~Long, ~Lat, popup = ~Week.1,
                   radius = ~(Week.1)/40000,
                   stroke = FALSE, 
                   fillOpacity = 0.5)

任何人都可以建议如何最好地解决这个问题吗?我想说的是 radius = input$weekView/40000

所有剧本:

library(shiny)
library(leaflet)   

ui <- fluidPage(

  selectInput("weekView", "Week's Revenue:",
              c("1" = "Week.1",
                "2" = "Week.2",
                "3" = "Week.3")), 

  leafletOutput("mymap"),
  p()
)

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


  output$mymap <- renderLeaflet({

    x <- input$weekView

    P<- read.csv("Lebara_weeks_rev4.csv")

    as.numeric(P$Long)
    as.numeric(P$Lat)
    as.character(P$Week.1)

    P$Week.1 <- as.numeric(gsub(",","",P$Week.1))
    P$Week.2 <- as.numeric(gsub(",","",P$Week.2))
    P$Week.3 <- as.numeric(gsub(",","",P$Week.3))

    long <- P$Long
    Lat <- P$Lat

    leaflet(data = P) %>%
      addTiles() %>%  # Add default OpenStreetMap map tiles
      addCircleMarkers(~Long, ~Lat, popup = ~Week.1,
                       radius = ~(Week.1)/40000,
                       stroke = FALSE, 
                       fillOpacity = 0.5)
  })
}

shinyApp(ui, server)

df = P

的负责人
    > head(P)
    group      Lat      Long        Country  Week.1  Week.2  Week.3  Week.4
1 178.100 55.37805 -3.435973 United Kingdom 649,613 665,147 640,732 649,642
2 174.890 51.16569 10.451526        Germany 117,766 120,402 104,167  91,157
3 144.100 46.22764  2.213749         France 135,784 117,759 109,337 101,873
4 174.211 52.13263  5.291266    Netherlands 403,950 397,438 377,855 389,345
5 174.990 40.46367 -3.749220          Spain  94,472  95,742  88,313  86,400
6 178.600 56.26392  9.501785        Denmark  70,094  72,487  67,597  66,769

谢谢! 皮特

我使用了tidyr和subsetWeek1&lt; -P [1:7,]然后为subsetWeek1$value/40000制作变量以使它们对于这些子集的半径值足够小。我试图使selectInput 1 = subsetWeek1 $ value,2 = subsetWeek2 $ value和3 = subsetWeek3 $ value ...

library(tidyr)
P <- tidyr::gather(P, week, value, Week.1:Week.3)

subsetWeek1 <- P[1:7,]
subsetWeek2 <- P[8:14,]
subsetWeek3 <- P[15:21,]

Week1val <- subsetWeek1$value
Week2val <- subsetWeek2$value
Week3val <- subsetWeek3$value

这似乎没有获取selectInput值并更改地图。地图上没有标记,也没有提供错误。难道圆圈的比例会让它们看不见吗?在非应用程序版本中,它们没问题。

1 个答案:

答案 0 :(得分:1)

你关闭了,你只需要更新变量名并清理一下。一般来说,如果您重复使用稍有不同参数的同一行,那么更好的方法。一个选项,使用tidyr gatherextract_numeric

library(shiny)
library(tidyr)
library(leaflet)

P <- read.csv("Lebara_weeks_rev4.csv")

# do munging that won't change based on input here
P2 <- gather(P, week, value, Week.1:Week.4)    # gather to long form
P2$value <- extract_numeric(P2$value)    # convert to numeric

ui <- fluidPage(
    selectInput("weekView", "Week's Revenue:",
                c("1" = "Week.1",
                  "2" = "Week.2",
                  "3" = "Week.3",
                  "4" = "Week.4")),

    leafletOutput("mymap")
)

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

    output$mymap <- renderLeaflet({
        # do munging dependent on input here
        P3 <- P2[P2$week == input$weekView, ]    # subset based on input

        leaflet(data = P3) %>%
            addTiles() %>%  # Add default OpenStreetMap map tiles
            addCircleMarkers(lng = ~Long, lat = ~Lat, 
                             popup = ~format(value, big.mark = ','),    # reinsert commas
                             radius = ~value/40000,
                             stroke = FALSE, 
                             fillOpacity = 0.5)
    })
}

shinyApp(ui, server)