Comments (6)
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.
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.
If you can make a small reproducible example I may be able to add a better solution?
from mapdeck.
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.
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.
excellent, glad you figured it out, and thanks for updating this post with the solution
from mapdeck.
Related Issues (20)
- Dragging Javascript errors HOT 3
- Custom tile layer (slippy map) HOT 1
- mapdeck crashing in windows R 4.3.1 ... but not in R 4.1.3 HOT 1
- clear_bitmap() not working HOT 2
- add_text: --> Error: Must supply `x`, `y` and `text` attributes HOT 1
- Cesium Troubles HOT 6
- onClick events don't return layerId HOT 1
- Map crashes when hidden in multitab shiny app HOT 9
- Legend order is incorrect for factors HOT 2
- Support for updating pitch/zoom/bearing without specifying the location HOT 3
- quarto tabset and mapdeck HOT 7
- geoarrow + mapdeck HOT 2
- crosstalk support
- Return coordinates with map click event HOT 1
- Styling parameters in add_text()
- Change scatterplot colour on client side HOT 1
- mapdeck_view() doesn't use current viewState properties HOT 5
- map freezes HOT 2
- Upgrade to Deck.gl 9.0.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapdeck.