使用Knitr和RStudio将代码折叠在外部文件中 [英] Code folding in external files with knitr and RStudio

查看:127
本文介绍了使用Knitr和RStudio将代码折叠在外部文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到将语法上可接受的RStudio样式折叠插入到外部R代码文件中的方法,该外部R代码文件是为从Knitr文档设置而使用的.还是我错过了一些东西.有几种方法可以完成此操作: 1)允许代码标头,例如:

I can find no way to insert syntactically acceptable RStudio style folds into an external R code file that is set up for use from a knitr document. Or am I missing something. There are several ways this might be done: 1) Allow a code header such as:

## @knitr Q1 ----

或者也许

## @knitr 'Q1' ----

2)折叠每个代码块(这将是RStudio中的更改),但这不是 就像我理想中的一般.

2) Fold every code chunk (this would be a change in RStudio), but this is not as general as I would ideally like.

3)允许在代码文件中包含某种注释行,以指示折叠.我还没有找到一种方法不会将注释行添加到以前的代码块中.

3) Allow the inclusion of some kind of comment line in code files that would indicate a fold. I have not been able to find a way to do this that does not add the comment line to the previous code chunk.

[自从最初发布此内容以来,我注意到read_chunk()中的参数'from'和'to'可以是为代码块指定起始字符串和起始字符串的正则表达式.因此,这提供了一种允许插入可以指定折叠的注释行的方法.不过,能够使用上面的机制1-3中的一种或多种是很好的.]

[Since initially posting this, I have noticed that the arguments 'from' and 'to' in read_chunk() can be regular expressions that specify start and from character strings for code chunks. So this gives one way to allow the insertion of comment lines that can specify folds. It would be nice however to be able to use one or more of mechanisms 1-3 above.]

推荐答案

knitr v1.2.11及更高版本开始,knitr中始终支持RStudio样式代码标头.规则基本上是# ---- label:

From knitr v1.2.11 and above, the RStudio style code headers are supported consistently in knitr. The rule is basically # ---- label:

  • 开头有一个或多个哈希#
  • 至少跟四个破折号----
  • 后跟大块标签
  • ,并可选地后跟任意数量的破折号
  • one or more hashes # in the beginning
  • followed by at least four dashes ----
  • followed by the chunk label
  • and optionally followed by any number of dashes

read_chunk()purl()均支持此功能,即,在knitr中的导入和导出代码中均使用这种注释风格.

This is supported in both read_chunk() and purl(), i.e., this style of comments is used in both importing and exporting code in knitr.

要使RStudio支持代码折叠,您必须在注释标题的末尾至少添加四个破折号,例如

For RStudio to support code-folding, however, you will have to add at least four dashes to the end of the comment header, e.g.,

# ---- chunk-label -----------------------------

knitr 1.2.11是Github上的开发版本,最终将成为1.3在CRAN上.

knitr 1.2.11 is a development version on Github, which will become 1.3 eventually on CRAN.

这篇关于使用Knitr和RStudio将代码折叠在外部文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆