11

It's fairly trivial to load external R scripts as per this R Sweave example:

<<external-code, cache=FALSE>>=
read_chunk('foo-bar.R')
@

Can the same be done for R Markdown?

Uwe
  • 41,420
  • 11
  • 90
  • 134
opyate
  • 5,388
  • 1
  • 37
  • 64
  • @DirkEddelbuettel AFAIK, this specific question has not been answered before, and since I know the answer, [I am encouraged to self-answer](http://meta.stackexchange.com/questions/12513/should-i-not-answer-my-own-questions). – opyate Feb 10 '13 at 17:19
  • Once, maybe. Twice, gets suspicious. – Dirk Eddelbuettel Feb 10 '13 at 17:21
  • 7
    @DirkEddelbuettel Why suspicious? SO is a Q&A site where answering your own question is encouraged. If someone has a question, searches SO and cannot find an answer, but does find the answer with their own research then we should be happy that they return here to provide that answer for others with the same question later. – Dan Midwood Feb 10 '13 at 18:28
  • @DanMidwood, it is suspicious behaviour because in both cases it has been just parroting back either the help file directly, or the examples from the website which acts as the help. – mnel Feb 10 '13 at 23:26

1 Answers1

16

Yes.

Put this at the top of your R Markdown file:

```{r setup, echo=FALSE}
opts_chunk$set(echo = FALSE, cache=FALSE)
read_chunk('../src/your_code.R')
```

Delimit your code with the following hints for knitr (just like @yihui does in the example):

## @knitr part1
plot(c(1,2,3),c(1,2,3))

## @knitr part2
plot(c(1,2,3),c(1,2,3))

In your R Markdown file, you can now have the snippets evaluated in-line:

Title
=====

Foo bar baz...

```{r part1}
```

More foo...

```{r part2}
```
opyate
  • 5,388
  • 1
  • 37
  • 64
  • Sorry, @opyate, do you know, is it possible to use multi-words hints (like `# @knitr part one`)? After some testing I assume that's not possible but may be I'm just missing something... – A S Apr 29 '15 at 10:59
  • I'm not currently set up to test this suggestion, but try putting them on two separate lines? – opyate Apr 29 '15 at 12:19
  • What do you mean? It's no problem to put another word on another line. But knitr only recognizes one word as it seems to me... (The point is I already have comments in my file that divide that file into sections. And in order not to multiply entities I was thinking to re-use those existing comments also with knitr... But unfortunately that doesn't seem to work :)). – A S Apr 29 '15 at 12:26
  • Let me clarify: ```# @knitr part``` then on the next line ```# @knitr one```. – opyate Apr 29 '15 at 12:31
  • Just for clarity: nope, that dosen't work, too. Anyway, thanks for your support. – A S Apr 29 '15 at 13:36
  • How about escaping the space? e.g. ```# @knitr one\ two``` – opyate Apr 29 '15 at 13:48
  • Nope, sorry :). (Tried different quotation marks, too… With the same result.) – A S Apr 29 '15 at 14:00
  • The worst part is that it seems you to can't actually add anything after the first word after `# @knitr`. I.e., I use RStudio's folding by adding `####` at the end of comment to separate different sections – and no, you can't do that. As a result, have to duplicate quite a lot of comments. – A S Apr 29 '15 at 14:03
  • And the only solution to avoid that seems to read file first, play with `sub` in some way, produce tmp file for the knitting only, `read_chunk` that tmp file, knit, delete tmp file. – A S Apr 29 '15 at 14:06