简体   繁体   English

knitr to PDF 不包装评论

[英]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.当尝试将我的 Rmarkdown 文件编织为 PDF 时, knitr似乎没有包装评论,并且文本只是超出了 pdf 的边距。

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?我怎样才能让knitr将评论包装在边距内?

My guess is that you didn't have the formatR package installed.我的猜测是你没有安装formatR包。 If that is the case, you need to install.packages('formatR') .如果是这种情况,您需要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")使用 paste0("foo ... ", "... bar")

This issue was fixed after installing R v4.0.0 and reinstalling packages.安装 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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