如何在R Shiny中添加具有两个不同输入的两个不同标记?

编程入门 行业动态 更新时间:2024-10-09 14:17:59
本文介绍了如何在R Shiny中添加具有两个不同输入的两个不同标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试为两个不同的输入添加两个不同的标记.我让第一个工作,但没有第二个工作.这是我的代码

I am trying to add two different markers for two different inputs. I got the first one working but not for the second one. Here is my code

ui.R

library(shiny) library(leaflet) shinyUI(fluidPage( # Application title titlePanel("Aspen GBS Population Structure results on map"), # Side bar layout sidebarLayout( sidebarPanel( selectInput("structure", label = "Select K for display", choices = c("2", "3", "4", "5", "6"), selected = "2"), checkboxInput("origin", label = "Flood path")), mainPanel( leafletOutput("map") ) ) ) )

server.R

leafIcons <- icons( iconUrl = ifelse(data_K2$FP_Icon == "greenleafIcon", "leafletjs/docs/images/leaf-green.png", "leafletjs/docs/images/leaf-red.png" ), iconWidth = 38, iconHeight = 95, iconAnchorX = 22, iconAnchorY = 94, shadowUrl = "leafletjs/docs/images/leaf-shadow.png", shadowWidth = 50, shadowHeight = 64, shadowAnchorX = 4, shadowAnchorY = 62 ) library(shiny) shinyServer(function(input, output, session) { dt <- reactive( switch(input$structure, "2" = data_K2$Structure.2, "3" = data_K2$Structure.3)) output$map <- renderLeaflet( leaflet(data = data_K2) %>% addTiles() %>% setView(lng = -106.1039361,lat = 50.543981, zoom = 4) %>% addCircleMarkers(lat = ~Lat, lng = ~Long, popup = ~Location_discription, radius=2, color = ~dt(), fill = TRUE) %>% addMarkers(lat = ~Lat, lng = ~Long, popup = ~Location_discription, icon = leafIcons) ) })

当我仅使用checkboxInput按钮时,我希望激活addMarkers.但是现在默认情况下它是选中的.

I want the addMarkers to get activated when i use the checkboxInput button only. But right now it is selected by default.

推荐答案

我发现最简单的方法是用组标记标记,然后仅在输入时显示/隐藏它们.这样,您就可以节省一些计算,而Leaflet旨在使用leafletProxy来完成此操作(Rstudio指南中已对此进行了详细介绍).您还需要添加一个观察者,以更新地图,如本例所示,

I've found the easiest way is to label the markers with groups, then just show/hide them on input. That way you save some computation, and leaflet is designed to do this with leafletProxy (it's well documented on the Rstudio guide). You would need to add an observer as well that would update the map, as in this example,

library(shiny) library(leaflet) ui <- shinyUI(fluidPage( sidebarLayout( sidebarPanel( checkboxInput("show", "Show/Hide") ), mainPanel( leafletOutput("map") ) ) )) dat <- data.frame(lng = rnorm(3, -106.1039361, 0.5) , lat = rnorm(3, 50.543981, 0.5)) server <- shinyServer(function(input, output, session) { ## Your map, give the markers groups output$map <- renderLeaflet( leaflet(data = dat) %>% addTiles() %>% setView(lng = -106.1039361,lat = 50.543981, zoom = 4) %>% addCircleMarkers(group="circles", popup = ~paste(lat), radius=2, fill = TRUE) %>% addMarkers(group="markers") ) ## Observer to update map on input observeEvent(input$show, { proxy <- leafletProxy('map') if (input$show) proxy %>% showGroup('markers') else proxy %>% hideGroup('markers') }) }) shinyApp(ui, server)

更多推荐

如何在R Shiny中添加具有两个不同输入的两个不同标记?

本文发布于:2023-05-31 10:29:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   标记   如何在   Shiny

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!