如何在sliderbar(交互式地图)中过滤闪亮传单中的年份 库(闪亮) 图书馆(单张) 图书馆(RMySQL) 图书馆(DBI) 数据% addLegend(position=“bottomleft”, title=“事故类型”, pal=d_颜色,值=~d$acci_类型,不透明度=1) }) d_颜色% clearShapes()%>% 添加圆(lng=~d$经度,纬度=~d$纬度,颜色=~d_颜色(d$acci_类型),半径=20, popup=paste0(“事故地点:”,d$事故地址,事故年份:”,d$事故年份,발생건수:", d$발생건수, “사상자수:“,d$사상자수, "사망자수:", d$사망자수, “중상자수:“,d$중상자수, "경상자수:", d$경상자수, “부상자수:“,d$부상자수)) }) } shinyApp(用户界面=用户界面,服务器=服务器)
我为你把一些韩语变为英语! 因为这个函数,我一周内不能进行下一步。。如何在sliderbar(交互式地图)中过滤闪亮传单中的年份 库(闪亮) 图书馆(单张) 图书馆(RMySQL) 图书馆(DBI) 数据% addLegend(position=“bottomleft”, title=“事故类型”, pal=d_颜色,值=~d$acci_类型,不透明度=1) }) d_颜色% clearShapes()%>% 添加圆(lng=~d$经度,纬度=~d$纬度,颜色=~d_颜色(d$acci_类型),半径=20, popup=paste0(“事故地点:”,d$事故地址,事故年份:”,d$事故年份,발생건수:", d$발생건수, “사상자수:“,d$사상자수, "사망자수:", d$사망자수, “중상자수:“,d$중상자수, "경상자수:", d$경상자수, “부상자수:“,d$부상자수)) }) } shinyApp(用户界面=用户界面,服务器=服务器),r,shiny,r-leaflet,R,Shiny,R Leaflet,我为你把一些韩语变为英语! 因为这个函数,我一周内不能进行下一步。。 非常感谢您的回答!!更新 错误持续存在于您的代码中您正在替换地图中的所有点,因此您的地图不会使用sliderInput更改。您需要通过以下方式更改lng=~d$经度,lat=~d$纬度,: lng=~经度,lat=~纬度,这意味着您不想在地图中添加所有的圆~d$longitude lat=~d$latitude,而只想添加通过滑块过滤的圆putlng=~经度lat=~纬度 当您使用Filteredata()进行筛选时,您不需要
非常感谢您的回答!!更新 错误持续存在于您的代码中您正在替换地图中的所有点,因此您的地图不会使用sliderInput更改。您需要通过以下方式更改
lng=~d$经度,lat=~d$纬度,
:
lng=~经度,lat=~纬度,
这意味着您不想在地图中添加所有的圆~d$longitude lat=~d$latitude
,而只想添加通过滑块过滤的圆putlng=~经度lat=~纬度
当您使用Filteredata()进行筛选时,您不需要d中的所有信息,例如d$lat
,您只需要通过滑块输入的筛选信息:~lat
旧答案
代码中的错误如下所示:
proxy(“m”,data=filteredData())%>%
clearShapes()%>%
addCircles(lng=~d$long,lat=~d$lat,color=~d_color(d$acci_类型),#此行
您将使用创建地图的相同点(d$longt和d$lat)替换这些点,因此地图不会更改
要解决此问题,需要按filteredData()列放置点:
以下是全文:
库(闪亮)
图书馆(单张)
d=数据帧(
会计年度=c(2012201320142015),
长=c(126.97806126.97822126125.97806124.97806),
lat=c(37.56667,35.56667,38.56667,37.56667),
acci_类型=c(“低”、“高”、“中”、“高”),
事故发生地点=c(“1字”、“2字”、“3字”、“4字”),
事故2=c(“另一个词1”、“另一个词2”、“另一个词3”、“另一个词4”),
事故3=c(“Otherword1”、“Otherword2”、“Otherword3”、“Otherword4”),
事故4=c(“示例1”、“示例2”、“示例3”、“示例4”),
事故5=c(“另一个示例1”、“另一个示例2”、“另一个示例3”、“另一个示例4”),
事故6=c(“一个或多个示例1”、“一个或多个示例2”、“一个或多个示例3”、“一个或多个示例4”),
事故7=c(“ex1”、“ex2”、“ex3”、“ex4”),
事故8=c(“2号事故1号”、“2号事故2号”、“2号事故3号”、“2号事故4号”)
)
ui%
addLegend(position=“bottomleft”,
标题=”사고유형",
pal=d_颜色,值=~d$acci_类型,不透明度=1)
})
d_颜色%
clearShapes()%>%
添加圆(lng=~long,lat=~lat,color=~d_color(acci_类型),半径=20,
弹出=粘贴0(“
사고장소:“,d$事故发生地点,”
발생년도:“,d$意外事故2,”
意外事故3:”,d$意外事故3,
“
事故4:”,d$事故4,
事故5:”,d$事故5,
“
事故6:”,d$事故6,
事故7:”,d$事故7,
事故(8:,d$事故(8))
} )
}
shinyApp(用户界面、服务器)
您不需要使用dbGetquery两次:
library(shiny)
library(leaflet)
d=data.frame(
acci_year=c(2012,2013,2014,2015),
longt=c(126.97806,126.97822126,125.97806,124.97806),
lat=c(37.56667,35.56667,38.56667,37.56667),
acci_type=c("low","high","medium","high"),
accident_happen_place=c("word1","word2","word3","word4"),
accident_2 =c("anotherword1","anotherword2","anotherword3","anotherword4"),
accident_3=c("otheword1","otheword2","otheword3","otheword4"),
accident_4 =c("example1","example2","example3","example4"),
accident_5 =c("anotherexample1","anotherexample2","anotherexample3","anotherexample4"),
accident_6 =c("onemoreexample1","onemoreexample2","onemoreexample3","onemoreexample4"),
accident_7 =c("ex1","ex2","ex3","ex4"),
accident_8 =c("2_ex1","2_ex2","2_ex3","2_ex4")
)
ui <- navbarPage("Interactive Map",
tabPanel("Map",
leafletOutput("m", height=800),
tags$style("
#controls {
backgropund-color: #ddd;
opacity: 0.7;
}
#controls:hover{
opacity: 1;
}
"),
absolutePanel(id = "controls", class="panel panel-default",
fixed =TRUE, draggable = TRUE, top=60, left="auto",
right=20, bottom ="auto", width=250, height=450,
sliderInput("year",
"years:",
min=min(d$acci_year),
max=max(d$acci_year),
value=2012:2019,
step=1, sep=""))))
server <- function(input, output, session){
filteredData <- reactive({
d[d$acci_year >= input$year[1] & d$acci_year <= input$year[2],]
})
d_colour <- colorFactor("viridis", d$acci_type)
output$m <- renderLeaflet({
leaflet(d) %>%
setView(lng = 126.97806, lat=37.56667, zoom=7) %>%
addTiles() %>%
addCircles(lng=~d$longt, lat=~d$lat, color=~d_colour(d$acci_type), radius=20,
popup=paste0("<br>사고장소:", d$accident_happen_place, "<br>accident_2:", d$accident_2, "<br>accident_3:", d$accident_3,
"<br>accident_4:", d$accident_4, "<br>accident_5:", d$accident_5,
"<br>accident_6:", d$accident_6, "<br>accident_7:", d$accident_7,
"<br>accident_8:", d$accident_8)) %>%
addLegend(position = "bottomleft",
title = "사고유형",
pal = d_colour, values = ~d$acci_type, opacity = 1)
})
d_colour <- colorFactor("viridis", d$acci_type)
observe({
leafletProxy("m", data=filteredData()) %>%
clearShapes() %>%
addCircles(lng=~longt, lat=~lat, color=~d_colour(acci_type), radius=20,
popup=paste0("<br>사고장소:", d$accident_happen_place, "<br>발생년도:", d$accident_2, "<br>accident_3:", d$accident_3,
"<br>accident_4:", d$accident_4, "<br>accident_5:", d$accident_5,
"<br>accident_6:", d$accident_6, "<br>accident_7:", d$accident_7,
"<br>accident_8:", d$accident_8) )
} )
}
shinyApp(ui, server)
d当您键入控制台d时$사고발생지 发生了什么?@KmnsE2啊,它的意思是“列”사고발생지(意外发生地点)“”的数据dMy问题已完成:在observe中的弹出窗口中更改d$->Filteredata()$!!非常感谢您的回答!!但是,我收到了以下错误:addcircles中的错误:找不到函数“addcircles”将addcircles替换为addcircles,R区分大小写。将您的Filteredata更改为My Filteredata=输入$year[1]&d$acci\u一年好的,我会再问!!我再问问题
ui <- navbarPage("Interactive Map",
tabPanel("Map",
leafletOutput("m", height=800),
tags$style("
#controls {
backgropund-color: #ddd;
opacity: 0.7;
}
#controls:hover{
opacity: 1;
}
"),
absolutePanel(id = "controls", class="panel panel-default",
fixed =TRUE, draggable = TRUE, top=60, left="auto",
right=20, bottom ="auto", width=250, height=450,
sliderInput("year",
"years:",
min=min(d$acci_year),
max=max(d$acci_year),
value=range(d$acci_year),
step=1, sep=""))))
server <- function(input, output, session){
filteredData <- reactive({
d[d$acci_year >= input$year[1] & d$acci_year <= input$year[2],]
})
d_colour <- colorFactor("viridis", d$acci_type)
output$m <- renderLeaflet({
leaflet(d) %>%
setView(lng = 126.97806, lat=37.56667, zoom=13) %>%
addTiles() %>%
addCircles(lng=~d$longitude, lat=~d$latitude, color=~d_colour(d$acci_type), radius=20,
popup=paste0("<br>accident place:", d$accident_address, "<br>accident year:", d$acci_year, "<br>발생건수:", d$발생건수,
"<br>사상자수:", d$사상자수, "<br>사망자수:", d$사망자수,
"<br>중상자수:", d$중상자수, "<br>경상자수:", d$경상자수,
"<br>부상자수:", d$부상자수)) %>%
addLegend(position = "bottomleft",
title = "types of accident",
pal = d_colour, values = ~d$acci_type, opacity = 1)
})
d_colour <- colorFactor("viridis", d$acci_type)
observe({
leafletProxy("m", data=filteredData()) %>%
clearShapes() %>%
addCircles(lng=~d$longitude, lat=~d$latitude, color=~d_colour(d$acci_type), radius=20,
popup=paste0("<br>accident place:", d$accident_address, "<br>accident year:", d$acci_year, "<br>발생건수:", d$발생건수,
"<br>사상자수:", d$사상자수, "<br>사망자수:", d$사망자수,
"<br>중상자수:", d$중상자수, "<br>경상자수:", d$경상자수,
"<br>부상자수:", d$부상자수))
})
}
shinyApp(ui=ui, server=server)
leafletProxy("m", data=filteredData()) %>%
clearShapes() %>% clearMarkers() %>%
addCircles(lng=~longt, lat=~lat, #don't forget ~ to specify that the column comes from filteredData()
color=~d_colour(acci_type),
library(shiny)
library(leaflet)
d=data.frame(
acci_year=c(2012,2013,2014,2015),
longt=c(126.97806,126.97822126,125.97806,124.97806),
lat=c(37.56667,35.56667,38.56667,37.56667),
acci_type=c("low","high","medium","high"),
accident_happen_place=c("word1","word2","word3","word4"),
accident_2 =c("anotherword1","anotherword2","anotherword3","anotherword4"),
accident_3=c("otheword1","otheword2","otheword3","otheword4"),
accident_4 =c("example1","example2","example3","example4"),
accident_5 =c("anotherexample1","anotherexample2","anotherexample3","anotherexample4"),
accident_6 =c("onemoreexample1","onemoreexample2","onemoreexample3","onemoreexample4"),
accident_7 =c("ex1","ex2","ex3","ex4"),
accident_8 =c("2_ex1","2_ex2","2_ex3","2_ex4")
)
ui <- navbarPage("Interactive Map",
tabPanel("Map",
leafletOutput("m", height=800),
tags$style("
#controls {
backgropund-color: #ddd;
opacity: 0.7;
}
#controls:hover{
opacity: 1;
}
"),
absolutePanel(id = "controls", class="panel panel-default",
fixed =TRUE, draggable = TRUE, top=60, left="auto",
right=20, bottom ="auto", width=250, height=450,
sliderInput("year",
"years:",
min=min(d$acci_year),
max=max(d$acci_year),
value=2012:2019,
step=1, sep=""))))
server <- function(input, output, session){
filteredData <- reactive({
d[d$acci_year >= input$year[1] & d$acci_year <= input$year[2],]
})
d_colour <- colorFactor("viridis", d$acci_type)
output$m <- renderLeaflet({
leaflet(d) %>%
setView(lng = 126.97806, lat=37.56667, zoom=7) %>%
addTiles() %>%
addCircles(lng=~d$longt, lat=~d$lat, color=~d_colour(d$acci_type), radius=20,
popup=paste0("<br>사고장소:", d$accident_happen_place, "<br>accident_2:", d$accident_2, "<br>accident_3:", d$accident_3,
"<br>accident_4:", d$accident_4, "<br>accident_5:", d$accident_5,
"<br>accident_6:", d$accident_6, "<br>accident_7:", d$accident_7,
"<br>accident_8:", d$accident_8)) %>%
addLegend(position = "bottomleft",
title = "사고유형",
pal = d_colour, values = ~d$acci_type, opacity = 1)
})
d_colour <- colorFactor("viridis", d$acci_type)
observe({
leafletProxy("m", data=filteredData()) %>%
clearShapes() %>%
addCircles(lng=~longt, lat=~lat, color=~d_colour(acci_type), radius=20,
popup=paste0("<br>사고장소:", d$accident_happen_place, "<br>발생년도:", d$accident_2, "<br>accident_3:", d$accident_3,
"<br>accident_4:", d$accident_4, "<br>accident_5:", d$accident_5,
"<br>accident_6:", d$accident_6, "<br>accident_7:", d$accident_7,
"<br>accident_8:", d$accident_8) )
} )
}
shinyApp(ui, server)
d <- dbGetQuery(con, "select * from accidents"
dbGetQuery(con,d)
d <- dbGetQuery(con, "select * from accidents")