3

I am using googleway library in Shiny R.

The heatmap displays correctly, but I cannot change the heatmap options. If I uncomment the block code where I try to change options, the app crashes.

Here is the part of the code that works, with the offending lines commented out.

library(googleway)
library(magrittr)
library(shiny)
library(shinydashboard)

# Define UI for app

header1 <- dashboardHeader(
  title = "My Dashboard"
)

sidebar1 <- dashboardSidebar(
  sidebarMenu(
    fileInput("file0", "Choose CSV File",
              multiple = TRUE,
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",".csv")),
    sliderInput("opacity", "Opacity:",
                min = 0, max = 1,
                value = 0.5, step = 0.05),
    sliderInput("radius", "Radius:",
                min = 0, max = 50,
                value = 25),
    sliderInput("blur", "Blur:",
                min = 0, max = 1,
                value = 0.75, step = 0.05),
    sliderInput("maxvalue", "MaxValue:",
                min = 0, max = 1,
                value = 1, step = 0.05)
  ) #sidebarMenu
) #dashboardSidebar

body1 <- dashboardBody(
  fluidRow(
    tabBox(
      title = "TabBox Title 1",
      id = "tabset1", height = "400px", width = 11,
      selected = "Tab1",
      tabPanel("Tab1",
               google_mapOutput("Map1")
      ),
      tabPanel("Tab2", "Tab content 2")
    ) #box
  ) #fluidRow
) #dashboardBody

ui <- dashboardPage(header1, sidebar1, body1)

# Define data
df <- data.frame(lat = c(14.61),
                  lon = c(-90.54),
                  weight = c(100))


# Define SERVER logic
server <- function(input, output, session) {

  map_key <- "my_key"
  ## https://developers.google.com/maps/documentation/javascript/get-api-key

  ## plot the map
  output$Map1 <- renderGoogle_map({
    google_map(key = map_key, data = df, zoom = 2, search_box = F) %>%
      add_heatmap(weight = "weight") #%>%
      #add_traffic()

  }) #renderGoogle_map

  observeEvent(input$opacity, {

    # THIS PART IS COMMENTED OUT BECAUSE THE APP CRASHES
    # google_map_update(map_id = "Map1") %>%
    #   update_heatmap(data = df, option_opacity = input$opacity)

  }) #observeEvent

} #server


# Run app
shinyApp(ui, server)

Your help with this will be greatly appreciated! :)

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
fhaidacher
  • 123
  • 9
  • 1
    `update_heatmap` doesn't have a `option_opacity` argument. You can currently only update the `weight` value – SymbolixAU Jan 11 '18 at 03:41
  • could you pass `input$opacity` directly to `add_heatmap(option_opacity = )` to achieve the control you want? – Nate Jan 11 '18 at 03:46
  • @Nate - yes, but you'd have to `clear` the original heatmap layer first, though, which might look clunky on large data. I've added this as a [potential update](https://github.com/SymbolixAU/googleway/issues/102), but I'm not sure how feasible it is at the moment (note: I'm the author of googleway) – SymbolixAU Jan 11 '18 at 03:48

1 Answers1

3

You can use a reactive({}) to carry the input$opacity value and pass it directly to add_heatmap() to achieve the opacity responsiveness.

This can still be done inside the google_map_update(), but you'd have to clear the heatmap layer first, otherwise you'd just be adding layers on top of each other.

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

    map_key <- "your_key"
    ## https://developers.google.com/maps/documentation/javascript/get-api-key

    opacity <- reactive({
        return(input$opacity)
    })

    ## plot the map
    output$Map1 <- renderGoogle_map({
        google_map(key = map_key, data = df, zoom = 2, search_box = F) %>%
            add_heatmap(weight = "weight") #%>%
        #add_traffic()

    }) #renderGoogle_map

    observeEvent(input$opacity, { 

        google_map_update(map_id = "Map1") %>%
            clear_heatmap() %>%
            add_heatmap(data = df, option_opacity = opacity())
        })
    }

} #server
Nate
  • 10,361
  • 3
  • 33
  • 40
  • a good work-around, but perhaps consider using it inside a `google_map_update`, to update the current map, rather than re-loading a new map each time? – SymbolixAU Jan 11 '18 at 04:10
  • would that require the support you mentioned for `update_heatmap()`? i'm not sure how to implement that, first time seeing this package (looks awesome btw). would love to see how the package author would do it :) – Nate Jan 11 '18 at 04:17
  • There you go; hope that's OK – SymbolixAU Jan 11 '18 at 04:23
  • aha i see now, so this is preferable to preserve other existing map layers and only clear/rebuild the changed layers for the sake of speed? – Nate Jan 11 '18 at 04:25
  • 2
    yes exactly. Also, if you're interested, I've made a lot of additions to [the development version](https://github.com/SymbolixAU/googleway/blob/master/NEWS.md), and examples [on my blog for 'what's next in googleway'](https://www.symbolix.com.au/blog) – SymbolixAU Jan 11 '18 at 04:27
  • 1
    definitely will be reading up on those in the morning. cheers from UTC-5. SO is awesome – Nate Jan 11 '18 at 04:36
  • Awesome! Thank you Nate & SimbolixAU for your help! I see that this is heading in the right direction! The clearing of the heatmap layer makes an unpleasant blink in the app. Also, there are two problems: 1. minimum opacity seems to be truncated at 50% although the slider is at minimum, and 2. every update resets the map's current zoom. I would love it to work like this ... heatmap.joyofdata.de – fhaidacher Jan 11 '18 at 16:41
  • I have reproduced your code in my example and the opacity change works! The reset of the map's zoom still happens with every opacity adjustment. – fhaidacher Jan 11 '18 at 17:47
  • I know things like maintaining zoom are [possible with `library(leaflet)`](https://stackoverflow.com/questions/34985889/how-to-get-the-zoom-level-from-the-leaflet-map-in-r-shiny) but I can't find anything similar in the documentation for `googleway`. There are ways to implement pure `JS` in shiny apps, like `library(shinyjs)`, and the Google Maps API has a function `getZoom()` to do what you are asking, but that a lot of overhead – Nate Jan 11 '18 at 19:47
  • 2
    @fhaidacher - in the development version on github the `add_heatmap()` function has an `update_map_view` argument. Set this to false and it will work. Install instructions for the dev version are on the [homepage](https://github.com/SymbolixAU/googleway) – SymbolixAU Jan 11 '18 at 23:11
  • 1
    In the latest version on github I now have an example where you can now update the [opacity, radius and gradient](https://github.com/SymbolixAU/googleway/blob/master/tests/manual_tests.R#L16). This version will be released to CRAN shortly – SymbolixAU Jan 31 '18 at 03:06