2016-04-08 7 views
3

ユーザーがリーフレットマップを変更できるアプリケーションを作成しました。このマップをpdfレポートで使用したいと考えています。私は 1.インストールされたパッケージリーフレット、webshotを持っているとリーフレットマップをRで保存する

以下 2.インストールPhantomJSをhtmlwidgetコード

server.Rの簡易版である:

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

    shinyServer(function(input, output, session) { 

     output$amap <- renderLeaflet({ 
     leaflet() %>% 
     addProviderTiles("Stamen.Toner", 
        options = providerTileOptions(noWrap = TRUE,  reuseTiles=TRUE)) 
    }) 

    observe({ 
    leafletProxy("amap") %>% 
    clearShapes() %>% 
    addCircles(lng = c(22,-2), lat = c(42,65)) 
    }) 



    observeEvent(input$saveButton,{ 
    themap<- leafletProxy("amap") 
    saveWidget(themap, file="temp.html", selfcontained = F) 
    webshot("temp.html", file = "Rplot.png", 
      cliprect = "viewport") 

    }) 
}) 

ui.R:

fluidPage(
    leafletOutput("amap", height="600px", width="600px"), 
    br(), 
    actionButton("saveButton", "Save") 
) 

このエラーメッセージが表示されます。

警告:system.fileにおけるエラー: 73:72 system.file:readLines 71:yaml.load 69:YAML 70を貼り付ける 'パッケージ' は長さ1つの スタックトレース(最も内側の第一)でなければなりません:: yaml.load_file 68:getDependency 67:widget_dependencies 66:htmltools :: attachDependencies 65:toHTML 64:saveWidget 63:observeEventHandler [C:\ Rファイル\#24テスト/ server.R] 1: shiny :: runApp

保存ボタンが有効になっているとき。

observeEvent(input$saveButton,{ 
    themap<-leaflet() %>% 
     addProviderTiles("Stamen.Toner", 
         options = providerTileOptions(noWrap = TRUE, reuseTiles=TRUE)) 

    saveWidget(themap, file="temp.html", selfcontained = F) 
    webshot("temp.html", file = "Rplot.png", 
      cliprect = "viewport") 

    }) 

しかし、私が本当にユーザーがwebshotで行う変更をしたい:私はこのような保存ボタンを定義するときに正常に動作しますsavewidget 誰も助けることができますか?

+0

ファイルを保存できるようにしますか?私はこれがJavaScript側でうまく処理されると思う。私はあなたのための例を整えようとします。 – timelyportfolio

+0

まあ、私の華麗な解決策は、クロスオリジンの問題で絡み合ってしまいます。私は別の対処方法を考えようとします。 – timelyportfolio

答えて

3

これは完全ではありませんが、ここではライブラリhtml2canvasを使用したソリューションです。帰属、ライセンス、および著作権に注意してください。また、がRStudio Viewerでは動作しませんが、動作させる方法はあります。

library(leaflet) 
library(htmltools) 

lf <- leaflet() %>% 
    addProviderTiles(
    "Stamen.Toner", 
    options = providerTileOptions(
     noWrap = TRUE, 
     reuseTiles=TRUE 
    ) 
) 


# add the mapbox leaflet-image library 
# https://github.com/mapbox/leaflet-image 
#lf$dependencies[[length(lf$dependencies)+1]] <- htmlDependency(
# name = "leaflet-image", 
# version = "0.0.4", 
# src = c(href = "http://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-image/v0.0.4/"), 
# script = "leaflet-image.js" 
#) 

lf$dependencies[[length(lf$dependencies)+1]] <- htmlDependency(
    name = "html2canvas", 
    version = "0.5.0", 
    src = c(href="https://cdn.rawgit.com/niklasvh/html2canvas/master/dist/"), 
    script = "html2canvas.min.js" 
) 



browsable(
    tagList(
    tags$button("snapshot",id="snap"), 
    lf, 
    tags$script(
' 
document.getElementById("snap").addEventListener("click", function() { 
    var lf = document.querySelectorAll(".leaflet"); 
    html2canvas(lf, { 
    useCORS: true, 
    onrendered: function(canvas) { 
     var url = canvas.toDataURL("image/png"); 
     var downloadLink = document.createElement("a"); 
     downloadLink.href = url; 
     downloadLink.download = "map.png" 

     document.body.appendChild(downloadLink); 
     downloadLink.click(); 
     document.body.removeChild(downloadLink); 
    } 
    }); 
}); 
'  
    ) 
) 
) 
+0

早く返信いただきありがとうございます。私はそれがまだ私の光沢のあるアプリの中で動作させていない。私はjavascriptであまりにも強くない.....今週末に戻ってくる必要があります。これまでの仕事をありがとう –

関連する問題