使用带有knitr的内联代码包含预定义变量

时间:2013-04-16 19:10:39

标签: r knitr

如何使用带有knitr的内联表达式包含变量。

例如,这可以按预期工作:

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

但是如果你想打印预定义的字符串/数字:

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

Some text about `r company`

编织后没有印刷品。是否有需要包装的东西


更新会话信息,根据要求:

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   

1 个答案:

答案 0 :(得分:8)

将变量包装在I()中使其按预期工作。

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

Some text about `r I(company)`

虽然不明确.Rmd,但这里记录了这一点:http://yihui.name/knitr/demo/output/