user3149973
user3149973

Reputation: 53

Using knitr to create HTML slide with separate output of just the R code

I am using knitr to create a set of lecture slides for a class using R. I would like to create a separate "companion file" that contains just has the R code (corresponding to the slides), so that students can execute the R code by cutting and pasting from the companion file.

For example, in the .Rmd file:

``` {r ....}
plot(x,y)
```

Then there would be a text file with:

plot(x,y)

But, have such a file be automatically produced from the .Rmd file?

Even better if the .Rmd file has such tags:

``` {r basic.plot ....}
plot(x,y)
```

Then, text file has:

# basic.plot
plot(x,y)

Can this be accomplished using knitr?

Upvotes: 3

Views: 187

Answers (1)

Chris
Chris

Reputation: 136880

Yes, this is possible. What you're trying to do is called tangling, and it comes from the world of literate programming.

The knit function supports a tangle option that should be set to TRUE if you want to extract source code.

Upvotes: 6

Related Questions