Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control over layers/groups #64

Open
Camil88 opened this issue Mar 10, 2021 · 4 comments
Open

Control over layers/groups #64

Camil88 opened this issue Mar 10, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@Camil88
Copy link

Camil88 commented Mar 10, 2021

Hi,

I'm somehow referring to issue #62. I would like to cover 2 issues here:

  • layers control
  • groups

As for the first issue - I would like to hide/show points on the map upon clicking on the button (in short - toggle points on the map). I'm wondering how I could do it using mapboxer since I can't get to layer's properties like visibility. What I can only do, I guess, is setting visibility using set_layout_properties e.g. set_layout_property(ns("map"), layer_id="points", property="visibility", value="visible"). However, I don't know how to first get to the layer's properties (something what's possible in mapbox's function getLayoutProperty). Please take a look at this example from mapbox site:
https://docs.mapbox.com/mapbox-gl-js/example/toggle-layers/
where visibility is get/set using getLayoutProperty/setLayoutProperty.

Or maybe there's other way to do it, without getting and setting visibility from a layer. Below is my workable example from #62, where I added some dummy code (in observeEvent) showing how I would 'toggle' points on the map (I have only one button in this example but in my app I have 3 buttons and want to add some logic to observe event, e.g if points 'xyz' are visible one the map then show points 'kkk' etc.).

Another issue is groups. Is it possible to make groups using mapboxer just like it's possible in leaflet? In leaflet I can use function addLayersControl which manage groups quite easily (also enables toggling points on a map). Please take a look at this example where it's shown clearly: https://rstudio.github.io/leaflet/showhide.html

And here is my workable example with commented dummy code:

library(shiny)
library(mapboxer)
library(dplyr)
library(sf)

Sys.setenv(MAPBOX_API_TOKEN =    "pk.eyJ1IjoiaHdsIiwiYSI6ImNramJxY2YxcDV2YXoyeW40YXlvbmUyazQifQ.7HBEvMyrAnVpkKO7MNH7ww")

moduleServer <- function(id, module) {
callModule(module, id)
}

# UI 
mod_btn_UI <- function(id) {

ns <- NS(id)
tagList(
    actionButton(ns("btn"), "Click me!"),
    mapboxerOutput(ns("map"))
    #leafletOutput(ns("map"), width = "100%")
)
}

# Server 
mod_btn_server <- function(id){
moduleServer(id, function(input, output, session) {
    
  ns <- NS(id)
  
    coords <- quakes %>%
        sf::st_as_sf(coords = c("long","lat"), crs = 4326)
         
    output$map <- mapboxer::renderMapboxer({
        mapboxer::mapboxer(
            style = mapboxer::basemaps$Mapbox$light_v10,
            center = c(174.387636,-33.543557),
            logoPosition = "bottom-left",
            pitch = 5,
            padding = 0,
            zoom = 3)
    })
    
    # visibility = mapboxer::get_layout_property(ns(map),layer_id="xyz",'visibility')

    observeEvent(input$btn, {
      # if(visibility == "visible") {
      # mapboxer::set_layout_property(ns("map"),"xyz",'visibility',"none")
      # }
      # else {
        mapboxer::mapboxer_proxy(ns("map")) %>%
            mapboxer::add_circle_layer(
                id = "xyz",
                source = mapboxer::as_mapbox_source(coords),
                circle_color = "#952444",
                circle_opacity = 0.7,
                circle_radius = 6
            ) %>%
            mapboxer::update_mapboxer()
      # }
    })
                   
})
}

# App 

ui <- fluidPage(    
tagList(
    mod_btn_UI("test-btn"))
)

server <- function(input, output, session) {
mod_btn_server("test-btn")    
}

shinyApp(ui = ui, server = server)
@crazycapivara
Copy link
Owner

@Camil88 See shiny-toogle-layer-example where it is done via set_layout_property. You just have to store the current visibility properties of your layers in your shiny app. Furthermore, a layer switcher control is on the way, I already played around with a lot of new controls in the develop and feature/sliders branch. But I have to finish them, so that they can be merged into master. Maybe I can add a addLayerSwitcherControl in the coming days, because I think it is very useful.

@crazycapivara crazycapivara added the enhancement New feature or request label Mar 11, 2021
@Camil88
Copy link
Author

Camil88 commented Mar 11, 2021

Thanks for example, I'll look into it. I guess such a switcher would be a nice option either.

@crazycapivara
Copy link
Owner

#49

@tvroylandt
Copy link

Hi @crazycapivara,

Thanks for this great package. Is there an option to use this without Shiny (or with a mix of R & JS) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants