2

I've been trying to develop a few web apps using Mapdeck in R that revolve around building site maps. Right now I'm trying to develop an app that will return a value on click.

My question, with the below sample, what exactly does Mapdeck return on a click event? I thought it was whatever was assigned to the 'id' value, however the stuff I'm getting back seems to be a nested data that isn't related to my data frame input. In the below example I'd like it to return the 'Point_ID' value for each site.

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

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

Map_DF <- data.frame("Point_ID" = c("A1", "B1", "C3"), 
                     "Latitude" = c(38.05, 39.08, 40.05), 
                     "Longitude" = c(-107.00, -107.05, -108.00),
                     "PointUse" = c("farm", "house", "well"),
                     "info" = c("A1 38.05 -107.00 farm", 
                               "B1 39.08 -107.05 house",
                               "C3 40.05 -108.00 well"))


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

################################################################################################
################################################################################################
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",
                id = 'Point_ID',
                tooltip = "info",
                radius = 10000,
                # radius_min_pixels = 3,
                auto_highlight = TRUE,
            )
    })

    ##Click Event, return text
    observe({
        click <- input$mapA_scatterplot_click
        if(is.null(click)) {
            output$textOutA <- renderUI({
                str1 <- paste("Nothing...")
                HTML(paste(str1))
            })
        } else {
            output$textOutA <- renderUI({
                str1 <- paste(click)
                HTML(paste(str1))
            })
        }
    })

}

################################################################################################
################################################################################################
shinyApp(ui = ui, server = server)
rwjam
  • 163
  • 10
  • It's _supposed_ to return the `id` you supply, but I think you've found a [bug](https://github.com/SymbolixAU/mapdeck/issues/307). However, a way around this for now is the `index` you see in the onClick output is the row-index (using 0-based indexing) of your data, so you can use that to subset your data based on the row. – SymbolixAU Jun 11 '20 at 22:35
  • Just to come back to this idea of using the index instead, what would the code for this look like? I'm not sure how to pass the index of a table to mapdeck as an ID value.. – rwjam Jul 13 '20 at 21:20
  • you don't pass the index to mapdeck; mapdeck returns the index of your data – SymbolixAU Jul 13 '20 at 22:28

1 Answers1

1

It might be a bug with id argument . For now, here is what you can do to overcome this (the idea is to use row index contained on the string):

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

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

Map_DF <- data.frame("Point_ID" = c("A1", "B1", "C3"), 
                     "Latitude" = c(38.05, 39.08, 40.05), 
                     "Longitude" = c(-107.00, -107.05, -108.00),
                     "PointUse" = c("farm", "house", "well"),
                     "info" = c("A1 38.05 -107.00 farm", 
                                "B1 39.08 -107.05 house",
                                "C3 40.05 -108.00 well"))


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

################################################################################################
################################################################################################
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, layer_id = "PointUse",
        lat = "Latitude",
        lon = "Longitude",
        id = 'Point_ID',
        tooltip = "info",
        radius = 10000,
        # radius_min_pixels = 3,
        auto_highlight = TRUE,
      )
  })
  

  output$textOutA <- renderUI({
   
    click <- input$mapA_scatterplot_click
    if(is.null(click)) {str1 <- "Nothing..."}
    else {str1 <- Map_DF[as.numeric(gsub(".*:","",gsub(",.*","",click)))+1,]$Point_ID}
    
    HTML(str1)
    })
  

  
}

################################################################################################
################################################################################################
JeanBertin
  • 633
  • 1
  • 7
  • 23