We’ll need the reticulate
package. Enable the code chunk to install the package. (Set eval = TRUE
.)
Let’s now use the reticulate
package.
Next, we’re passing the secret from R to Python. (Variable secret
from r
.)
And finally, we’re checking in R if py$secret == secret
. (Variable secret
from py
.)
## [1] "Hello, World!"
All done!
The complete R Markdown
code for rpython_markdown.rmd
is below and here https://github.com/rmelikov/rpython_markdown/blob/master/rpython_markdown.rmd.
---
title: R and Python in R Markdown
subtitle: Variables get passed
from code chunk to code chunk
author: 'https://github.com/rmelikov/rpython_markdown'
# author:
# name: Ramin Melikov
# affiliation:
# 'https://www.linkedin.com/in/melikov/'
# email: ramin.melikov@gmail.com
# affiliation: Georgia Institute
# of Technology
# email: melikov@gatech.edu
# affiliation: University of
# Illinois at Urbana-Champaign
# email: melikov2@illinois.edu
# date: '`r format(as.POSIXct(Sys.Date()), '%B %d, %Y')`'
output:
html_document:
toc: false
theme: journal
highlight: tango
editor_options:
chunk_output_type: console
---
### Setup
We'll need the `reticulate`
package. Enable the code chunk
to install the package. (Set
`eval = TRUE`.)
```{r eval = FALSE}
install.packages("reticulate")
```
Let's now use the `reticulate`
package.
```{r}
library(reticulate)
```
### Start in R
First, we're using R to create a
secret.
```{r}
secret <- "RPythonMarkdown"
```
### Go to Python
Next, we're passing the secret
from R to Python. (Variable
`secret` from `r`.)
```{python}
secret = r.secret
```
### Back in R
And finally, we're checking in R
if `py$secret == secret`.
(Variable `secret` from `py`.)
```{r}
if (py$secret == secret) {
"Hello, World!"
}
```
All done!