R-graphic flyer is unpredictable

My Shiny application uses data tactics:

enter image description here

and subsets accordingly, allowing the user to select the person ( P1_name ) and date ( date ).

When launching, it looks like this:

enter image description here

and already, it’s clear that the application is not working. There should be a letter "N" at the location of the Apple Valley city, but instead there is nothing. I cannot understand why, since DF was subset correctly:

enter image description here

and the layers should be built correctly:

 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) 

Unfortunately, this is just one of the symptoms of some kind of behavior that my application is exhibiting. If this was the only problem, I would be happy. Instead, let's say I choose John Doe in his front row (which should be a crater city)

enter image description here

and BOOM I get:

enter image description here

As in the world, Leaflet thought that I gave him two sets of coordinates to speak, and that made him think that John Doe once grew up in the Pacific Ocean.

Nothing makes sense here. I do not see the pattern in the chaos that it displays. These are just 100 lines of simple code.

Some ideas:

  • conditionalPanel mixes my data framework? I don’t think so, since I can View(DF) and see that this part is not a problem.
  • The markup in the icons does not work? Not sure how this will be a problem, since we know that this is the right way to create icons.
  • I get a warning xtable , Warning in run(timeoutMs) : data length exceeds size of matrix , but this is only for the part of tableOutput , which, I believe, is not related to any problem that I am not familiar with.

I'm at a dead end. Stuck on this all day. If anyone has ideas, ideas, spells, etc., I would love to hear them.

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) 
+4
source share
1 answer

One problem may be that you add empty tokens to your subsets, and the leaflet reacts strangely to this.

For example, if you select Joe Blow all subsets for P1_outcome == "W" , "L" or "D" are empty.

As described here , you can use the iconList function to change the icons depending on P1_outcome and delete all the subset .

You can add, for example:

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

immediately after defining all the icons and use:

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

to create your map.

+3
source

All Articles