In R Markdown, I can set an object in an R code chunk, then later have the value of that object in the body of my document, like so:
```{r}
TMP<-"Blue"
TMP
```
The sky is `r TMP`
The output of this in my compiled PDF document looks like this:
TMP<-"Blue"
TMP
## [1] "Blue"
The sky is Blue
This feature is incredibly useful. However, I do not want to be limited to using it solely with R code. I would like to be able to set objects in code chunks using other languages and then call up their values in text in the same way.
RMarkdown + knitr does a great job of allowing you to write and compile these code chunks in other languages, but I am not finding any way to call up the values of these objects in my document's text in the same way that this format in RMarkdown or the \Sexpr{} function from LaTeX does. I am open to using a different documentation system to accomplish this if it's easier. I have seen questions like this but this is simply not helpful because the scripts I will be using are much longer and more complicated than little one-liners like that.
Here is a complete RMarkdown document detailing the current behavior with R, and the desired (same) behavior with Python, etc..
---
title: "SAMPLE"
author: "me"
date: "September 21, 2015"
output:
pdf_document:
keep_tex: yes
---
```{r}
TMP<-"Blue"
TMP
```
You can insert the value of an object created with an R code chunk into text like this:
The sky is `r TMP`
```{r,engine='python'}
COLOR = "red"
print COLOR
```
You cannot do the same thing with Python, or other types of code:
The car is `python COLOR`
knitr
, as for every engine (except the native R engine) a new instance is started: github.com/yihui/knitr/blob/…, github.com/yihui/knitr/blob/…. – krlmlrpython
engine, which is possible, e.g. github.com/yihui/runr You will have to write your own function for inline expressions, e.g.get_python_value = function(code) { .... }
(it may or may not be trivial to fill the blank here) then useget_python_value('your python code')
in the inline R expression. – Yihui Xie