knitr to PDF 不包装评论

knitr to PDF not wrapping comments

当尝试将我的 Rmarkdown 文件编织成 PDF 时,knitr 似乎没有将评论换行,文本刚好超出了 pdf 边距。

我试过指定几个参数,但似乎没有任何效果。

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

如何让 knitr 将评论包含在空白处?

我遇到了同样的问题,即使进行了详尽的搜索也找不到其他解决方案:

  • 手动将评论分成更短的行
  • 使用 paste0("foo ... ", "... bar")

我猜你没有安装 formatR 软件包。如果是这样的话,你需要install.packages('formatR')。这是我在电脑上得到的:

此问题在安装 R v4.0.0 并重新安装软件包后得到解决。

> 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