1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# Evaluate and echo different lines
For demonstration purposes, we may want to show some source code in the output,
but really evaluate different code in the background.
```{r}
hook_source <- knit_hooks$get('source')
knit_hooks$set(source = function(x, options) {
res <- hook_source(x, options)
gsub("(^|\n)#'#' ", '\\1', res)
})
```
The trick is to mask the source code in special comments (e.g. `#'#'`), and
remove the comment markers later. Of course, you have to guarantee these markers
are unique.
```{r test, echo=-3}
x <- 2
## 1/sqrt(2 * pi) * exp(-x^2/2)
dnorm(x)
```
```{r sdf}
dfdfd
```
We used `echo=-3` to remove the 3rd expression from the source code, and
`gsub()` to strip `#'#'` off.
This is completely hack. Use with care.
Example code:
```{r}
## Example code:
paggregate(argent[, 2:5], by = list(Transect = argent$Transect, Season = argent$Season), FUN = sum)
```
|