2

The following Shiny app combining ggmap and plotly with heatmap:

require(ggplot2)
require(plotly)
require(ggmap)
require(shiny)

server = function(input, output, session, module_input) {
  
  output$map = renderPlotly({
    
    d = data.frame(lon = rnorm(1e5, mean = mean(c(-125, -66)), sd=4),
                   lat = rnorm(1e5, mean = mean(c(25, 50)), sd=4)) 

    bb = list(bottom = 25, left = -125, top = 50, right = -66)
    basemap = get_stamenmap(bbox = unlist(bb), zoom = 4, maptype = 'toner-lite')
    
    p = ggmap(basemap) +
      stat_bin2d(data = d, aes(lon, lat), alpha = 0.75, drop = TRUE) +
      scale_fill_continuous(low = 'grey95', high = 'red')
    
    ggplotly(p)
  })
}


ui = function() {
  plotlyOutput('map', width = '100vw', height = '100vh')
}

shinyApp(ui, server, options = list(launch.browser = FALSE))

enter image description here

.. works fine but generates a confusing and unnecessary warning that is clogging up my error logs. Have tried suppressWarnings around the plotly call, shinyApp() call, and even the whole script without success (knowing the latter of these two would be bad practice). Any other ideas?

Warning: 'heatmap' objects don't have these attributes: 'mode'
Valid attributes include:
'autocolorscale', 'coloraxis', 'colorbar', 'colorscale', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoverongaps', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'meta', 'metasrc', 'name', 'opacity', 'reversescale', 'showlegend', 'showscale', 'stream', 'text', 'textsrc', 'transforms', 'transpose', 'type', 'uid', 'uirevision', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xgap', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'xtype', 'y', 'y0', 'yaxis', 'ycalendar', 'ygap', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'ytype', 'z', 'zauto', 'zhoverformat', 'zmax', 'zmid', 'zmin', 'zsmooth', 'zsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'heatmap' objects don't have these attributes: 'mode'
Valid attributes include:
'autocolorscale', 'coloraxis', 'colorbar', 'colorscale', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoverongaps', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'meta', 'metasrc', 'name', 'opacity', 'reversescale', 'showlegend', 'showscale', 'stream', 'text', 'textsrc', 'transforms', 'transpose', 'type', 'uid', 'uirevision', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xgap', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'xtype', 'y', 'y0', 'yaxis', 'ycalendar', 'ygap', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'ytype', 'z', 'zauto', 'zhoverformat', 'zmax', 'zmid', 'zmin', 'zsmooth', 'zsrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
geotheory
  • 22,624
  • 29
  • 119
  • 196

2 Answers2

2

The problem is that these warnings are not thrown by ggplotly(), but rather its print() method. When you run suppressWarnings(ggplotly(p)) the warnings still appear, because the implicit printing is not wrapped in suppressWarnings(). But if you make the printing explicit, then suppression works:

suppressWarnings(print(ggplotly(p))) does not yield any warnings.

shs
  • 3,683
  • 1
  • 6
  • 34
  • Thanks. This doesn't actually work for me, but wrapping around the `shinyApp(..)` call does. This feels dangerous as I'll miss all warnings that occur in production, but I suppose it might be the only solution. WIll leave question open for a while otherwise will accept this. – geotheory Feb 04 '22 at 12:59
  • 1
    Sorry, deleted the comment because I was mistaken. It actually still did appear on my machine as well – shs Feb 04 '22 at 13:23
  • Or rather in did appear once I set `launch.browser = T` – shs Feb 04 '22 at 13:24
1

You can use the workaround provided in this answer which disables the warning only for a short time during the plot:

require(ggplot2)
require(plotly)
require(ggmap)
require(shiny)

server = function(input, output, session, module_input) {
  
  output$map = renderPlotly({
    # suppress warnings  
    storeWarn<- getOption("warn")
    options(warn = -1) 
    
    d = data.frame(lon = rnorm(1e5, mean = mean(c(-125, -66)), sd=4),
                   lat = rnorm(1e5, mean = mean(c(25, 50)), sd=4)) 
    
    bb = list(bottom = 25, left = -125, top = 50, right = -66)
    basemap = get_stamenmap(bbox = unlist(bb), zoom = 4, maptype = 'toner-lite')
    
    p = ggmap(basemap) +
      stat_bin2d(data = d, aes(lon, lat), alpha = 0.75, drop = TRUE) +
      scale_fill_continuous(low = 'grey95', high = 'red')
    
    #restore warnings, delayed so plot is completed
    shinyjs::delay(expr =({ options(warn = storeWarn) }) ,ms = 100) 
    
    ggplotly(p)
  })
}


ui = function() {
  plotlyOutput('map', width = '100vw', height = '100vh')
}

shinyApp(ui, server, options = list(launch.browser = FALSE))
HubertL
  • 19,246
  • 3
  • 32
  • 51