2017-01-19 24 views
1

我想在地圖上顯示一個條件面板,當我點擊一個圓時,如果我在圓外點擊,但這個條件面板必須消失,但它不會出現,我不知道爲什麼。R小冊子:有條件的面板不會出現在地圖中

我認爲這是關於被動值(再一次)。

如果有任何想法,請告訴我。

非常感謝你,這是一個可重複的例子(感謝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

我向你求婚一個稍微不同的方法,使用library(shinyjs)來使用javascript來控制面板是否隱藏。

在這個例子中我已經創建了一個隱藏的div元件(即,面板將應用程序打開時啓動的隱藏)。然後,當點擊該圓圈時顯示'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) 
+0

你是最好的SymbolixAU內,如果沒有你的幫助,我永遠都不會發現如何去做,這是你從昨天開始第三次幫助我,謝謝你,祝你有個美好的一天 –

+0

@Mickey_NC - 沒問題!隨時接受/'tick '如果它解決了你的問題的答案! – SymbolixAU

0

只需使用absolutePanel一個conditionalPanel其條件重置基於用戶的輸入。