Set page width in Knitr for md or HTML output

倖福魔咒の 提交于 2019-12-09 16:33:42

问题


I am having knitr to create an output of my statistical analysis along with figures. My analysis has a number of levels that are marked by headers. To get the nice html page with table of contents on the side I use "pander" (pandoc R package) to convert my .md file to html because knitr does not embed table of contents in the html file.

The problem: When I use pander it creates a fixed width page (quite narrow) where my large figures need to be scrolled left and right. Is there any way to resize either .md page width or direct pander to output a page with auto-width settings (adjusting to a any screen width).

I did spend time looking for a solution: either have knitr aromatically embed TOC, embed width parameter into the r code

```{r set-options, echo=FALSE, cache=FALSE}
options(width=600)
opts_chunk$set(comment = "", warning = FALSE, message = FALSE, echo = TRUE, tidy = FALSE,       size="small")

```

or adjust pader output paramaters but did not have any luck.

If anyone has a solution to the problem I would really appreciate it.


回答1:


knitr does not take care of markdown rendering directly, and you can compile *.md to *.html through the markdown package, for which knitr has a wrapper function knit2html(). To get the table of contents, you can add the toc option to markdown::markdownToHTML(), e.g.

library(knitr)
knit2html('foo.Rmd', options = c('toc', markdown::markdownHTMLOptions(TRUE)))



回答2:


To avoid scrolling, You can use out.width and out.height to fix width and height of the plot in the final output file.

```{r fig.width=7, fig.height=6,out.width=250,out.height=400}
plot(cars)
```


来源:https://stackoverflow.com/questions/14662626/set-page-width-in-knitr-for-md-or-html-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!