rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
809 stars 507 forks source link

add EasyPrint #581

Open mhaditama26 opened 6 years ago

mhaditama26 commented 6 years ago

How to add function like EasyPrint like on this link http://rowanwins.github.io/leaflet-easyPrint/ , but embed on icon that we create? Is it possible to embed R Markdown or KnitR script on icon that also we create?

Please answer my question..

Regards, MH.Aditama

mhaditama26 commented 6 years ago

or somethink like this link https://github.com/Igor-Vladyka/leaflet.browser.print

Regards, MH.Aditama

mhaditama26 commented 6 years ago

I've try some basic javascript to print leaflet web page. unfortunately the result still has some missing icon.

The script is : <a href = "javascript:window.print()"><img src = ".\Printer_Leaflet.png" alt = "print this page" id="print-button" class="printer" /> </a>

The missing icon is like : result

ignore the icon print (still trial and error) from the icon i put some action from javascript to print the webpage.

hopefully any one could give the answer.

Regards, MH.Aditama

ragapack commented 5 years ago

For me EasyPrint works fine with my Shiny app.

Add to the ui.R

jsfile <- "https://rawgit.com/rowanwins/leaflet-easyPrint/gh-pages/dist/bundle.js" 

shinyUI(fluidPage(),

  tags$head(tags$script(src = jsfile)),

And to the server.R

output$map <- renderLeaflet({

    leaflet() %>% 

...........
 onRender(
      "function(el, x) {
            L.easyPrint({
              sizeModes: ['Current', 'A4Landscape', 'A4Portrait'],
              filename: 'ipcmap',
              exportOnly: true,
              hideControlContainer: false
            }).addTo(this);
            }"
    )

Works with Firefox and Chrome, not with Edge

mhaditama26 commented 5 years ago

your script like work , but i cant find button to execute your script. help me please :)

is that my script correct ?

on ui jsfile <- "https://rawgit.com/rowanwins/leaflet-easyPrint/gh-pages/dist/bundle.js" ui <- fluidPage(tags$head(tags$script(src = jsfile)),'

and on server

server <- function(input, output,session) { output$Map_recl <- renderLeaflet({ leaflet() %>% ........%>% onRender("function(el, x) { L.easyPrint({ sizeModes: ['Current', 'A4Landscape', 'A4Portrait'], filename: 'ipcmap', exportOnly: true, hideControlContainer: false }).addTo(this);}") }

ragapack commented 5 years ago

Have a look to this page, here is a working example: https://stackoverflow.com/questions/47343316/shiny-leaflet-easyprint-plugin

 library(leaflet)
  library(shiny)
  library(htmlwidgets)

  jsfile <- "https://rawgit.com/rowanwins/leaflet-easyPrint/gh-pages/dist/bundle.js" 
  ui <- fluidPage(
    tags$head(tags$script(src = jsfile)),
    leafletOutput("map")
  )

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

    output$map <- renderLeaflet({
      leaflet() %>% 
        addProviderTiles("OpenStreetMap.Mapnik") %>%
        setView(-122.23, 37.75, zoom = 10) %>%
        onRender(
          "function(el, x) {
            L.easyPrint({
              sizeModes: ['Current', 'A4Landscape', 'A4Portrait'],
              filename: 'mymap',
              exportOnly: true,
              hideControlContainer: true
            }).addTo(this);
            }"
        )
      })

    }

  shinyApp(ui, server)
mhaditama26 commented 5 years ago

thanks @ragapack its work.. i haven't yet open the shinyApp on chrome...

but i have any question.. i just modified some script to input X,Y coordinate from [https://rstudio.github.io/leaflet/shiny.html] and the button like not work anymore...

here it is my script

library(shiny) library(leaflet) library(htmlwidgets)

jsfile <- "https://rawgit.com/rowanwins/leaflet-easyPrint/gh-pages/dist/bundle.js"

ui <- fluidPage( tags$head(tags$script(src = jsfile)), textInput("X","Longitude",116.94784), textInput("Y", "Latitude",-8.86888), actionButton("recalc","New Point"), leafletOutput("Map_recl",height = 525)

)

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

point = eventReactive(input$recalc,{ cbind(as.numeric(input$X), as.numeric(input$Y)) },ignoreNULL = FALSE)

output$Map_recl <- renderLeaflet({ leaflet() %>% addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery") %>% setView(lng = 116.98321, lat = -8.86473, zoom = 14)%>% addMarkers(data = point())%>% onRender( "function(el, x) { L.easyPrint({ sizeModes: ['Current', 'A4Landscape', 'A4Portrait'], filename: 'mymap', exportOnly: true, hideControlContainer: true }).addTo(this); }" )

})

}

shinyApp(ui = ui, server = server)

sorry for not writing script properly...

trafficonese commented 4 years ago

The easyPrint plugin is now wrapped in leaflet.extras2. You can see a working example here.