motsu's blog

大学生のブログ, 主に統計とか 数学とか 諸々の話題

Rのleafletで作成した地図をpngファイルにする際のエラー対処

状況

すでにleafletでmという変数にデータを入れている状況で、そのmをhtmlファイルではなく、pngファイルとして、画像として出力したい状況。

エラー

PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.

解決策

webshot::install_phantomjs()

を実行する。

コード

library(htmltools)
library(magrittr)
library(leaflet)
library(mapview)
webshot::install_phantomjs()

mapview::mapshot(m, file = paste0(getwd(), 
    "/tokyo_DID.png"))

参考

PマップまたはjpgファイルとしてRマップにLeafletを保存する方法? - コードログ
leafletで描画した地図を画像ファイルとして保存する - cucumber flesh
How to save Leaflet in R map as png or jpg file? - Stack Overflow