简体   繁体   中英

knitr to PDF not wrapping comments

When trying to knit my Rmarkdown files to PDF, knitr doesn't seem to wrap the comments and the text just goes outside of the pdf margins.

I have tried specifying several parameters but nothing seems to have any effect.

---
title: "Demo margin"
output: pdf_document
---

```{r setup, include=FALSE}
options(width=80)
knitr::opts_chunk$set(echo = TRUE,out.width = 40, tidy=T, tidy.opts=list(width.cutoff=60))
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars, out.width=80, tidy = 'formatR', tidy.opts = list(blank = FALSE, width.cutoff = 60)}
summary(cars)
#very very very very very very very very very very very very very very very very very very very very very very very very incredibly very long comment
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}
plot(pressure)
```

在此处输入图像描述

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.6.1  htmltools_0.4.0 tools_3.6.1     yaml_2.2.0      Rcpp_1.0.3     
 [6] rmarkdown_2.0   knitr_1.26      xfun_0.11       digest_0.6.23   rlang_0.4.2    
[11] evaluate_0.14 

How can I get knitr to wrap the comments within the margins?

My guess is that you didn't have the formatR package installed. If that is the case, you need to install.packages('formatR') . Here is what I get on my computer:

由 formatR 重新包装的评论

I had the same problem and even with exhaustive searching found no other solution than either:

  • Break the comment manually into shorter lines
  • Use paste0("foo ... ", "... bar")

This issue was fixed after installing R v4.0.0 and reinstalling packages.

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252    LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                       LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] formatR_1.7

loaded via a namespace (and not attached):
 [1] compiler_4.0.0  htmltools_0.4.0 tools_4.0.0     yaml_2.2.1      Rcpp_1.0.4.6    rmarkdown_2.1   knitr_1.28      xfun_0.13       digest_0.6.25   rlang_0.4.6     evaluate_0.14 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM