Adding variable to hyperlink reference in leaflet popup for Shiny app

847 views
Skip to first unread message

Kim Sanders

unread,
Feb 18, 2016, 9:36:06 AM2/18/16
to Shiny - Web Framework for R

I am working on a shiny app that incorporates a leaflet map with marker popups. Within the popup, I want to be able to provide a hyperlink based on a variable. I am able to establish a functioning hyperlink, however, it is not specific to the marker. Below is a sample of the code I am working with:



shinyServer(function(input, output) {
  
  myData<-read_csv(...insert csv file here) 
  myData<-data.frame(myData)

output$myMap1 <- renderLeaflet({
    
 
    pal <- colorFactor(c("#ad1d28", "#800080", "green", "blue", "navy"), domain = c("3", "4", "5", "6", "8"))
   
    map1<-leaflet(data = myData[1:nrow(myData),])  %>% setView(lng = -86.7, lat = 32.7, zoom = 7) %>% addTiles() %>%
      addPolygons(data=finalHex,fillColor="transparent",weight=1) %>%
      addCircleMarkers(~LONGITUDE, ~LATITUDE, radius = 6,color=~pal(Variable1),stroke=FALSE,fillOpacity=0.75,
                                                        popup = ~paste0("Add some text here",Variable2,'<BR>',"Add some text here",
                                                        Variable3, '<BR>',"Add some text here",
                                                        Variable4,"%",'<BR>',"Add some text here",
                                                        Variable5,'<BR>',
                                                        a("View Input XML",target="_blank",href=paste0(Variable2,"Input.txt")),'<BR>'))
     
   
    map1
  })

})



This is the warning message received:

Warning in if (!is.na(attribValue)) { :
  the condition has length > 1 and only the first element will be used
Warning in charToRaw(text) :
  argument should be a character vector of length 1
all but the first element will be ignored




Thanks!
Message has been deleted

MySchizo Buddy

unread,
Feb 19, 2016, 6:57:50 AM2/19/16
to Shiny - Web Framework for R
I have used a select button inside the popup. Hope this will help

library(shiny)
library
(leaflet)

df
<- data.frame("id" = c("1", "2"),
                 
"lng" = c(-93.65, -93.655),
                 
"lat" = c(42.0285, 42.03),
                 
"Text" = c("Department of Statistics", "something else"))


ui
<- fluidPage(
  leafletOutput
("map"),
  textOutput
("locationid1"),
  textOutput
("locationid2")
)

server
<- function(input, output, session) {

  output$map
<- renderLeaflet({
    df
%>% leaflet() %>%
      addProviderTiles
("CartoDB.Positron") %>%
      setView
(-93.65, 42.0285, zoom = 15) %>%
      addMarkers
(layerId = ~id, popup = ~paste("<b>", Text, "</b></br>", actionButton("selectlocation", "Select this Location", onclick = 'Shiny.onInputChange(\"button_click\",  Math.random())')))
 
})


  id1
<- reactive({
     validate
(
       need
(!is.null(input$map_marker_click), "Please select a location from the map above")
     
)
    input$map_marker_click$id
 
})

  id2
<- eventReactive(input$button_click, {
    input$map_marker_click$id
 
})


  output$locationid1
<- renderText({paste("Location Selected using marker click:", id1())})
  output$locationid2
<- renderText({paste("Location Selected using popup select button click:", id2())})

}

shinyApp
(ui, server)

Reply all
Reply to author
Forward
0 new messages