Giter VIP home page Giter VIP logo

Comments (6)

dcooley avatar dcooley commented on June 11, 2024

the argument to fill_colour should be the name of the variable of data. It should be the column name of a data.frame, not the column of data itself. so you may need fill_colour = input$fill_variable_select

from mapdeck.

shevelp avatar shevelp commented on June 11, 2024

Hi, changing the fill_colour argument does not work. The only "solution" I've found is to render de map again inside de observer, but I think its not a good aproach.

output$map_exp <- mapdeck::renderMapdeck({
          mapdeck::mapdeck(token = mb_token,
                           style = mapdeck::mapdeck_style("light"),
                           location = c(3.5, 43.45),
                           zoom = 3) %>%
            mapdeck::clear_polygon() %>%
            mapdeck::add_polygon(data = filtered_data,
                                 fill_colour = input$fill_variable_select,
                                 fill_opacity = 200,
                                 legend = TRUE,
                                 update_view = FALSE)
        })

from mapdeck.

dcooley avatar dcooley commented on June 11, 2024

If you can make a small reproducible example I may be able to add a better solution?

from mapdeck.

shevelp avatar shevelp commented on June 11, 2024

Hi, I think that the issue its something related with the modular approach. I've created two app.R files, one of them a simple, not modular, shinyapp (app,R) and another one using modules (app_modular.R).

Both use a database that can download from here (test.gpkg): https://we.tl/t-bRof80Ji9T

app.R:

library(shiny)
library(shinycssloaders)
library(shinyWidgets)
library(mapdeck)
library(sf)
library(dplyr)

mb_token <- ' ' #your token

ui <- fluidPage(

  #filter
  shinyWidgets::pickerInput("country", "Country", choices = NULL, selected = NULL, multiple = TRUE, options = list(`actions-box` = TRUE)),

  div(
    shinycssloaders::withSpinner(mapdeck::mapdeckOutput("map"), type = 3, color.background = "blue")
  ),
)

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

  output$map <- mapdeck::renderMapdeck({
    mapdeck::mapdeck(token = mb_token,
                     style = mapdeck::mapdeck_style("light"),
                     location = c(3.5, 43.45),
                     zoom = 3)
  })

  EW_db_NUTS3 <- reactiveVal()
  EW_db <- sf::read_sf("~/Desktop/test.gpkg") #load the dataset (care with path)
  EW_db <- sf::st_transform(EW_db, crs = "+proj=longlat +datum=WGS84")
  EW_db_NUTS3(EW_db)  # Set the value of reactive variable

  shinyWidgets::updatePickerInput(session, "country", choices = unique(EW_db$CNTR_CODE))

  filtered_data <- reactive({

    EW_db <- EW_db_NUTS3()  # Get the value of reactive variable

    #filter by country
    if (!is.null(input$country)) {
      EW_db %>% dplyr::filter(CNTR_CODE %in% input$country)
    } else {
      EW_db
    }

  })

  observe({

        # Here, we establish a dependency on filtered_data()
        data <- filtered_data()
        print(data)

        mapdeck::mapdeck_update(map_id = "map") %>%
          mapdeck::add_polygon(
            data = data,
            fill_colour = input$fill_variable_select,
            fill_opacity = 200,
            legend = TRUE,
            update_view = FALSE)
      })

}

shinyApp(ui, server)

and now app_modular.R:

library(shiny)
library(shinycssloaders)
library(shinyWidgets)
library(mapdeck)
library(sf)
library(dplyr)

#read data
EW_db <- sf::read_sf("~/Desktop/test.gpkg")
EW_db <- sf::st_transform(EW_db, crs = "+proj=longlat +datum=WGS84")

mb_token <- ' ' #your mapdeck token

mod_ui <- function(id) {
  ns <- NS(id)
  tagList(
    # Filter
    pickerInput(ns("country"), "Country", choices = NULL, selected = NULL, multiple = TRUE, options = list(`actions-box` = TRUE)),

    div(
      withSpinner(mapdeckOutput(ns("map")), type = 3, color.background = "blue")
    )
  )
}

mod_server <- function(id, EW_db) {
  moduleServer(
    id,
    function(input, output, session) {
      output$map <- renderMapdeck({
        mapdeck::mapdeck(token = mb_token,
                         style = mapdeck::mapdeck_style("light"),
                         location = c(3.5, 43.45),
                         zoom = 3)
      })

      EW_db_NUTS3 <- reactiveVal(EW_db)

      observe({
        shinyWidgets::updatePickerInput(session, "country", choices = unique(EW_db$CNTR_CODE))
      })

      filtered_data <- reactive({
        EW_db <- EW_db_NUTS3()

        if (!is.null(input$country)) {
          EW_db %>% dplyr::filter(CNTR_CODE %in% input$country)
        } else {
          EW_db
        }
      })

      observeEvent(filtered_data(), {
        data <- filtered_data()
         print(data)

        mapdeck::mapdeck_update(map_id = "map") %>%
          mapdeck::add_polygon(
            data = data,
            fill_colour = input$fill_variable_select,
            fill_opacity = 200,
            legend = TRUE,
            update_view = FALSE
          )
      })
    }
  )
}

ui <- fluidPage(
  mod_ui("module"),
)

server <- function(input, output, session) {
  mod_server("module", EW_db)
}

shinyApp(ui, server)

As I said, I think the problem is related to the map_id argument on map_update. If you need anything else just let me know!

from mapdeck.

shevelp avatar shevelp commented on June 11, 2024

Hi again @dcooley,

I hope you're doing well. Over the weekend, I've been working on the issue and I made an interesting discovery regarding the mapdeck_update function. Unlike other outputs in shiny, it seems that for mapdeck_update, you need to explicitly specify the session in the map_id argument.

To address the issue when the shinyapp is modular, you can simply write the mapdeck update like this:

 mapdeck::mapdeck_update(map_id = session$ns("map"))

from mapdeck.

dcooley avatar dcooley commented on June 11, 2024

excellent, glad you figured it out, and thanks for updating this post with the solution

from mapdeck.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.