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`
The reticulate package includes a Python engine for R Markdown that enables easy interoperability between Python and R chunks. Python chunks behave very similar to R chunks (including graphical output from matplotlib) and the two languages have full access each other's objects.
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
There are three basic components of an R Markdown document: the metadata, text, and code.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With