6
votes

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`
1
I'm afraid this is currently not possible with 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/….krlmlr
NB: The question would be clearer if you showed the desired behavior (e.g., example file with desired output). Welcome to StackOverflow!krlmlr
I have included the entire sample RMarkdown document in the original post. Thanks for the quick reply and information, I was unable to figure that out before. When you say that a new instance is started for each engine, is that for the entire document? Or per each code chunk? And how does that preclude calling values/objects from those instances for insertion in the text? I am considering writing output from other languages to a text file and calling that in an R chunk if I have to, though its a rather inelegant solution. Thanks.user5359531
A new instance for each chunk. This means that e.g. each Python chunk starts a fresh Python session, and you need something that preserves state and/or transfers it to/from R. I guess that a simple file interface, as you suggested, is the next best thing you can do currently.krlmlr
It is currently not possible with knitr unless you extend its python 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 use get_python_value('your python code') in the inline R expression.Yihui Xie

1 Answers

1
votes

Instead of looking to change or extend the inline code delimiter to interpret multiple languages, use the reticulate to call Python form R and return the results to R objects.

Modifying the .rmd file as below. Make sure to use the correct path for the version of python you want to use. The thing to note about this rmd file is that everything is evaluated via R. Python code is evaluated via py_run_string and the results are returned to the R environment via py_to_r call.

---
title: "SAMPLE"
author: "me"
date: "September 21, 2015"
output:
  pdf_document:
    keep_tex: yes
---

```{r setup}
library(reticulate)
reticulate::use_python(python = "/opt/anaconda3/envs/ten2/bin/python", required = TRUE)
```


```{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}
pycode <- 
'
COLOR = "red"
print(COLOR)
'
pyrtn <- py_to_r(py_run_string(code = pycode))
```

You cannot do the same thing with Python, or other types of code:
The car is  `r pyrtn$COLOR`

The resulting PDF looks like this:

enter image description here