Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set page width in Knitr for md or HTML output

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.

like image 704
user16890 Avatar asked Feb 02 '13 13:02

user16890


People also ask

How do I change the output size in R Markdown?

To change the output size you can use the corresponding LaTeX commands, set just before the code junk. The smallest option would be \tiny . For a full overview consider e.g. this. After the code junk it's important to set back to the size you used before, e.g. \normalsize .

How do I render RMD in HTML?

Rendering. To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

What does knitr :: Opts_chunk set echo true mean?

The first code chunk: ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` is used to specify any global settings to be applied to the R Markdown script. The example sets all code chunks as “echo=TRUE”, meaning they will be included in the final rendered version.


2 Answers

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)))
like image 95
Yihui Xie Avatar answered Sep 28 '22 06:09

Yihui Xie


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)
```
like image 20
agstudy Avatar answered Sep 28 '22 05:09

agstudy