Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include pre-defined variable using inline code with knitr

Tags:

r

knitr

How does one include a variable using inline expressions with knitr.

For example, this works as expected:

A random number from the normal distribution `r rnorm(1)`.

But if you want to print a pre-defined string/number:

```{r}
company <- "ABC Co"
```

Some text about `r company`

Nothing is printed after knitting. Is there something that needs to be wrapped


Update Session Info, as per request:

R version 3.0.0 (2013-04-03)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
 [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8    LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] slidify_0.3.3 knitr_1.2    

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.3 formatR_0.7    markdown_0.5.4 stringr_0.6.2  tools_3.0.0    whisker_0.1   
[8] yaml_2.1.7   
like image 362
Brandon Bertelsen Avatar asked Apr 16 '13 19:04

Brandon Bertelsen


1 Answers

Wrapping the variable in I() makes it work as expected.

```{r}
company <- "ABC Co"
```

Some text about `r I(company)`

Although not explicit for .Rmd, this is documented here: http://yihui.name/knitr/demo/output/

like image 153
Brandon Bertelsen Avatar answered Nov 16 '22 03:11

Brandon Bertelsen