Skip to content

Instantly share code, notes, and snippets.

@slopp
Created January 24, 2017 00:43
Show Gist options
  • Save slopp/3332e36287a34a45f5eeeda85bc4da15 to your computer and use it in GitHub Desktop.
Save slopp/3332e36287a34a45f5eeeda85bc4da15 to your computer and use it in GitHub Desktop.
Python + SQL in RStudio Notebooks
---
title: "dplyr Database Example"
output: github_document
---
```{r}
library(DBI)
library(RSQLite)
# Can be any DBI compliant database connection
con <- RSQLite::datasetsDb()
RSQLite::dbListTables(con)
```
```{sql connection=con}
Select * FROM trees
```
```{sql connection=con, output.var='trees'}
Select * FROM trees
```
```{r}
trees
```
---
title: "Interacting with Python from R"
output: html_notebook
---
# Using python from within R
The following uses Bryan Lewis's `python` package. All of the code *is R code*, where R is acting as a wrapper that calls python objects. One thing that is handy about this is the python objects can stick around!
## Setup
```{r}
#devtools::install_github("bwlewis/python")
library(python)
np = import("numpy")
```
## Go back and forth between R any Python objects.
From R to Python:
```{r}
set.seed(1)
x = matrix(rnorm(9), nrow=3)
pyX = np$array(x)
print(pyX)
```
Note that the `p` variable is really a Python object, for example the output
above was printed by Python.
```{r}
class(pyX)
```
From python object back into R:
```{r}
Rx = R(pyX)
class(Rx)
identical(Rx, x)
```
## Python methods
Use the `pyhelp()` function if you want help on something. At the moment,
`pyhelp()` needs you to provide a full valid Python name (with dots). For instance:
```{r}
pyhelp("numpy.ndarray.prod")
```
```{r}
pyX$prod()
```
---
title: "R & Python Code Chunks"
output:
html_document: default
html_notebook: default
---
Comparing pricipal component analysis in R and Python.
## R
```{r}
data(iris)
iris.data <- iris[, 1:4]
pca <- princomp(iris.data)
scores <- pca$scores[, 1:2]
scores[1:6, ]
```
## Python
```{python}
from sklearn import decomposition, datasets
iris = datasets.load_iris()
pca = decomposition.PCA(n_components=2)
pca.fit(iris.data)
scores = pca.transform(iris.data)
print scores[0:6, ]
```
You can run multiple versions:
## Python Versions
```{python, engine.path="/usr/bin/python2.6"}
import sys
print(sys.version)
```
```{python, engine.path="/usr/bin/python2.7"}
import sys
print(sys.version)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment