[英]knitr HTML output too large
I have been using rmarkdown
/ knitr
's knit to html
capability to generate html code for some blogs. 我一直在使用
rmarkdown
/ knitr
knit to html
功能来为一些博客生成HTML代码。 I've found it extremely helpful and convenient, but have been running into some problems lately with file size. 我发现它非常有用和方便,但最近因文件大小而遇到了一些问题。
When I knit a script that has graphics that use shapefiles or ggmap
images, the html file gets too big for the blog host to make sense of it (I've tried with both blogger and wordpress). 当我编写一个包含使用shapefile或
ggmap
图像的图形的脚本时,html文件太大,博客主机无法理解它(我已经尝试过使用blogger和wordpress)。 I believe this has to do with the relatively large data.frames/files that are the shapefiles/ ggmap
being put into html form. 我相信这与相对较大的data.frames /文件有关,这些文件是shapefiles /
ggmap
放入html格式。 Is there anything I can do to get a smaller html file that can be parsed by a blog host? 我有什么办法可以获得一个可以被博客主机解析的小型html文件吗?
For reference, the html output from an rmarkdown script with one graphic using a ggmap
layer, a layer of shapefiles and some data is 1.90MB, which is too big for blogger or wordpress to handle in html input. 作为参考,来自rmarkdown脚本的html输出,其中一个图形使用
ggmap
图层,一层shapefile和一些数据是1.90MB,这对于博客或wordpress在html输入中处理来说太大了。 Thanks for any ideas. 谢谢你的任何想法。
Below are 3 different options to help you reduce the file size of HTML files with encoded images. 下面是3个不同的选项,可帮助您减少带有编码图像的HTML文件的文件大小。
You can run this Python script on an existing HTML file. 您可以在现有HTML文件上运行此Python脚本 。 The script will:
该脚本将:
Usage: 用法:
python optimize_html.py infile.html
It writes output to infile-optimized.html
. 它将输出写入
infile-optimized.html
。
knitr 1.15 includes a hook called hook_optipng
that will run the optipng program on generated PNG files to reduce file size. knitr 1.15包含一个名为
hook_optipng
的钩子,它将在生成的PNG文件上运行optipng程序以减小文件大小。
Here is a .Rmd
example (taken from: knitr-examples/035-optipng.Rmd ): 这是一个
.Rmd
示例(取自: knitr-examples / 035-optipng.Rmd ):
# 035-optipng.Rmd
This demo shows you how to optimize PNG images with `optipng`.
```{r setup}
library(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=''}
library(methods)
library(ggplot2)
set.seed(123)
qplot(rnorm(1e3), rnorm(1e3))
```
Writing your own hook is also quite easy, so I wrote a hook that calls the pngquant program. 编写自己的钩子也很容易,所以我编写了一个调用pngquant程序的钩子。 I find that
pngquant
runs faster, and the output files are smaller and look better. 我发现
pngquant
运行得更快,输出文件更小,看起来更好。
Here is a .R
example that defines and uses hook_pngquant
(taken from this gist ). 这是一个
.R
示例,它定义并使用hook_pngquant
(取自这个要点 )。
#' ---
#' 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))
I prefer to write my reports in R scripts ( .R
) instead of R markdown documents ( .Rmd
). 我更喜欢用R脚本(
.R
)而不是R markdown文档( .Rmd
)编写报表。 See http://yihui.name/knitr/demo/stitch/ for more information on how to do that. 有关如何执行此操作的详细信息,请参见http://yihui.name/knitr/demo/stitch/ 。
One thing you could do would be to not use embedded image and other resources. 您可以做的一件事是不使用嵌入式图像和其他资源。 To achieve this, you can set the
self_contained
option in the YAML header for your document to false
, eg: 要实现此目的,您可以将文档的YAML标题中的
self_contained
选项设置为false
,例如:
---
output:
html_document:
self_contained: false
---
More info here: http://rmarkdown.rstudio.com/html_document_format.html 更多信息: http : //rmarkdown.rstudio.com/html_document_format.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.