使用UI打开和关闭数据和传单显示 [英] Using UI to turn on data and leaflet display on and off

查看:90
本文介绍了使用UI打开和关闭数据和传单显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ui.R和server.R文件中设置了此问题的样本.这是ui.R文件.有三个随机数据集.无论使用UI进行什么操作,都将显示带有标记的标记.其他两个数据集仅应在请求为圆形"的情况下绘制.

I have set up a sample of this problem in ui.R and server.R files. This is the ui.R file. There are three random data sets. The one with markers will be displayed no matter what is done with the UI. The other two data sets should get plotted only if requested as "Circles".

library(shiny)
library(leaflet)
pageWithSidebar(
    headerPanel("Main Title Here"),
    sidebarPanel(
        checkboxInput('Circles', 'Show Locations', TRUE)
    ),

    mainPanel(
        leafletOutput("map")
    )

)

该映射由server.R生成,如下所示.这样会生成随机数据以及纬度和经度位置.

The map is generated by server.R, which follows below. This generates the random data and the latitude and longitude locations.

library(shiny)
library(leaflet)
library(ggplot2)

shinyServer(function(input, output) {
    output$map <- renderLeaflet({
        leaflet() %>%
             addTiles(group = "OpenStreetMap") %>%
             addMarkers(runif(20,-75,-74),
                  runif(20, 41, 42))
    })
    observe({
        proxy <- leafletProxy("map")
        if (input$Circles == TRUE) {
            proxy %>% addCircles(runif(20,-75,-74),
                           runif(20, 41, 42),
                           group = "Circles",
                           color = "red") %>%
            addCircles(runif(20,-75,-74),
                   runif(20, 41, 42),
                   group = "Circles",
                   color = "blue") %>%
            addLegend(
                position = "bottomright",
                colors = c("red", "blue"),
                labels = c("Group 1", "Group 2")
            )
      # cannot put group or label on legend.
      } else {
          proxy %>% hideGroup("Circles")
          # %>% hideGroup("Group 2")
      }
    })
})

在程序启动时,显示圆圈已打开(TRUE).我在这里看到两个主要问题.首先,可以关闭圆圈,但这就是所有UI控件的结束.问题在于它们无法再次打开.如果我尝试将checkboxInput启动为FALSE,则UI控件不会将其打开.我不知道这是标准的,还是做错了什么.

Displaying the circles is turned on (TRUE) as the program starts. I am seeing two major problems here. First, the circles can be turned off, but that is the end of any UI control. The problem is that they cannot be turned on again. If I try starting checkboxInput as FALSE, the UI control will not turn it on. I don't know if that is standard, or if I am doing something wrong.

第二个主要问题是我无法编程将图例与圆一起关闭.圆形通过hideGroup()函数关闭.拒绝将其用于图例.我接受了我尝试过的命令,并将其变成了注释行.继续并删除#号,以便您可以看到它产生的错误.我尚未找到替代方法.

The second major problem is that I cannot program the legend to be turned off along with the circles. The circles are turned off with the hideGroup() function. This is rejected for use on the legend. I have taken the command I tried and turned it into a comment line. Go ahead and remove the # sign so that you can see the errors that it creates. I have not yet found an alternative.

推荐答案

我要感谢SymblixAU向正确的方向发送邮件. hideGroup和showGroup的问题在于这两个函数都带有少量变量.数据显示必须由其他命令(例如addCircles或addMarkers)预先定义.我终于有了一个工作版本,如下所示:

I would like to thank SymblixAU for sending me in the right direction. The problem with hideGroup and showGroup is that both functions take a small number of variables. The data display has to be pre-defined by other commands, such as addCircles or addMarkers. I finally have a working version, shown below:

library(shiny)
library(leaflet)

shinyServer(function(input, output) {
    output$map <- renderLeaflet({
        leaflet() %>%
            addTiles(group = "OpenStreetMap") %>%
            addMarkers(runif(20,-75,-74),
                   runif(20, 41, 42))
    })

    proxy <- leafletProxy("map")

    proxy %>% addCircles(runif(20,-75,-74),
                     runif(20, 41, 42),
                     group = "Circles",
                     color = "red") %>%
    addCircles(runif(20,-75,-74),
               runif(20, 41, 42),
               group = "Circles",
               color = "blue")

# now we can show or hide depending on UI
    observe({
        if (input$Circles == TRUE) {
            proxy %>% showGroup("Circles")
        } else {
            proxy %>% hideGroup("Circles")
        }
    })

})

我也放弃了使用图例.我还没有找到可以充分控制添加到图中的图例数量的软件,因此我将不得不用另一种方式来描述颜色的使用,也许是在文字中.

I have also abandoned the use of the legend. I have yet to find software that will adequately control the number of legends being added to the plot, so I will have to describe the use of colors another way, perhaps in the text.

这篇关于使用UI打开和关闭数据和传单显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆