如何在Shiny应用程序的selectModUI中更新传单地图? [英] How to update the leaflet map in the selectModUI in a Shiny app?
问题描述
使用Shiny
时,我想针对不同的leaflet
映射从mapedit
包中更新selectModUI
.下面是一个有效的示例.
I would like to update the selectModUI
from the mapedit
package for different leaflet
maps when using Shiny
. Below is a working example.
library(tidyverse)
library(shiny)
library(sf)
library(leaflet)
library(mapview)
library(mapedit)
library(DT)
library(viridis)
# Load the sf object
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
# Project transformation
nc <- st_transform(nc, crs = 4326)
# Create a color function for the leaflet map
sid74_pal <- colorBin(palette = viridis(10), domain = nc$SID74, bins = 4)
# Create a leaflet map
sid74_map <- leaflet() %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addFeatures(nc,
color = ~sid74_pal(SID74),
label = ~htmltools::htmlEscape(NAME),
layerId = ~seq_len(length(st_geometry(nc)))) %>%
addLegend(position = "bottomright", pal = sid74_pal,
values = nc$SID74,
title = "SID74") %>%
addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
ui <- fluidPage(
# Select Module Output
h3("Map"),
selectModUI(id = "Sel_Map"),
# Datatable Output
h3("Table"),
dataTableOutput(outputId = "Table")
)
server <- function(input, output) {
# Create selectMod
sel <- callModule(selectMod, "Sel_Map", sid74_map)
# Reactive values
rv <- reactiveValues(
selectnum = NULL,
sub_table = nc %>%
st_set_geometry(NULL) %>%
slice(0)
)
# Subset the table based on the selection
observe({
# the select module returns a reactive
gs <- sel()
# Filter for the county data
rv$selectnum <- as.numeric(gs[which(gs$selected == TRUE), "id"])
if (!is.null(rv$selectnum)){
rv$sub_table <- nc %>%
st_set_geometry(NULL) %>%
slice(rv$selectnum)
}
})
# Create a datatable
output$Table <- renderDataTable({
datatable(rv$sub_table, options = list(scrollX = TRUE))
})
}
# Run the application
shinyApp(ui = ui, server = server)
想法是创建地图,用户可以选择或取消选择地图上的多边形.根据用户的选择,数据表输出将动态显示选择了哪个县并显示数据,如屏幕截图所示.
The idea is to create a map and users can select or unselect the polygons on the map. Based on the users' selection, a data table output would dynamically show which counties are selected and present the data, as the screenshot shows.
现在,我想添加一个选择输入,以便用户可以使用应用程序确定要显示的参数.我觉得我可以创建一些反应性或反应性值来存储地图,然后更新以下是我创建的示例".请注意,与示例1相比,我在示例2中创建了一个名为sid79_map
的新传单地图,并添加了一个select输入,以便人们可以选择.但是,此策略不起作用.如果有人可以指出前进的方向,那就太好了.
Now I want to add a select input so users can decide which parameter they want to visualize using the app. I feel like I can create some kinds of reactivities or reactive values to store the maps, and then update the Below is an example I created. Notice that compared to Example 1, I created a new leaflet map called sid79_map
in Example 2 and add a select input so people can select. However, this strategy is not working. It would be great if someone can point out a direction to go.
library(tidyverse)
library(shiny)
library(sf)
library(leaflet)
library(mapview)
library(mapedit)
library(DT)
library(viridis)
# Load the sf object
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
# Project transformation
nc <- st_transform(nc, crs = 4326)
# Create a color function for the leaflet map
sid74_pal <- colorBin(palette = viridis(10), domain = nc$SID74, bins = 4)
sid79_pal <- colorBin(palette = viridis(10), domain = nc$SID79, bins = 4)
# Create a leaflet map
sid74_map <- leaflet() %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addFeatures(nc,
color = ~sid74_pal(SID74),
label = ~htmltools::htmlEscape(NAME),
layerId = ~seq_len(length(st_geometry(nc)))) %>%
addLegend(position = "bottomright", pal = sid74_pal,
values = nc$SID74,
title = "SID74") %>%
addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
sid79_map <- leaflet() %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addFeatures(nc,
color = ~sid79_pal(SID79),
label = ~htmltools::htmlEscape(NAME),
layerId = ~seq_len(length(st_geometry(nc)))) %>%
addLegend(position = "bottomright", pal = sid79_pal,
values = nc$SID79,
title = "SID79") %>%
addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
ui <- fluidPage(
# Select input
selectInput(inputId = "Selection", label = "Select Counties", choices = c("SID74", "SID79"), selected = "SID74"),
# Select Module Output
h3("Map"),
selectModUI(id = "Sel_Map"),
# Datatable Output
h3("Table"),
dataTableOutput(outputId = "Table")
)
server <- function(input, output) {
# Try to create reactivity based on the select input type, not working
sel_type <- reactive({
input$Selection
})
leafmap <- reactive({
if(sel_type() == "SID74"){
sid74_map
} else if (sel_type() == "SID79"){
sid79_map
}
})
# Create selectMod
sel <- callModule(selectMod, "Sel_Map", leafmap())
# Reactive values
rv <- reactiveValues(
selectnum = NULL,
sub_table = nc %>%
st_set_geometry(NULL) %>%
slice(0)
)
# Subset the table based on the selection
observe({
# the select module returns a reactive
gs <- sel()
# Filter for the county data
rv$selectnum <- as.numeric(gs[which(gs$selected == TRUE), "id"])
if (!is.null(rv$selectnum)){
rv$sub_table <- nc %>%
st_set_geometry(NULL) %>%
slice(rv$selectnum)
}
})
# Create a datatable
output$Table <- renderDataTable({
datatable(rv$sub_table, options = list(scrollX = TRUE))
})
}
# Run the application
shinyApp(ui = ui, server = server)
推荐答案
主要问题是您的callModule()
必须位于反应性上下文中.
我已经使用observeEvent()
稍微修改了您的示例以解决此问题.
The main problem is that your callModule()
needs to be inside a reactive context.
I have modified your example slightly to fix that, using observeEvent()
.
请参见下文(我导入了dplyr::slice
,因为我想避免加载完整的tidyverse
).
See below (I imported dplyr::slice
because I wanted to avoid loading the full tidyverse
).
编辑:我做了进一步的清理,并添加了自定义版本的selectMod
来解决OP的评论.
I did some further clean-up and added a custom version of selectMod
to address the OP's comment.
library(shiny)
library(sf)
library(leaflet)
library(mapview)
library(mapedit)
library(DT)
library(viridis)
# Load the sf object
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
# Project transformation
nc <- st_transform(nc, crs = 4326)
# Create a color function for the leaflet map
sid74_pal <- colorBin(palette = viridis(10), domain = nc$SID74, bins = 4)
sid79_pal <- colorBin(palette = viridis(10), domain = nc$SID79, bins = 4)
# Create a leaflet map
sid74_map <- leaflet() %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addFeatures(nc,
color = ~sid74_pal(SID74),
label = ~htmltools::htmlEscape(NAME),
layerId = ~seq_len(length(st_geometry(nc)))) %>%
addLegend(position = "bottomright", pal = sid74_pal,
values = nc$SID74,
title = "SID74") %>%
addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
sid79_map <- leaflet() %>%
addTiles(group = "OSM") %>%
addProviderTiles("CartoDB", group = "CartoDB") %>%
addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
addFeatures(nc,
color = ~sid79_pal(SID79),
label = ~htmltools::htmlEscape(NAME),
layerId = ~seq_len(length(st_geometry(nc)))) %>%
addLegend(position = "bottomright", pal = sid79_pal,
values = nc$SID79,
title = "SID79") %>%
addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
selectMod <- function(input, output, session, leafmap,
styleFalse = list(fillOpacity = 0.2, weight = 1, opacity = 0.4),
styleTrue = list(fillOpacity = 0.7, weight = 3, opacity = 0.7))
{
print("*** custom selectMod")
output$map <- leaflet::renderLeaflet({
mapedit:::add_select_script(leafmap, styleFalse = styleFalse, styleTrue = styleTrue,
ns = session$ns(NULL))
})
id <- "mapedit"
select_evt <- paste0(id, "_selected")
df <- data.frame()
selections <- reactive({
id <- as.character(input[[select_evt]]$id)
if (length(df) == 0) {
# Initial case, first time module is called.
# Switching map, i.e. subsequent calls to the module.
# Note that input[[select_evt]] will always keep the last selection event,
# regardless of this module being called again.
df <<- data.frame(id = character(0), selected = logical(0),
stringsAsFactors = FALSE)
} else {
loc <- which(df$id == id)
if (length(loc) > 0) {
df[loc, "selected"] <<- input[[select_evt]]$selected
} else {
df[nrow(df) + 1, ] <<- c(id, input[[select_evt]]$selected)
}
}
return(df)
})
return(selections)
}
ui <- fluidPage(
# Select input
selectInput(inputId = "Selection", label = "Select Counties", choices = c("SID74", "SID79"), selected = "SID74"),
# Select Module Output
h3("Map"),
selectModUI(id = "Sel_Map"),
# Datatable Output
h3("Table"),
dataTableOutput(outputId = "Table")
)
server <- function(input, output) {
# Reactivity based on the select input type
leafmap <- reactive({
my_sel <- input$Selection
if (my_sel == "SID74") {
sid74_map
} else if (my_sel == "SID79") {
sid79_map
}
})
# Reactive values
rv <- reactiveValues(
sel = reactive({}),
selectnum = NULL,
sub_table = nc %>%
st_set_geometry(NULL) %>%
dplyr::slice(0)
)
# Create selectMod
observeEvent(leafmap(),
rv$sel <- callModule(selectMod, "Sel_Map", leafmap())
)
# Subset the table based on the selection
observeEvent(rv$sel(), {
# The select module returns a reactive
gs <- rv$sel()
# Filter for the county data
rv$selectnum <- as.numeric(gs[which(gs$selected == TRUE), "id"])
rv$sub_table <- nc %>%
st_set_geometry(NULL) %>%
dplyr::slice(rv$selectnum)
})
# Create a datatable
output$Table <- renderDataTable({
datatable(rv$sub_table, options = list(scrollX = TRUE))
})
}
# Run the application
shinyApp(ui = ui, server = server)
这篇关于如何在Shiny应用程序的selectModUI中更新传单地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!