我有一个带有是和否的radioButtons输入:
radioButtons("rd", "3 rotor diameter circles:",list("Yes", "No"))
我应该如何修改server.R中的输出,如果选择为YES,它会考虑addCircles
命令,如果是,则忽略该行?
output$mymap <- renderLeaflet({
infile=input$File
if (is.null(infile))
return(NULL)
df.20 <- Coor1
getColor <- function(Layout) {
sapply(Layout$j1, function(j1) {
if(j1 < 1) {
"red"
} else {
"green"
} })
}
icons <- awesomeIcons(
spin=TRUE,
icon = 'star',
iconColor = 'lightgray',
library = 'fa',
markerColor = getColor(df.20)
)
leaflet() %>%
addProviderTiles("OpenTopoMap", group = "MapQuestOpen.Aerial") %>%
addAwesomeMarkers(data = df.20,~long, ~lat, icon=icons, popup = ~as.character(mag), label = ~as.character(Name))%>%
addCircles(data = df.20,lng=~long, lat=~lat,radius=~rad,weight = 1,fill = FALSE)%>%
addMeasure(primaryLengthUnit='meters',primaryAreaUnit='sqmeters')
})
答案 0 :(得分:1)
您应该使用传单的proxy
方法来执行此操作,并在observer
中添加/删除圈子,请考虑以下示例:
library("shiny")
library("leaflet")
df.20 <- quakes[1:20,]
ui <- fluidPage(
radioButtons(inputId = "add_circles", label = "Add circles", choices = c("Yes", "No")),
leafletOutput(outputId = "mymap")
)
server <- function(input, output, session) {
# your initial map
output$mymap <- renderLeaflet({
leaflet(df.20) %>% addTiles() %>% addCircles()
})
# add or remove circles when clicking the radio button
observeEvent(input$add_circles, {
if (input$add_circles == "Yes") {
leafletProxy(mapId = "mymap", data = df.20) %>% addCircles()
} else if (input$add_circles == "No") {
leafletProxy(mapId = "mymap") %>% clearShapes()
}
}, ignoreInit = TRUE)
}
shinyApp(ui = ui, server = server)
查看https://rstudio.github.io/leaflet/shiny.html
注意:此处我使用了clearShapes
,它还删除了多边形,矩形和折线,您只能通过在layerId
中定义addCircles
并使用removeShape
来删除圆圈。 / p>
答案 1 :(得分:0)
你可以试试这个:
leaflet() %>%
addProviderTiles("OpenTopoMap", group = "MapQuestOpen.Aerial") %>%
addAwesomeMarkers(data = df.20,~long, ~lat, icon=icons, popup = ~as.character(mag), label = ~as.character(Name))%>%
{if(input$rd == "Yes")addCircles(data = df.20,lng=~long, lat=~lat,radius=~rad,weight = 1,fill = FALSE)}%>%
addMeasure(primaryLengthUnit='meters',primaryAreaUnit='sqmeters')
我在传单代码中使用了if语句,说如果input$rd == "Yes"
,则应考虑addCircles(...)
。