0
votes

I'm building a shinydashboard with a leaflet map display and am having difficulty showing popup text based on a user input. Here is the code so far (the "output$bar" part can be ignored as I haven't started working on that yet):

 ## app.R ##
library(shinydashboard)
library(shiny)
library(leaflet)
library(ggplot2)

refs <- read.csv('./BSAProjects/HurricaneModel/Data/monthlyrefinerymeanprod.csv', stringsAsFactors = FALSE)
hurs <- read.csv('./BSAProjects/HurricaneModel/Data/hurrdates.csv', stringsAsFactors = FALSE)

huricon <- makeIcon('http://www.youngsvillepolice.com/wp-content/uploads/2015/11/action-icon-HURRICANE.png',
                    iconWidth = 70,
                    iconHeight = 70)

ui <- dashboardPage(
    dashboardHeader(title = 'Hurricane Model'),
    dashboardSidebar(disable = TRUE),
    dashboardBody(
        fluidRow(
            box(leafletOutput('leaflet'),
                width = '100%')),
        fluidRow(
            box(selectInput('menu','Hurricane', choices = hurs$hurricane)),
            box(plotOutput('bar', 'Production Levels'))
        )
    )
)

server <- function(input, output) {
    colors <- colorFactor(rainbow(length(refs$SPRRegion)), refs$SPRRegion)

    output$leaflet <- renderLeaflet({
        map <- leaflet() %>%
            addTiles(urlTemplate = 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}') %>%
            addCircleMarkers(~Longitude,
                             ~Latitude,
                             popup = paste('<b>EIAID:</b>', refs$EIAID, '<br>',
                                           '<b>SPR Refinery Group:</b>', refs$Refinery.Group, '<br>',
                                           '<b>Refinery:</b>', refs$SprRespondentName, '<br>',
                                           '<b>Capacity:</b>', refs$Capacity, 'MBbl/day'),
                             clusterOptions = markerClusterOptions(),
                             color = ~colors(SPRRegion),
                             data = refs) %>% 
            addMarkers(~longitude, 
                       ~latitude,
                       icon = huricon,
                       popup = paste('<b>Hurricane:</b>', hurs$hurricane,'<br>',
                                     '<b>Date:</b>', hurs$date,'<br>',
                                     '<b>HSI:</b>', hurs$hsi),
                       data = hurs[hurs$hurricane == input$menu,])
    })

    output$bar <- renderPlot({
        bar <- ggplot(hurs, aes())
    })
}


shinyApp(ui, server)

What I'm attempting to do is to allow the user to select a hurricane by the drop down menu created with "selectInput" in the UI object. That will then add a marker to the leaflet map with a popup containing the information I have in "addMarkers(popup =...)"

I can successfully get the functionality to place the marker on the leaflet map based on the drop down menu selection, but the popup text is always for the same hurricane that appears in the last row of the "hurs" data file. If I remove the line:

data = hurs[hurs$hurricane == input$menu,]

and have all the hurricanes plotted instead

data = hurs

then I have no problems. I don't have any problems with the labels updating in my "addCircleMarkers" object either so I suspect this has something to do with how the data set is being filtered based on the drop down menu.

Any ideas?

1

1 Answers

0
votes

Figured this out after seeing a similar post here (How to subset a dataframe and plot with leaflet depending on inputselect in shiny).

It seems the issue is that the subsetted data needs to be produced outside of the call to "addMarkers." I was trying to subset within and was receiving this issue, but merely pulling it out and placing it immediately following the call to "renderLeaflet" worked:

server <- function(input, output) {
colors <- colorFactor(rainbow(length(refs$SPRRegion)), refs$SPRRegion)

output$leaflet <- renderLeaflet({
    hur.subset <- subset(hurs, hurs$hurricane == input$menu) ##PLACED IT HERE
    map <- leaflet() %>%
        addTiles(urlTemplate = 'http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}') %>%
        addCircleMarkers(~Longitude,
                         ~Latitude,
                         popup = paste('<b>EIAID:</b>', refs$EIAID, '<br>',
                                       '<b>SPR Refinery Group:</b>', refs$Refinery.Group, '<br>',
                                       '<b>Refinery:</b>', refs$SprRespondentName, '<br>',
                                       '<b>Capacity:</b>', refs$Capacity, 'MBbl/day'),
                         clusterOptions = markerClusterOptions(),
                         color = ~colors(SPRRegion),
                         data = refs) %>% 
    addMarkers(~longitude,
                ~latitude,
                icon = huricon,
                popup = paste('<b>Hurricane:</b>', hur.subset$hurricane,'<br>',
                             '<b>Date:</b>', hur.subset$date,'<br>',
                             '<b>HSI:</b>', hur.subset$hsi),
                data = hur.subset)
})

}