1

I'm finding Mapdeck to be a really cool alternative mapping method when working with large datasets in r shiny. However I'm finding it difficult to pass in multiple datasets in order to expand the complexity of the resulting map.

For Example: I'm trying to populate the tooltip to create a popup with 'two' items by pulling in information from a second data frame (to illustrate a 1-to-many relationship). But I can't get it to work. Any thoughts?

Data sample

Map_DF <- data.frame("Point_ID" = 1:3, "Latitude" = c(38.05, 39.08, 40.05), "Longitude" = c(-107.00, -107.05, -108.00))
PointUse_DF <- data.frame("Point_ID" = c(1, 1, 2, 2, 3), "PointUse" = c("farm", "house", "farm", "house", "farm"))

The Code.

# ################################################################################################
# ################################################################################################
# # Sec 1a. Needed Libaries & Input Files

library(shiny)
library(shinydashboard)
library(mapdeck)
library(dplyr)


################################################################################################
################################################################################################
#UI
ui <- dashboardPage(
    dashboardHeader(), 
    dashboardSidebar(), 
    dashboardBody(
        mapdeckOutput(outputId = 'mapA')
    )
)

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


    ##The MapDeck Token
    key <- '## put your own token here ##'
    set_token(key) ## set your access token


    ### The Map
    output$mapA <- renderMapdeck({
     mapdeck() %>%
            add_scatterplot(
                data = Map_DF, 
                lat = "Latitude",
                lon = "Longitude",
                layer_id = 'Point_ID',
                tooltip = c("Point_ID",
                             PointUse_DF$PointUse[Map_DF$Point_ID]  # the idea of passing in a a key and returning multiple items
                            )
             )
    })

}

################################################################################################
################################################################################################
shinyApp(ui = ui, server = server)
rwjam
  • 163
  • 10

1 Answers1

1

The tooltip renders a column of the data object inside the popup. So whatever you want displayed in the popup has to already exist on the data

with your example you need to create the tooltip column

# Using data.table to rshape the data to give a comma-separated list of 'PointUse' values
library(data.table)

dt <- setDT( PointUse_DF )[, .(info = paste0(PointUse, collapse = ", ")), by = Point_ID]

## Now you can put this onto the map object

Map_DF <- merge(
  x = Map_DF
  , y = dt
  , by = "Point_ID"
  , all.x = T
)

mapdeck() %>%
  add_scatterplot(
    data = Map_DF, 
    lat = "Latitude",
    lon = "Longitude",
    layer_id = 'Point_ID',
    radius_min_pixels = 3,
    tooltip = "info"
    )
  )

enter image description here

SymbolixAU
  • 25,502
  • 4
  • 67
  • 139
  • Hmm, okay. Which I guess if it has to come from a column within the single data entry, it can be from a list at least. Good to know, and that solves this case example. – rwjam May 29 '20 at 16:18
  • It is possible to expand upon this 'list' idea? Maybe with a reactive function within the shiny environment? Some other use cases I'm trying to solve include 'coloring the point by PointUse (e.g., farm = red, house = blue, farm & house = first red then blue if filtered) & implementing reactive functions based on inputs$ filters from the shiny app (e.g., only show those points related with 'house', etc etc). Alot of the issues I'm trying to address come down to how to handle that 1-to-many relationship from my data while using MapDeck to plot the sites. – rwjam May 29 '20 at 16:26
  • Can you open an issue on the github page so we can discuss it there? It will be easier, and more appropriate than this comment section. – SymbolixAU May 29 '20 at 21:04
  • Sure. Followed up with a similar comment as an issue. – rwjam Jun 01 '20 at 22:52