4

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. (see https://yihui.name/knitr/demo/stitch/)

Indeed, the rmarkdown feature in Rstudio, while super neat, is actually really a hassle because

  • 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. 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?.

As discussed here (http://deanattali.com/2015/03/24/knitrs-best-hidden-gem-spin/), spin seems to be the solution.

Indeed, knitr:spin syntax looks like the following:

#' 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? Maybe in the same spirit as commenting a whole chunk of code lines?

Am I missing something?

Thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • 1
    What about replacing every hash with a hash and an apostrophe in a selected chunk of code? – kaksat Jan 08 '17 at 21:28
  • thanks dude but I just wanna write my text without adding anything manually. that completely defeats the purpose of helping to write a scientific document... – ℕʘʘḆḽḘ Jan 08 '17 at 21:34
  • 1
    Are you talking about comment text inside a code chunk or regular text outside of a code chunk? Can you post a sample of your full rmarkdown document? – eipi10 Jan 08 '17 at 21:38
  • I mean regular text outside of a code chunk. Something like "in this paper we do this this and this and oh by the way this is the output when we do this" and then boom I show the output of my code chunk – ℕʘʘḆḽḘ Jan 08 '17 at 21:39
  • 1
    For regular text outside of a chunk, just type regular text. No hash marks are needed. – eipi10 Jan 08 '17 at 21:41
  • I cannot get it to work `library(tidyverse) library(knitr) this is a test this is a test data <- data_frame(c('one', 'two', 'trree')) data` – ℕʘʘḆḽḘ Jan 08 '17 at 21:43
  • I cannot compile the short R code above. What am I missing again? thanks! – ℕʘʘḆḽḘ Jan 08 '17 at 21:44
  • 1
    Open a new `rmarkdown` document in RStudio, which includes a sample document. Note how R code is set off in separate chunks. For inline R code, that is, R code within a sentence, let's say that you've created a value `x` earlier in a code chunk and you want to use it in a sentence. Then you can type this (without the double quotes): "And now we can see that \`x\` is equal to \`r x\`". All the basic info on using R markdown can be found [here](http://rmarkdown.rstudio.com/lesson-1.html). – eipi10 Jan 08 '17 at 21:48
  • 1
    It sounds like he wants to use `knitr::spin` and not write an rmd. – Jake Kaupp Jan 08 '17 at 21:52
  • that is exacly that. sorry for the confusion I am updating my question – ℕʘʘḆḽḘ Jan 08 '17 at 21:54
  • 1
    If you need the objects in your script, but hate typing the roxygen comment, why not just `source` your code in the beginning of an `rmd`? This way you don't have to worry about matching environments of `knitr` and your code, and can reference any objects constructed by your code. – Jake Kaupp Jan 09 '17 at 00:54
  • HI @JakeKaupp do you mean using `source` allows me to 1) load my data once and for all and 2) create some charts, compile, change some stuff, compile again, add some text, re-compile WITHOUT loading the whole data again??? – ℕʘʘḆḽḘ Jan 09 '17 at 12:54
  • Yes. Or you can just put that code in a rmd chunk, and have it run and have access to everything. – Jake Kaupp Jan 09 '17 at 13:04
  • i am confused. If I put the thing in a code chunk, how can I compile my document without rstudio needing to reload everything? according to https://stackoverflow.com/questions/11155182/is-there-a-way-to-knitr-markdown-straight-out-of-your-workspace-using-rstudio rmarkdown needs to run the whole stuff again. sorry if I dont get your point again – ℕʘʘḆḽḘ Jan 09 '17 at 13:09
  • what I want to do is to generate my html/pdf/whatever many times as I write it, but without needing to run the whole code every time. – ℕʘʘḆḽḘ Jan 09 '17 at 13:14
  • 1
    Really, the easiest way to regenerate results quickly, without running long running parts, is to use the caching feature of chunks in knitr. Look it up, it will make re-runs of full documents take much less time. This applies for either `spin` or `knit`. – rmflight Jan 10 '17 at 01:58

2 Answers2

4

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

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.

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.

rmflight
  • 1,871
  • 1
  • 14
  • 22
  • 1
    I would also note that if you **start** with using Rmd, the whole thing is much easier, and you can use `Ctrl+alt+p` to run all previous chunks, run all chunks to get similar behavior as `rmarkdown::render`. – rmflight Jan 09 '17 at 14:09
  • thanks! one question that I still have is the following. say my first chunk of code loads all the data I need. Can I comment that code afterwards so that when I `render` my document Rstudio only uses the objects in memory? – ℕʘʘḆḽḘ Jan 09 '17 at 14:51
  • 1
    you can comment that code, or use `eval=FALSE`, so it won't get re-run on next render. – rmflight Jan 09 '17 at 14:58
0

With an RMarkdown document, you would write something like this:

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


Edit: For those who are downvoting, please read the comments below this... OP didn't specify he was using spin earlier.

Aryaman
  • 3,136
  • 3
  • 13
  • 16