我一直在使用rmarkdown/knitr的knit to html功能为一些博客生成html代码,我发现它非常有用和方便,但最近遇到了一些文件大小的问题。
当我编写一个包含使用shapefile或ggmap图像的图形的脚本时,html文件变得太大,博客主机无法理解它(我已经尝试过blogger和wordpress)。我相信这与相对较大的数据帧/文件有关,这些数据帧/文件是shapefile/ggmap放入html格式。有没有什么我可以做的,以获得一个较小的html文件,可以由博客主机解析?
作为参考,使用ggmap层、shapefiles层和一些数据的rmarkdown脚本的html输出是1.90MB,这对于blogger或wordpress来说太大了,无法处理html输入。谢谢你的点子。
发布于 2016-11-16 03:11:58
下面是3个不同的选项,可帮助您减小包含编码图像的HTML文件的文件大小。
1.优化现有的HTML文件
您可以在现有的超文本标记语言文件上运行this Python script。该脚本将:
用法:
python optimize_html.py infile.html它将输出写入infile-optimized.html。
2.使用内置的编织钩子优化PNG图像
PNG1.15包括一个名为hook_optipng的钩子,它将在生成的knitr文件上运行optipng程序以减小文件大小。
下面是一个.Rmd示例(取自:knitr-examples/035-optipng.Rmd):
# 035-optipng.Rmd
This demo shows you how to optimize PNG images with `optipng`.
```{r setup}库(Knitr)
knit_hooks$set(optipng = hook_optipng)
Now we set the chunk option `optipng` to a non-`NULL` value,
e.g. `optipng=''`, to activate the hook. This string is passed to
`optipng`, so you can use `optipng='-o7'` to optimize more heavily.
```{r use-optipng, optipng=''}库(方法)
库(Ggplot2)
set.seed(123)
qplot(rnorm(1e3),rnorm(1e3))
3.为任何图像优化器编写你自己的编织钩子
Writing your own hook也很简单,所以我编写了一个调用pngquant程序的钩子。我发现pngquant运行得更快,输出的文件更小,看起来也更好。
下面是一个定义和使用hook_pngquant (取自this gist)的.R示例。
#' ---
#' title: "pngquant demo"
#' author: "Kamil Slowikowski"
#' date: "`r Sys.Date()`"
#' output:
#' html_document:
#' self_contained: true
#' ---
#+ setup, include=FALSE
library(knitr)
# Functions taken from knitr/R/utils.R
all_figs = function(options, ext = options$fig.ext, num = options$fig.num) {
fig_path(ext, options, number = seq_len(num))
}
in_dir = function(dir, expr) {
if (!is.null(dir)) {
owd = setwd(dir); on.exit(setwd(owd))
}
wd1 = getwd()
res = expr
wd2 = getwd()
if (wd1 != wd2) warning(
'You changed the working directory to ', wd2, ' (probably via setwd()). ',
'It will be restored to ', wd1, '. See the Note section in ?knitr::knit'
)
res
}
is_windows = function() .Platform$OS.type == 'windows'
in_base_dir = function(expr) {
d = opts_knit$get('base.dir')
if (is.character(d) && !file_test('-d', d)) dir.create(d, recursive = TRUE)
in_dir(d, expr)
}
# Here is the code you can modify to use any image optimizer.
hook_pngquant <- function(before, options, envir) {
if (before)
return()
ext = tolower(options$fig.ext)
if (ext != "png") {
warning("this hook only works with PNG")
return()
}
if (!nzchar(Sys.which("pngquant"))) {
warning("cannot find pngquant; please install and put it in PATH")
return()
}
paths = all_figs(options, ext)
in_base_dir(lapply(paths, function(x) {
message("optimizing ", x)
cmd = paste(
"pngquant",
if (is.character(options$pngquant)) options$pngquant,
shQuote(x)
)
message(cmd)
(if (is_windows())
shell
else system)(cmd)
x_opt = sub("\\.png$", "-fs8.png", x)
file.rename(x_opt, x)
}))
return()
}
# Enable this hook in this R script.
knit_hooks$set(
pngquant = hook_pngquant
)
#' Here we set the chunk option `pngquant='--speed=1 --quality=0-50'`,
#' which activates the hook.
#+ use-pngquant, pngquant='--speed=1 --quality=0-50'
library(methods)
library(ggplot2)
set.seed(123)
qplot(rnorm(1e3), rnorm(1e3))与R标记文档(.Rmd)相比,我更喜欢用R脚本(.R)编写报告。有关如何做到这一点的更多信息,请参见http://yihui.name/knitr/demo/stitch/。
发布于 2016-08-08 02:51:59
您可以做的一件事是不使用嵌入式图像和其他资源。为此,您可以将文档的YAML头中的self_contained选项设置为false,例如:
---
output:
html_document:
self_contained: false
---更多信息请点击此处:http://rmarkdown.rstudio.com/html_document_format.html
https://stackoverflow.com/questions/38815784
复制相似问题