Skip to content

Instantly share code, notes, and snippets.

@kohske
Last active October 29, 2015 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohske/f4b8a828da5da7c74717 to your computer and use it in GitHub Desktop.
Save kohske/f4b8a828da5da7c74717 to your computer and use it in GitHub Desktop.
Seamless access to python objects in R
---
title: "Seamless Use of Python with RMarkdown and PythonInR"
author: '@kohske'
date: "Oct. 10, 2015"
output: html_document
---
PythonInR http://pythoninr.bitbucket.org/
https://cran.r-project.org/web/packages/PythonInR/README.html
Define a hook
```{r include=FALSE}
library(knitr)
library(PythonInR)
c2s = function(before, options, envir) {
if (before) {
} else {
v=pyExecg(paste0(options$code, collapse="\n"), mergeNamespaces = TRUE, override = TRUE)
for (n in names(v)) assign(n, v[[n]], envir)
}
}
knit_hooks$set(py = c2s)
```
Write python chunk
In future python chunks will be defined by chunk option `engine = 'py'`
```{r, eval=FALSE, py=TRUE, include=FALSE}
# this is a python code
a=range(0, 3)
b=range(0, 5)
```
Access the variable
```{r}
a
b
expand.grid(a, b)
```
```{r, eval=FALSE, py=TRUE, include=FALSE}
c=a+a
```
```{r}
c
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment