webshotのインストール:
# CRANから
install.packages("webshot")
# GithHub版
devtools::install_github("wch/webshot")
PATH
を通しておくwebshot::install_phantomjs()
library(webshot)
url <- "https://kazutan.github.io/kazutanR"
webshot(url, file = "pics/webshot_demo_01.png", delay = 0.5)
webshot()
関数についてwebshot(url = NULL, file = "webshot.png", vwidth = 992, vheight = 744,
cliprect = NULL, selector = NULL, expand = NULL, delay = 0.2,
zoom = 1, eval = NULL)
cliprect
を利用cliprect = "viewport"
と指定すると,vwidth
とvheight
で指定したウィンドウサイズでスクリーンショットを撮影します:
url2 <- "http://www.kyoritsu-pub.co.jp/bookdetail/9784320113169"
webshot(url2, file = "pics/webshot_demo_02.png",
cliprect = "viewport",
vwidth = 800,
vheight = 600)
また,cliprect
を具体的な値で指定も可能:
webshot(url2, file = "pics/webshot_demo_03.png",
cliprect = c(200, 100, 300, 150))
selector
を利用セレクタを指定してみます:
webshot(url2, file = "pics/webshot_demo_04.png",
selector = "article#bookDetailMain")
resize()
関数
?resize
を参照shrink()
関数
?shrink
を参照library(dplyr)
library(leaflet)
library(htmlwidgets)
m <- leaflet() %>% addTiles() %>%
addPopups(135, 35, "Popup!")
saveWidget(m, file = "leaflet2png.html")
webshot("leaflet2png.html",
file = "pics/webshot_demo_05.png",
delay = 2)
htmltoolsを応用してrevealjs用にサムネイル作成:
library(htmltools)
thumbnail <- function(title, img, href, caption = TRUE) {
tags$div(class = "column1",
tags$a(class = "thumbnail", title = title, href = href,
tags$img(src = img),
tags$div(class = ifelse(caption, "caption", ""),
ifelse(caption, title, "")
)
)
)
}
title <- c("原典", "日本語版")
urls <- c("https://sites.google.com/site/doingbayesiandataanalysis/",
"http://www.kyoritsu-pub.co.jp/bookdetail/9784320113169")
imgs <- webshot(urls, file = "pics/thumb.png", cliprect = "viewport", delay = 1)
thumbs <- mapply(thumbnail, title = title, img = imgs, href = urls,
SIMPLIFY = FALSE, USE.NAMES = FALSE)
tags$div(class = "row", thumbs)
実行するとこのような感じになります:
(cssは独自で調整済みです)
以下の情報を参照してください。