Set page width in Knitr for md or HTML output

2020-07-09 02:52发布

问题:

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)
```