簡體   English   中英

R繪圖圖標無法預測的傳單

[英]Leaflet in R plotting icons unpredictably

我的Shiny應用程序采用如下數據框:

在此輸入圖像描述

通過允許用戶選擇人( P1_name )和日期( date )來適當地設置子集。

初始啟動時,它看起來像這樣:

在此輸入圖像描述

已經很明顯,該應用程序無法運行。 在Apple Valley鎮的位置應該有一個字母'N',但沒有任何東西。 我無法弄清楚為什么,因為DF已被正確地子集化:

在此輸入圖像描述

並且應正確繪制圖層:

m <- leaflet(DF) %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  setView(lat=setzoom[1], lng=setzoom[2], zoom=zoom_num) %>%
  addMarkers(lat=subset(DF, P1_outcome=='W')$lat, lng=subset(DF, P1_outcome=='W')$lon, icon = icon_W) %>%
  addMarkers(lat=subset(DF, P1_outcome=='L')$lat, lng=subset(DF, P1_outcome=='L')$lon, icon = icon_L) %>%
  addMarkers(lat=subset(DF, P1_outcome=='D')$lat, lng=subset(DF, P1_outcome=='D')$lon, icon = icon_D) %>%
  addMarkers(lat=subset(DF, P1_outcome=='N')$lat, lng=subset(DF, P1_outcome=='N')$lon, icon = icon_N)

不幸的是,這只是我的應用程序正在顯示某種跳傘運動行為的一種症狀。 如果這是唯一的問題,我會很高興。 相反,我選擇John Doe,在他的第一排(應該是Crecent City)

在此輸入圖像描述

和BOOM我得到:

在此輸入圖像描述

Leaflet認為我在世界上如何為它繪制兩套坐標,以及是什么讓它認為John Doe在太平洋的某個地方淹死了。

這里沒有什么是有道理的。 我無法看到它輸出的混亂模式。 它只有100行簡單的代碼。

一些想法:

  • conditionalPanel正在混淆我的數據幀? 我不這么認為,因為我可以View(DF)並看到這部分不是問題。
  • 圖標中的分層不起作用? 不知道這將是一個什么問題,因為我們知道這是繪制圖標的正確方法。
  • 我得到一個xtable警告, Warning in run(timeoutMs) : data length exceeds size of matrix ,但這只是tableOutput部分,我認為這與我所遇到的任何問題無關。

我很難過。 一直困在這一整天。 如果有人有任何見解,想法,咒語等,我很樂意聽到他們。

UI.R

library(shiny)
library(ggplot2)
library(dplyr)
library(leaflet)
library(data.table)
options(xtable.include.rownames=F)
library(ggmap)
library(lubridate)

DF <- data.frame(lon=c(-120.6596156, -87.27751, -119.7725868, -124.2026, -117.1858759),  
                 lat=c(35.2827524, 33.83122, 36.7468422, 41.75575, 34.5008311), 
                 date=c('2014-03-14', '2014-01-11', '2013-11-22', '2012-08-23', '2013-08-23'),
                 location=c('San Luis Obispo', 'Jasper', 'Fresno', 'Crescent City', 'Apple Valley'), 
                 P1_name=c('John Doe', 'John Doe', 'John Doe', 'John Doe', 'Joe Blow'),
                 P1_outcome=c('W', 'L', 'D', 'W', 'N'))

DF$date <- as.Date(DF$date, format="%Y-%m-%d")
DF <- arrange(DF, P1_name, date)
DT <- data.table(DF)
DT[, .date := sequence(.N), by = "P1_name"]
DF$date <- paste(DF$date, '   (', DT$.date, ')', sep='')
DF <- arrange(DF, P1_name, desc(date))
DF$P1_name <- as.character(DF$P1_name)
DF$P1_outcome <- as.character(DF$P1_outcome)
DF$location <- as.character(DF$P1_location)
#str(DF$P1_outcome)

icon_W <- makeIcon(
  iconUrl = "http://i58.tinypic.com/119m3r5_th.gif",
  iconWidth = 10, iconHeight = 23,
  iconAnchorX = 10, iconAnchorY =23 
)

icon_L <- makeIcon(
  iconUrl = "http://i62.tinypic.com/2dulcvq_th.jpg",
  iconWidth = 10, iconHeight = 23,
  iconAnchorX = 10, iconAnchorY = 23
)

icon_D <- makeIcon(
  iconUrl = "http://i58.tinypic.com/2zox2yf_th.gif",
  iconWidth = 10, iconHeight = 23,
  iconAnchorX = 10, iconAnchorY = 23
)

icon_N <- makeIcon(
  iconUrl = "http://i62.tinypic.com/339j7de_th.gif",
  iconWidth = 10, iconHeight = 23,
  iconAnchorX = 22, iconAnchorY = 94
)

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

  output$dates<-renderUI({
    selectInput('dates', 'by date / number', choices=DF[which(DF$P1_name == input$person), ]$date, selectize = FALSE)
  })

  output$map<-renderLeaflet({
    validate(
      need(!is.null(input$dates),""),
      need(!is.null(input$person),"")
    )

    if(input$radio=='by date'){
      DF <- filter(DF, P1_name==input$person, date==input$dates)
      View(DF)   
      zoom_num <- 5
      setzoom <- c(DF$lat, DF$lon) 
      outcome <- data.frame(DF$P1_outcome, DF$location)
      output$table <- renderTable(outcome)
    }
    else{
      DF <- filter(DF, P1_name==input$person)
      View(DF)
      zoom_num <- 2
      setzoom <- c(DF$lat[1], DF$lon[1])
      outcome <- data.frame(DF$P1_outcome, DF$location)
      output$table <- renderTable(outcome)
    }



    m <- leaflet(DF) %>%
      addTiles() %>%  # Add default OpenStreetMap map tiles
      setView(lat=setzoom[1], lng=setzoom[2], zoom=zoom_num) %>%
      addMarkers(lat=subset(DF, P1_outcome=='W')$lat, lng=subset(DF, P1_outcome=='W')$lon, icon = icon_W) %>%
      addMarkers(lat=subset(DF, P1_outcome=='L')$lat, lng=subset(DF, P1_outcome=='L')$lon, icon = icon_L) %>%
      addMarkers(lat=subset(DF, P1_outcome=='D')$lat, lng=subset(DF, P1_outcome=='D')$lon, icon = icon_D) %>%
      addMarkers(lat=subset(DF, P1_outcome=='N')$lat, lng=subset(DF, P1_outcome=='N')$lon, icon = icon_N)
  })  #<- end output$map
}     #<- end server function

ui <- fluidPage(
  titlePanel("Location Explorer"),
  sidebarLayout (
    sidebarPanel(
      selectInput('person', 'Select person', choices=unique(DF$P1_name), selectize = FALSE),
      radioButtons('radio', 'Select row(s)', choices=c('by date', 'all'), selected = NULL, inline = TRUE),
      conditionalPanel(
        condition = "input.radio == 'by date'",
        uiOutput('dates')   
      ),
      conditionalPanel(
        condition = "input.radio == 'all'"
      )      
    ),
    mainPanel(
      leafletOutput('map'),      
      fluidRow(column(4, tableOutput('table')))
    ))
)  #<-  end ui

shinyApp(ui = ui, server = server)

其中一個問題可能是您在子集中添加空標記,並且leaflet對此做出了奇怪的反應。

例如,當您選擇Joe BlowP1_outcome == "W""L""D"所有子集都為空。

如上所述在這里 ,你可以使用iconList功能改變取決於圖標P1_outcome並刪除所有的subset

你可以舉例如:

icon_list <- iconList(W=icon_W,L=icon_L,D=icon_D,N=icon_N)

在您定義所有圖標后立即使用:

m <- leaflet(DF) %>%
      addTiles() %>%  # Add default OpenStreetMap map tiles
      setView(lat=setzoom[1], lng=setzoom[2], zoom=zoom_num) %>%
      addMarkers(lat=DF$lat, lng=DF$lon,icon= ~icon_list[DF$P1_outcome]) 

創建你的地圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM