2017-06-02 8 views
1

をもとにナビゲーションバーのタブ私は私が望む何を以下の再現性のあるコードR光沢のある条件以前のタブ条件

Poly = data.frame(Strat = c("A","A","A","A","A","B","B","B","B","B"), long = c(174.5012, 174.5026, 174.5026, 174.5014,174.5012,174.5012 ,174.5020, 174.5020,174.5012,174.5012),lat = c(-35.84014, -35.84018, -35.84137,-35.84138,-35.84014,-35.84014,-35.84014,-35.84197,-35.84197,-35.84014)) 
Points = data.frame(long = c(174.5014 ,174.5017, 174.5021, 174.5023, 174.5020, 174.5017 ,174.5021 ,174.5017, 174.5021, 174.5019), lat = c(-35.84187, -35.84165, -35.84220 ,-35.84121, -35.84133, -35.84034, -35.84082, -35.84101, -35.84112, -35.84084)) 

library('leaflet') 
library('shiny') 


##### My take on Example 2 
ui <- navbarPage(title = "navigation bar", 
tabPanel("Home", fluidPage(bootstrapPage(
checkboxInput("check_box", label = "Click me to continue", FALSE), 
## Main text 
mainPanel(
tags$div() 
) 

))), 
tabPanel("View Data", 
    bootstrapPage(
    mainPanel(
    ), 
    leafletOutput("map", width ="100%", height = "600px") 
    ) 
) 
) 


server = function(input, output){ 

mymap <- reactive({ 
    leaflet() %>% addTiles(urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", attribution = NULL, layerId = NULL, group = NULL, options = tileOptions()) %>% 
    clearShapes() %>% 
    clearMarkers() %>%  
    fitBounds(lng1 = 174.5042, lat1= -35.83814,lng2= 174.5001, lat2 = -35.8424) 
}) 

output$map <- renderLeaflet({ 
    mymap()  
}) 
myfun <- function(map) { 
    print("adding points") 
    map %>% clearShapes() %>% 
    clearControls() %>% 
    clearMarkers() %>% 
    addCircles(lng = Points$long, lat = Points$lat, color = "blue",fillOpacity = 1,radius = 1) 
} 

AddStrataPoly <- function(map) { 
    print("adding polygons")  
    for(i in 1:length(unique(Poly$Strat))) { 
    map <- map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1)) 
    } 
    map 
} 

observe({ 
    leafletProxy("map") %>% myfun() %>% AddStrataPoly() 
}) 
} 

    shinyApp(ui, server); 

を持っているが、彼らが持っていない限り、ユーザーは、「データの表示」タブにクリックしないようにすることですチェックボックスをクリックしますか?タブは常に存在するのが理想的です。私は、タブフォントをグレーアウトして、この場合はチェックボックスの条件を満たしていなければ、ユーザーがクリックできないことを示します。

おかげ

+0

私が翻訳に苦労しています[この](https://stackoverflow.com/questions/25455154/navlistpanel-make-tabs-sequentially-active-in-shiny-app) – SBista

+0

@SBistaを見てください私の例でNavbarPageへのNavlistソリューション?あなたは助けてくれますか?それはリンクのためにthoのおかげです。 –

答えて

1

は私はJSとCSSの専門家ではないが、私は働く解決策を考え出すために管理しています。

##Data 
Poly = data.frame(Strat = c("A","A","A","A","A","B","B","B","B","B"), long = c(174.5012, 174.5026, 174.5026, 174.5014,174.5012,174.5012 ,174.5020, 174.5020,174.5012,174.5012),lat = c(-35.84014, -35.84018, -35.84137,-35.84138,-35.84014,-35.84014,-35.84014,-35.84197,-35.84197,-35.84014)) 
Points = data.frame(long = c(174.5014 ,174.5017, 174.5021, 174.5023, 174.5020, 174.5017 ,174.5021 ,174.5017, 174.5021, 174.5019), lat = c(-35.84187, -35.84165, -35.84220 ,-35.84121, -35.84133, -35.84034, -35.84082, -35.84101, -35.84112, -35.84084)) 

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


    ##JS Code for enabling and diabling 
    jscode <- "shinyjs.disabletab =function(name){ 
    $('ul li:has(a[data-value= \"Data\"])').addClass('disabled'); 

    } 

    shinyjs.enabletab =function(name){ 
    $('ul li:has(a[data-value= \"Data\"])').removeClass('disabled'); 
    } " 


    #UI 
    ui <- navbarPage(title = "navigation bar", 

        tabPanel("Home", fluidPage(bootstrapPage(
         checkboxInput("check_box", label = "Click me to continue", FALSE), 
         ## Main text 
         mainPanel(
         tags$div() 
         ) 

        ))), 

        tabPanel(title = "View Data", 
           value = "Data", 
           bootstrapPage(
           mainPanel(
           ), 
           leafletOutput("map", width ="100%", height = "600px") 
          ) 
        ), 

        #To use js code in the app 
        useShinyjs(), 
        extendShinyjs(text = jscode) 
    ) 


    server = function(input, output, session){ 

     mymap <- reactive({ 
     leaflet() %>% addTiles(urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", attribution = NULL, layerId = NULL, group = NULL, options = tileOptions()) %>% 
      clearShapes() %>% 
      clearMarkers() %>%  
      fitBounds(lng1 = 174.5042, lat1= -35.83814,lng2= 174.5001, lat2 = -35.8424) 
     }) 

     output$map <- renderLeaflet({ 
     mymap()  
     }) 
     myfun <- function(map) { 
     print("adding points") 
     map %>% clearShapes() %>% 
      clearControls() %>% 
      clearMarkers() %>% 
      addCircles(lng = Points$long, lat = Points$lat, color = "blue",fillOpacity = 1,radius = 1) 
     } 

     AddStrataPoly <- function(map) { 
     print("adding polygons")  
     for(i in 1:length(unique(Poly$Strat))) { 
      map <- map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1)) 
     } 
     map 
     } 

     observe({ 
     leafletProxy("map") %>% myfun() %>% AddStrataPoly() 
     }) 


     observeEvent(input$check_box,{ 

     if(input$check_box){#If true enable, else disable 
      js$enabletab("abc") 
     }else{ 
      js$disabletab("abc") 
     } 

     }) 

    } 

    shinyApp(ui, server) 

希望すると助かります!

[編集]: 私は、この質問に受け入れられた回答がありますが、後で誰かのために役立つように回答を編集していることを知っています。

私の回答を投稿しているときに、navbarが無効になっていてもclickイベントが存在することを認識していませんでした。

JS上記のコードをクリックイベント以下のものに交換されている場合は削除され、期待通りに解決策が機能:

##JS Code for enabling and diabling 
jscode <- "shinyjs.disabletab =function(name){ 
$('ul li:has(a[data-value= \"Data\"])').addClass('disabled'); 
$('.nav li.disabled a').prop('disabled',true) 
} 

shinyjs.enabletab =function(name){ 
$('.nav li.disabled a').prop('disabled',false) 
$('ul li:has(a[data-value= \"Data\"])').removeClass('disabled'); 
} " 
+0

あなたの編集をありがとう。 –

1

確かに、以下のコードはまだ「データの表示」にクリックするユーザーをすることができますタブ、しかし:「CHECK_BOXは」

  • が自動的にナビゲート空のとき

    1. は、このタブの内容を隠蔽する「データの表示」「CHECK_BOX」を選択したタブ

    多分それで十分でしょう。

    jsまたはcssが追加されていません。

    Poly = data.frame(Strat = c("A","A","A","A","A","B","B","B","B","B"), long = c(174.5012, 174.5026, 174.5026, 174.5014,174.5012,174.5012 ,174.5020, 174.5020,174.5012,174.5012),lat = c(-35.84014, -35.84018, -35.84137,-35.84138,-35.84014,-35.84014,-35.84014,-35.84197,-35.84197,-35.84014)) 
    Points = data.frame(long = c(174.5014 ,174.5017, 174.5021, 174.5023, 174.5020, 174.5017 ,174.5021 ,174.5017, 174.5021, 174.5019), lat = c(-35.84187, -35.84165, -35.84220 ,-35.84121, -35.84133, -35.84034, -35.84082, -35.84101, -35.84112, -35.84084)) 
    
    library('leaflet') 
    library('shiny') 
    
    
    ##### My take on Example 2 
    ## the "id" needs to be added to navbarPage arguments 
    ui <- navbarPage(title = "navigation bar", id = "navigation", selected = "Home", 
           tabPanel("Home", fluidPage(bootstrapPage(
            checkboxInput("check_box", label = "Click me to continue", FALSE), 
            ## Main text 
            mainPanel(
            tags$div() 
            )   
           ))), 
           tabPanel("View Data", 
    
              ## the content of "View Data" tabPanel is wrapped into conditionalPanel 
              ## what hides the map until "check_box" is marked 
              conditionalPanel(condition = "input.check_box == 1", 
                  bootstrapPage(
                  mainPanel(), 
                  leafletOutput("map", width ="100%", height = "600px") 
                  ) 
             ) 
           ) 
    ) 
    
    # argument "session" needs to be added 
    server = function(session, input, output){ 
    
        mymap <- reactive({ 
        leaflet() %>% addTiles(urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", attribution = NULL, layerId = NULL, group = NULL, options = tileOptions()) %>% 
         clearShapes() %>% 
         clearMarkers() %>%  
         fitBounds(lng1 = 174.5042, lat1= -35.83814,lng2= 174.5001, lat2 = -35.8424) 
        }) 
    
        output$map <- renderLeaflet({ 
        mymap()  
        }) 
        myfun <- function(map) { 
        print("adding points") 
        map %>% clearShapes() %>% 
         clearControls() %>% 
         clearMarkers() %>% 
         addCircles(lng = Points$long, lat = Points$lat, color = "blue",fillOpacity = 1,radius = 1) 
        } 
    
        # the observer below navigates automatically to "View Data" when "check_box" is selected 
        observe({ 
        if(input$check_box) 
         updateTabsetPanel(session, inputId = "navigation", selected = "View Data") 
    
        }) 
    
        AddStrataPoly <- function(map) { 
        print("adding polygons")  
        for(i in 1:length(unique(Poly$Strat))) { 
         map <- map %>% addPolygons(lng = Poly[Poly$Strat == unique(Poly$Strat)[i],]$long, lat = Poly[Poly$Strat == unique(Poly$Strat)[i],]$lat, layerId = unique(Poly$Strat)[i], color = 'gray60', options = list(fillOpacity = 0.1)) 
        } 
        map 
        } 
    
        observe({ 
        leafletProxy("map") %>% myfun() %>% AddStrataPoly() 
        }) 
    } 
    
    
    
    shinyApp(ui, server) 
    
  • +0

    以前の回答ではタブをクリックしてそのタブに移動できるので、これが答えです。 –

    +2

    @ Cyrillm_44は、あなたのコメントを見た後でそれを実現しました。私の答えを編集して、後でここで遭遇する他の人にとって役に立つかもしれない。 – SBista

    関連する問題