rdelbel
rdelbel

Reputation: 73

Knitr echo function call with evaluated parameter names

If I have previously defined

n <- 200   
maf <- .2 

Is there any way to echo

snp <- rbinom(n,2,maf)

such that it displays as

snp <- rbinom(200,2,.2)

in the resulting knitr document.

Upvotes: 6

Views: 106

Answers (1)

agstudy
agstudy

Reputation: 121578

You can use substitute like this :

```{r ,echo=FALSE}
 substitute(snp <- rbinom(n,2,maf),list(n=n,maf=maf))
 rbinom(n,2,maf)
```

## snp <- rbinom(200, 2, 0.2)
##   [1] 0 0 1 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 2 0 1 1 2 1 0 0 0 0
##  [36] 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 1
##  [71] 0 1 0 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 1 1 1 2 0 1 0 1 1 0 1 0 0
## [106] 1 1 0 1 1 1 0 0 1 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
## [141] 1 0 0 1 0 0 1 0 2 0 0 0 1 0 0 0 1 1 0 0 0 0 0 2 1 1 2 0 1 0 0 0 0 0 1
## [176] 0 0 2 0 1 1 0 0 0 0 0 1 0 1 2 0 1 1 2 0 0 1 1 1 0

Upvotes: 2

Related Questions