Re: [knitr] long chunk output lines truncated in markdown -> pdf document

12,582 views
Skip to first unread message

Yihui Xie

unread,
Jan 9, 2013, 4:01:27 PM1/9/13
to Dan Tenenbaum, kn...@googlegroups.com
This is a such frequently asked question...

The reason is that the output is written in the verbatim environment,
so there is no way to break or wrap it, unless you modify the output
or the environment. In your case, it is harder to modify the
environment, so perhaps you should modify the output.

I have cooked up one example here:

https://github.com/yihui/knitr-examples/blob/master/077-wrap-output.Rmd
https://github.com/yihui/knitr-examples/blob/master/077-wrap-output.md

In RStudio you can see the line wrapped because the <pre> tag was
defined in CSS to wrap the lines:

pre {
margin-top: 0;
max-width: 95%;
border: 1px solid #ccc;
white-space: pre-wrap;
}

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Wed, Jan 9, 2013 at 2:11 PM, Dan Tenenbaum <dand...@gmail.com> wrote:
> Hi,
>
> I am writing a package vignette in an .Rmd file and using knitr and pandoc
> to convert it to PDF.
>
> One of my code chunks produces long lines, ~106 columns. Here is a contrived
> reproduction:
>
> ```{r long_output}
> a <- '## "stx2A; shiga-like toxin II A subunit encoded by bacteriophage
> BP-933W; K11006 shiga toxin subunit A" '
> a
> ```
> When working in RStudio, if I click "Knit HTML", I see this output wrapped,
> and I can make the display window wider to see all of it on one line.
>
> But when I create a PDF from the markdown that knitr generates, the output
> does not wrap and it is cut off at about column 92, so there is no way to
> see the last columns of the line.
>
> I tried adding a width option to the beginning of the document:
>
> ```{r setup, echo=FALSE}
> options(width=80)
> ```
>
> That didn't make any difference. I'd like to cause code chunk output to wrap
> sensibly but at the very least I don't want it truncated in my pdf.
>
> In my Makefile, I don't pass any special options to pandoc. I tried
> --columns=80 and that made no difference. The pandoc docs say wrapping is on
> by default.
>
> In the .md file that knitr generates, the output of my document shows up
> like this:
>
> ```
> ## "stx2A; shiga-like toxin II A subunit encoded by bacteriophage BP-933W;
> K11006 shiga toxin subunit A"
> ```
>
> So I guess I'd like to know how to force that to wrap.
> Thanks,
> Dan
>

Dan Tenenbaum

unread,
Jan 9, 2013, 4:09:08 PM1/9/13
to kn...@googlegroups.com, Dan Tenenbaum


On Wednesday, January 9, 2013 1:01:27 PM UTC-8, Yihui Xie wrote:
This is a such frequently asked question...

The reason is that the output is written in the verbatim environment,
so there is no way to break or wrap it, unless you modify the output
or the environment. In your case, it is harder to modify the
environment, so perhaps you should modify the output.

I have cooked up one example here:

https://github.com/yihui/knitr-examples/blob/master/077-wrap-output.Rmd
https://github.com/yihui/knitr-examples/blob/master/077-wrap-output.md


Wonderful! It works a treat. Thanks for the great example.
Dan

Thiago Gonçalves Souza

unread,
Aug 9, 2013, 9:54:53 AM8/9/13
to kn...@googlegroups.com, Dan Tenenbaum
Dear Dan and Yihui,

I'm novice in knitr. I had the same problem with large text when compiling my pdf file.

For instance,

<<>>=
community<- data.frame(
sps = c("sp1", "sp2","sp3", "sp4"), 
habitat = factor(c("hab.a", "hab.b", "hab.c","hab.d")), height = c(1.1, 0.8, 0.9, 1), 
distance = c(1, 1.7, 0.6, 0.2))
community
@

The text did not appear in the grey box within the pdf. 

I did not understand how hook_output may be used.

Thanks.
Thiago

Yihui Xie

unread,
Aug 9, 2013, 3:44:09 PM8/9/13
to Thiago Gonçalves Souza, kn...@googlegroups.com, Dan Tenenbaum
Hi Thiago,

Your problem is different with Dan's, and it is also easier to solve
than Dan's. That is FAQ 7: http://bit.ly/knitr-faq also see
https://github.com/yihui/knitr-examples/blob/master/038-output-width.Rnw

Or just turn off the chunk option tidy=FALSE, and format your source
code manually.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle

Thiago Gonçalves-Souza

unread,
Aug 10, 2013, 9:57:37 PM8/10/13
to Yihui Xie, kn...@googlegroups.com, Dan Tenenbaum
Thank you very much! It works perfectly! I have also changed a little bit options(width=50) and it worked.


\documentclass{article}

<<setup, include=FALSE>>=
options(width=50)
@

\usepackage[latin9]{inputenc}
\begin{document}




2013/8/9 Yihui Xie <xiey...@gmail.com>



--
Thiago Gonçalves-Souza
Pesquisador colaborador (Pós-Doutorando)
Departamento de Biologia Animal
Universidade Estadual de Campinas (UNICAMP)

Thiago Gonçalves-Souza

unread,
Aug 14, 2013, 9:40:19 PM8/14/13
to Yihui Xie, kn...@googlegroups.com, Dan Tenenbaum
Dear friends,

Even when using 



2013/8/9 Yihui Xie <xiey...@gmail.com>

Thiago Gonçalves-Souza

unread,
Aug 14, 2013, 9:42:07 PM8/14/13
to Yihui Xie, kn...@googlegroups.com, Dan Tenenbaum
Dear friends,

Even when using tidy.opts=list(width.cutoff=40) it is possible that text will not appear in the grey box. You can see a reproducible example:

Thank you in advance.



\documentclass{article}
\begin{document}

<<points, tidy.opts=list(width.cutoff=40), fig.align="center", fig.width=16, out.width="0.9\\textwidth">>=

library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point(size=4) + theme_bw() + geom_abline(intercept=37.28, slope=-5.34) + labs(x="Vehicle Weight", y="Miles per Gallon") + theme(axis.title.x = element_text(size = rel(2), angle = 0), axis.title.y = element_text(size = rel(2), angle = 90)) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.text = element_text(size = rel(2)))

@

\end{document}


2013/8/14 Thiago Gonçalves-Souza <tgoncalv...@gmail.com>

Yihui Xie

unread,
Aug 18, 2013, 3:57:45 AM8/18/13
to Thiago Gonçalves-Souza, knitr, Dan Tenenbaum
See the alternative approach I mentioned in my previous reply.

width.cutoff does not guarantee the code can be cut off exactly at the
desired place.

Regards,
Yihui
--
Yihui Xie <xiey...@gmail.com>
Phone: 206-667-4385 Web: http://yihui.name
Fred Hutchinson Cancer Research Center, Seattle


Alison Shelton

unread,
Jan 29, 2017, 9:53:30 AM1/29/17
to knitr, tgoncalv...@gmail.com, dand...@gmail.com
I know this is an old thread, but I wanted to share a lower level solution that worked for me.

Using the results = 'asis' and no quote, I was able to get wrapping the way I wanted it. The solution described above worked beautifully, but I was looking for a way to do this without defining the output width.

```{r, messages=FALSE, results='asis'}
library(KEGG.db)
BRCA1.EID <- unlist(mget("BRCA1", org.Hs.egSYMBOL2EG, ifnotfound = NA))
BRCA1.EID <- as.character(BRCA1.EID)

kegg <- mget(as.character(BRCA1.EID), KEGGEXTID2PATHID, ifnotfound = list(NA))

library(KEGGREST)
genes <- keggGet(kegg)

noquote(genes[[1]]$DESCRIPTION)
```
Reply all
Reply to author
Forward
0 new messages