R 切换输入时,闪亮的传单贴图渲染两次
我正在尝试制作一个带有传单的闪亮应用程序,它可以根据不同的输入标准呈现choropleth地图。地图显示不同类型(R 切换输入时,闪亮的传单贴图渲染两次,r,shiny,leaflet,r-leaflet,R,Shiny,Leaflet,R Leaflet,我正在尝试制作一个带有传单的闪亮应用程序,它可以根据不同的输入标准呈现choropleth地图。地图显示不同类型(input$type)和背景(input$background)的事件。指定其他类型或背景时,多边形将填充更新的事件数据。它工作正常,但有一个障碍。当我将日期输入从日期范围(input$dateInput)切换到总统任期(input$president)时,总统任期的choropleth渲染一次,显示没有数据的多边形,然后再次显示填充了预选期间正确数据的多边形(“President1
input$type
)和背景(input$background
)的事件。指定其他类型或背景时,多边形将填充更新的事件数据。它工作正常,但有一个障碍。当我将日期输入从日期范围(input$dateInput
)切换到总统任期(input$president
)时,总统任期的choropleth渲染一次,显示没有数据的多边形,然后再次显示填充了预选期间正确数据的多边形(“President1”)。当按下“总统”选项卡时,如何避免像这样两次渲染地图
问题也列在RStudio社区上
可以在此处访问使用的原始数据和形状文件:
工作示例:
原始数据的名称:wikiraw
形状文件的名称:shapefile
全球:
library(shiny)
library(shinydashboard)
library(shinythemes)
library(leaflet)
library(rgdal)
library(rmapshaper)
library(sp)
library(dplyr)
library(lubridate)
wikiraw <-read.csv("~/wikisample_SO.csv")
shapefile <- readOGR("~/EGY_adm2.shp")
shapefile<-spTransform(shapefile, CRS("+init=epsg:4326"))
## Simplify shapefile to speed up rendering
shapefile <- ms_simplify(shapefile, keep = 0.01, keep_shapes = TRUE)
wikbounds<-bbox(shapefile)
wikiraw$incident_date <- as.Date(wikiraw$incident_date,
format = "%m/%d/%Y")
wikiraw$presidency <- rep(NA, nrow(wikiraw))
wikiraw$incident_date1 <- as.numeric(wikiraw$incident_date)
wikiraw$event <- rep(1,nrow(wikiraw))
## Generate presidency categorical var.
wikiraw$presidency <- cut(wikiraw$incident_date1,
breaks = c(-Inf, 15016, 15521, 15889, 16229, Inf),
labels = c("President1", "President2", "President3", "President4", "President5"),
right = FALSE)
用户界面:
ui如果将reactive
更改为reactiveValue
,并将数据分配到观察?我不知道它是否正常工作,因为我不知道应该使用哪些形状和颜色,但我再也看不到这种双重渲染了
(使用问题中的数据和准备)
库(闪亮)
图书馆(shinydashboard)
图书馆(shinythemes)
图书馆(单张)
图书馆(rgdal)
图书馆(rmapshaper)
图书馆(sp)
图书馆(dplyr)
图书馆(lubridate)
ui如果将反应性
更改为反应性值
,并在观察中分配数据,该怎么办?我不知道它是否正常工作,因为我不知道应该使用哪些形状和颜色,但我再也看不到这种双重渲染了
(使用问题中的数据和准备)
库(闪亮)
图书馆(shinydashboard)
图书馆(shinythemes)
图书馆(单张)
图书馆(rgdal)
图书馆(rmapshaper)
图书馆(sp)
图书馆(dplyr)
图书馆(lubridate)
您应该提供一个可复制的示例。否则,我们无法运行您的代码。感谢您抽出时间查看此内容。我已经编辑了原始帖子,添加了到github repo的链接,其中包含原始数据和使用的shapefile。我无法加载您的shapefile…我想我们还需要用补充shapefile更新.shp文件github repo的其他文件。再次感谢!这很奇怪,我没有发现这样的错误。我刚才还检查了,ID_2在shapefile和wikiraw中都是整数类。My sessionInfo如果有用:>sessionInfo()R版本3.4.3(2017-11-30)平台:x86_64-apple-darwin15.6.0(64位)运行在:macOS High Sierra 10.13.3下您应该提供一个可复制的示例。否则,我们无法运行您的代码。感谢您抽出时间查看此内容。我已经编辑了原始帖子,添加了到github repo的链接,其中包含原始数据和使用的shapefile。我无法加载您的shapefile…我想我们还需要用补充shapefile更新.shp文件github repo的其他文件。再次感谢!这很奇怪,我没有发现这样的错误。我刚才还检查了,ID_2在shapefile和wikiraw中都是整数类。我的sessionInfo如果有用:>sessionInfo()R版本3.4.3(2017-11-30)平台:x86_64-apple-darwin15.6.0(64位)运行在:macOS High Sierra 10.13.3下我想这已经解决了它!我需要更多地使用它来确保效果,但现在它的渲染似乎是正确的。非常感谢(非常抱歉,我只是抽出时间来看看这个)。今晚我要为你举杯!我想这已经解决了!我需要更多地使用它来确保效果,但现在它的渲染似乎是正确的。非常感谢(非常抱歉,我只是抽出时间来看看这个)。今晚我要为你举杯!
ID_2 incident_date incident_background incident_type presidency event
1 168 2013-11-26 Cultural Group President4 1
2 133 2013-11-29 Cultural Group President4 1
3 137 2014-01-25 Cultural Group President4 1
4 168 2011-01-28 Cultural Collective President1 1
5 168 2016-04-25 Cultural Group President5 1
6 163 2015-02-08 Political Individual President5 1
ui <- dashboardPage(
dashboardHeader(title = "Map tool"),
dashboardSidebar(sidebarMenu(menuItem("Map", tabName = "map"),
selectInput("input_type", "Date input type",
c("Date", "Presidency")),
uiOutput("dateSelect"),
uiOutput("typeSelect"),
uiOutput("backgroundSelect"),
uiOutput("presidentSelect"))),
dashboardBody(tabItems(
tabItem(tabName = "map",
leafletOutput("mymap", height=500)))))
server <- function(input, output, session) {
output$dateSelect <- renderUI({
switch(input$input_type,
"Date" = dateRangeInput("dateInput", "Dates:",
min=min(wikiraw$incident_date), max = max(wikiraw$incident_date),
start = min(wikiraw$incident_date), end = max(wikiraw$incident_date)),
"Presidency" = checkboxGroupInput("president", "Presidency",
choices = levels(wikiraw$presidency),
selected = "President1"))
})
output$typeSelect <- renderUI({
selectInput("type", "Incident type",
choices = unique(wikiraw$incident_type), multiple = TRUE,
selected = wikiraw$incident_type[1])})
output$backgroundSelect <- renderUI({
checkboxGroupInput("background", "Incident background",
choices = unique(wikiraw$incident_background),
selected = wikiraw$incident_background[1])})
selected <- reactive({
wikiagg <- wikiraw %>% group_by(ID_2, incident_date, incident_type, incident_background, presidency) %>%
summarize(sum_event = sum(event))
if(input$input_type=="Date"){wikiagg <- filter(wikiagg,
incident_date >= min(input$dateInput),
incident_date <= max(input$dateInput),
incident_type%in%input$type,
incident_background%in%input$background)}
if(input$input_type=="Presidency"){wikiagg <- filter(wikiagg,
incident_type%in%input$type,
incident_background%in%input$background,
presidency%in%input$president)}
wikiagg <- wikiagg %>% group_by(ID_2) %>%
summarize(sum_event = sum(sum_event))
wikiagg
})
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(mean(wikbounds[1,]),
mean(wikbounds[2,]),
zoom=6
)
})
observe({
if(!is.null(input$dateInput)){
shapefile@data <- left_join(shapefile@data, selected(), by="ID_2")
##Define palette across range of data
wikiaggpal <- wikiraw %>% group_by(ID_2) %>%
summarize(sum_event = sum(event))
pal <- colorBin("YlOrRd", wikiaggpal$sum_event, bins=5, na.color = "#bdbdbd")
leafletProxy("mymap", data = shapefile) %>%
addTiles() %>%
clearShapes() %>%
addPolygons(data = shapefile, fillColor = ~pal(sum_event), fillOpacity = 0.7,
color = "white", weight = 2)
}})
}
shinyApp(ui, server)
library(shiny)
library(shinydashboard)
library(shinythemes)
library(leaflet)
library(rgdal)
library(rmapshaper)
library(sp)
library(dplyr)
library(lubridate)
ui <- dashboardPage(
dashboardHeader(title = "Map tool"),
dashboardSidebar(sidebarMenu(menuItem("Map", tabName = "map"),
selectInput("input_type", "Date input type",
c("Date", "Presidency")),
uiOutput("dateSelect"),
uiOutput("typeSelect"),
uiOutput("backgroundSelect"),
uiOutput("presidentSelect"))),
dashboardBody(tabItems(
tabItem(tabName = "map",
leafletOutput("mymap", height=500)))))
server <- function(input, output, session) {
output$dateSelect <- renderUI({
switch(input$input_type,
"Date" = dateRangeInput("dateInput", "Dates:",
min=min(wikiraw$incident_date), max = max(wikiraw$incident_date),
start = min(wikiraw$incident_date), end = max(wikiraw$incident_date)),
"Presidency" = checkboxGroupInput("president", "Presidency",
choices = levels(wikiraw$presidency),
selected = "President1"))
})
output$typeSelect <- renderUI({
selectInput("type", "Incident type",
choices = unique(wikiraw$incident_type), multiple = TRUE,
selected = wikiraw$incident_type[1])})
output$backgroundSelect <- renderUI({
checkboxGroupInput("background", "Incident background",
choices = unique(wikiraw$incident_background),
selected = wikiraw$incident_background[1])})
sel_reactval = reactiveValues(s = NULL)
# selected <- reactive({
observe({
wikiagg <- wikiraw %>% group_by(ID_2, incident_date, incident_type, incident_background, presidency) %>%
summarize(sum_event = sum(event))
if(input$input_type=="Date"){wikiagg <- filter(wikiagg,
incident_date >= min(input$dateInput),
incident_date <= max(input$dateInput),
incident_type%in%input$type,
incident_background%in%input$background)}
if(input$input_type=="Presidency"){wikiagg <- filter(wikiagg,
incident_type%in%input$type,
incident_background%in%input$background,
presidency%in%input$president)}
wikiagg <- wikiagg %>% group_by(ID_2) %>%
summarize(sum_event = sum(sum_event))
sel_reactval$s = wikiagg
# wikiagg
})
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(mean(wikbounds[1,]),
mean(wikbounds[2,]),
zoom=6
)
})
observe({
req(!is.null(input$dateInput))
req(nrow(as.data.frame(sel_reactval$s))!=0)
# if(!is.null(input$dateInput)){
# shapefile@data <- left_join(shapefile@data, selected(), by="ID_2")
shapefile@data <- left_join(shapefile@data, sel_reactval$s, by="ID_2")
##Define palette across range of data
wikiaggpal <- wikiraw %>% group_by(ID_2) %>%
summarize(sum_event = sum(event))
pal <- colorBin("YlOrRd", wikiaggpal$sum_event, bins=5, na.color = "#bdbdbd")
leafletProxy("mymap") %>%
addTiles() %>%
clearShapes() %>%
addPolygons(data = shapefile, fillColor = ~pal(sum_event), fillOpacity = 1,
color = "white", weight = 2)
# }
})
}
shinyApp(ui, server)