knitr to PDF 不包装评论
Posted
技术标签:
【中文标题】knitr to PDF 不包装评论【英文标题】:knitr to PDF not wrapping comments 【发布时间】:2020-05-26 13:15:16 【问题描述】:在尝试将我的 Rmarkdown 文件合并为 PDF 时,knitr
似乎没有包裹 cmets,并且文本只是超出了 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
将 cmets 包裹在边距内?
【问题讨论】:
【参考方案1】:我遇到了同样的问题,即使进行了详尽的搜索,也没有找到其他解决方案:
手动将注释分成较短的行 使用 paste0("foo ... ", "... bar")【讨论】:
【参考方案2】:我的猜测是您没有安装 formatR 软件包。如果是这种情况,您需要install.packages('formatR')
。这是我在我的电脑上得到的:
【讨论】:
我已经安装并启动了这个库,但我仍然遇到同样的问题。 请通过将library(formatR)
放入Rmd 文档的代码块中来验证您是否已成功安装formatR。
是的,我这样做了,但没有任何区别,我只是再次尝试,得到了相同的结果。这一定是另一个问题......
这很奇怪。虽然我相信 R 3.6.1 应该可以工作,但它不是 R 的最新版本。也许在黑暗中盲目射击会升级 R 并重试......
到目前为止没有运气,我已经重新安装了 R 和所有的软件包,但仍然有同样的问题。我也可以将它复制到其他刚刚安装了全新 R 的笔记本电脑上。会不会是 Windows 或 MiKTeX 的原因?【参考方案3】:
安装 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
【讨论】:
以上是关于knitr to PDF 不包装评论的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Javascript 或 jQuery 中将标签包装在评论中 [重复]
使用 Ghostscript.NET 包装器打印 PDF - 空白页