R Leaflet:条件面板未显示在地图

时间:2017-01-19 23:14:07

标签: r shiny leaflet

当我点击一个圆圈时,我想在地图中显示一个条件面板,如果我在一个圆圈外面点击,这个条件面板必须消失,但它不会出现,我不知道为什么。< / p>

我认为它是关于反应值(再一次)。

如果有任何想法,请告诉我。

非常感谢,这是一个可重复的例子(感谢SymbolixAU):

ui:

library(shiny)
library(leaflet)

ui <- fluidPage(
  leafletOutput("mymap",width="100%",height="750px"), 

  conditionalPanel(

    condition = "output.COND == '2'",

    fluidRow(

      absolutePanel(id = "cond_panel", 
                    class = "panel panel-default", 
                    fixed = TRUE,
                    draggable = TRUE, 
                    top = "auto", 
                    left = 200, 
                    right = "auto", 
                    bottom = 0,
                    width = 400, 
                    height = 400,
                    fluidRow(

                    ) # e. of fluidRow(

       ) # # e. of absolutePanel

    ) # e. of fluidRow

  ) # e. of conditionalPanel

) # e. of fluidPage

和服务器:

server <- function(input, output){

  rv <- reactiveValues()
  rv$myDf <- NULL
  rv$cond <- NULL

  cities <- read.csv(textConnection("
                                City,Lat,Long,Pop
                                Boston,42.3601,-71.0589,645966
                                Hartford,41.7627,-72.6743,125017
                                New York City,40.7127,-74.0059,8406000
                                Philadelphia,39.9500,-75.1667,1553000
                                Pittsburgh,40.4397,-79.9764,305841
                                Providence,41.8236,-71.4222,177994
                                "))
  cities$id <- 1:nrow(cities) 

  output$mymap <- renderLeaflet({
    leaflet(cities) %>% addTiles() %>%
      addCircles(lng = ~Long, lat = ~Lat, weight = 1,
             radius = ~sqrt(Pop) * 30, popup = ~City, layerId = ~id)
  })

  observeEvent(input$mymap_click, {

    print("map clicked")
    rv$cond <- "1"
    print(paste("Value rv$cond = ", rv$cond))
    output$COND <- reactive({rv$cond})
    leafletProxy("mymap")

  }) # e. of observeEvent

  observeEvent(input$mymap_shape_click, {

    print("shape clicked")
    rv$cond <- "2"
    print(paste("Value rv$cond = ", rv$cond))
    output$COND <- reactive({rv$cond})
    leafletProxy("mymap")

  }) # e. of observeEvent

} # e. of server

2 个答案:

答案 0 :(得分:2)

我将提出一种稍微不同的方法,使用library(shinyjs)来使用javascript来控制面板是否隐藏。

在此示例中,我创建了一个隐藏 div 元素(即,当应用打开时,该面板将开始隐藏)。那么&#39; div&#39; div单击圆圈时显示,单击地图时再次隐藏。

这个答案的灵感来自@Daattali's answer here(他是library(shinyjs)的作者。

library(shiny)
library(leaflet)
library(shinyjs)

ui <- fluidPage(
    useShinyjs(),     ## Call to use shinyJS
    leafletOutput("mymap",width="100%",height="750px"), 

    #conditionalPanel(

        #condition = "output.COND === '2'",
    hidden( 
        div(id = "conditionalPanel",
            fluidRow(

                absolutePanel(id = "cond_panel", 
                            class = "panel panel-default", 
                            fixed = TRUE,
                            draggable = TRUE, 
                            top = "auto", 
                            left = 200, 
                            right = "auto", 
                            bottom = 0,
                            width = 400, 
                            height = 400,
                            fluidRow(

                    ) # e. of fluidRow(

                ) # # e. of absolutePanel

            ) # e. of fluidRow
        )
    )

#   ) # e. of conditionalPanel

) # e. of fluidPage


server <- function(input, output){

    rv <- reactiveValues()
    rv$myDf <- NULL
    rv$cond <- NULL

    cities <- read.csv(textConnection("
                        City,Lat,Long,Pop
                        Boston,42.3601,-71.0589,645966
                        Hartford,41.7627,-72.6743,125017
                        New York City,40.7127,-74.0059,8406000
                        Philadelphia,39.9500,-75.1667,1553000
                        Pittsburgh,40.4397,-79.9764,305841
                        Providence,41.8236,-71.4222,177994
                        "))
    cities$id <- 1:nrow(cities) 

    output$mymap <- renderLeaflet({
        leaflet(cities) %>% addTiles() %>%
            addCircles(lng = ~Long, lat = ~Lat, weight = 1,
                                 radius = ~sqrt(Pop) * 30, popup = ~City, layerId = ~id)
    })

    observeEvent(input$mymap_click, {

        shinyjs::hide(id = "conditionalPanel")
        print("map clicked")
        rv$cond <- "1"
        print(paste("Value rv$cond = ", rv$cond))
        output$COND <- reactive({rv$cond})
        leafletProxy("mymap")

    }) # e. of observeEvent

    observeEvent(input$mymap_shape_click, {

        shinyjs::show(id = "conditionalPanel")
        print("shape clicked")
        rv$cond <- "2"
        print(paste("Value rv$cond = ", rv$cond))
        output$COND <- reactive({rv$cond})
        leafletProxy("mymap")

    }) # e. of observeEvent

} # e. of server

shinyApp(ui, server)

答案 1 :(得分:0)

在conditionPanel中使用absolutePanel,根据用户输入重置其条件。