简体   繁体   English

Knitr:spin - 如何添加文本而无需手动添加#'每一行?

[英]Knitr:spin - how to add text without manually adding #' every line?

My understanding is that knitr:spin allows me to work on my plain, vanilla, regular ol' good R script, while keeping the ability to generate a full document that understands markdown syntax. 我的理解是knitr:spin允许我使用普通的,普通的,常规的'好R脚本',同时保持生成理解markdown语法的完整文档的能力。 (see https://yihui.name/knitr/demo/stitch/ ) (见https://yihui.name/knitr/demo/stitch/

Indeed, the rmarkdown feature in Rstudio , while super neat, is actually really a hassle because 事实上, rmarkdown在功能Rstudio ,而超级整齐,其实真的是一件麻烦事,因为

  • I need to duplicate my code and break it in chunks which is super boring + inefficient as it is hard to keep track of code changes. 我需要复制我的代码并将其分解为超级无聊+低效的块,因为很难跟踪代码更改。
  • On top of that rmarkdown cannot read my current workspace. 最重要的是, rmarkdown无法读取我当前的工作空间。 This is somehow surprising but it is what it is. 这在某种程度上令人惊讶,但它就是它的本质。

All in all this is very constraining... See here for a related discussion Is there a way to knitr markdown straight out of your workspace using RStudio? 总而言之,这是非常有限的...请参阅此处进行相关讨论有没有办法使用RStudio直接从工作区中编织降价? .

As discussed here ( http://deanattali.com/2015/03/24/knitrs-best-hidden-gem-spin/ ), spin seems to be the solution. 因为这里所讨论( http://deanattali.com/2015/03/24/knitrs-best-hidden-gem-spin/ ), spin似乎是解决方案。

Indeed, knitr:spin syntax looks like the following: 的确, knitr:spin语法如下所示:

#' This is a special R script which can be used to generate a report. You can
#' write normal text in roxygen comments.
#'
#' First we set up some options (you do not have to do this):

#+ setup, include=FALSE
library(knitr)

in a regular workspace! 在一个普通的工作区!

BUT note how each line of text is preceded by #' . 请注意每行文字的前面是#'

My problem here is that it is also very inefficient to add #' after each single line of text. 我的问题是在每行文本后添加#'效率非常低。 Is there a way to do so automatically? 有没有办法自动完成?

Say I select a whole chunk of text and rstudio adds this #' every row? 假设我选择了一大块文本,rstudio每行添加#' Maybe in the same spirit as commenting a whole chunk of code lines? 也许与评论一大堆代码行的精神相同?

Am I missing something? 我错过了什么吗?

Thanks! 谢谢!

In RStudio v 1.1.28, starting a line with #' causes the next line to start with #' when I hit enter in a *.R file on my machine (Ubuntu Linux 16.04LTS). 在RStudio v 1.1.28,开头的行#'导致下一行开始与#'时,我打在进入*.R我的机器(Ubuntu Linux操作系统16.04LTS)上的文件。

So as long as you start a text chunk with it, it will continue. 因此,只要您使用它启动文本块,它就会继续。 But for previously existing R scripts, it looks like you would have to use find -> replace , or write a function to modify the required file, this worked for me in a very simple test. 但对于以前存在的R脚本,看起来你必须使用find -> replace ,或编写一个函数来修改所需的文件,这对我来说非常简单。

comment_replace <- function(in_file, out_file = in_file){
  in_text <- scan(file = in_file, what = character(), sep = "\n")
  out_text <- gsub("^# ", "#' ", in_text)
  cat(out_text, sep = "\n", file = out_file)
}

I would note, that this function does not check for preexisting #' , you would want to build that in. I modified it so that it shouldn't replace them too much by adding a space in the regular expression. 我想指出,这个函数检查预先存在#' ,你想建立一个在我修改,以便它不应该取代他们太多的正则表达式加空格。

With an RMarkdown document, you would write something like this: 使用RMarkdown文档,您可以编写如下内容:

As you can see I have some fancy code below, and text right here.

```{r setup}
# R code here
library(ggplot2)
```

And I have more text here...

This gist offers a quick introduction to RMarkdown and knitr 's features. 这个要点简要介绍了RMarkdown和knitr的功能。 I think you don't entirely understand what RMarkdown really is, it's a markdown document with R sprinkled in between, not (as you said) an R script with markdown sprinkled in between. 我认为你并不完全理解RMarkdown究竟是什么,它是一个降价文档,其间有R,而不是(正如你所说)一个带有降价的R脚本。


Edit: For those who are downvoting, please read the comments below this... OP didn't specify he was using spin earlier. 编辑:对于那些正在贬低的人,请阅读下面的评论...... OP没有说明他之前使用的是spin

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

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