Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

How do I explain a complicated function in Rmarkdown?

$
0
0

I have a somewhat complicated R function (different parts refer to various equations I'd like to explain.) I want to write my code using R Markdown, but I need to be able to explain the different parts of the function in the document. A bit like literate programming.

I wonder if it is possible to split the function definition across different code chunks, or maybe insert markdown comments inside the function definition?

I'd like the end result to look something like this:

complicated = function(x) {

I use x^2 in the example so it is math, and yet short. Here it is in the actual R code:

  x^2
}

This doesn't work:

``` {r}
complicated = function(x) {
```
explain the function with some math $x^$.
``` {r}
 x^2
}
```

The following actually does work:

``` {r chunk1, echo=FALSE}
complicated=function(x) {
  x^2
}
```

``` {r chunk1, echo=1:2, eval=F}
```
In the complicated function I use x^2 as an example.

Though it is a bit tricky, because if lines in the function change, I have to edit everything again. Also in the Rmd file, you don't actually see the code described by the comments.

Note:

Maybe it is possible to somehow have the relevant chunks written to a file without being evaluated, and then the file read in? I didn't manage to figure out a way yet...


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>