Skip to content

Commit f9e1203

Browse files
committed
added a python tag to Wush's initial python from R post
1 parent 1f3e6bc commit f9e1203

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

_posts/2014-04-06-rcpp-python.md

+5-18
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
title: Call Python from R through Rcpp
33
author: Wush Wu
44
license: GPL (>= 2)
5-
tags: featured boost
5+
tags: featured boost python
66
summary: Integrate Python into R via Rcpp and Boost.Python
77
layout: post
88
src: 2014-04-06-rcpp-python.Rmd
99
---
1010

1111

1212

13-
1413
## Introduction
1514

1615
This post provides a brief introduction to calling Python from R through Rcpp. The
@@ -49,7 +48,6 @@ py_ldflags <- system("python2.7-config --ldflags", intern=TRUE)
4948
Sys.setenv("PKG_LIBS"=sprintf("%s %s %s", Sys.getenv("PKG_CFLAGS"), "-lboost_python-py27", py_ldflags))
5049
{% endhighlight %}
5150

52-
5351
The following `hello world` should then work:
5452

5553

@@ -81,9 +79,6 @@ void hello_python() {
8179

8280

8381

84-
85-
86-
8782
Let's call them in R:
8883

8984

@@ -93,12 +88,10 @@ hello_python()
9388
{% endhighlight %}
9489

9590

96-
9791
<pre class="output">
98-
Today is Sun Apr 6 09:03:02 2014
92+
Today is Thu Apr 2 11:32:17 2015
9993
</pre>
10094

101-
10295
It shows that the `hello_python` function successfully initializes the Python
10396
engine and runs the Python script through `PyRun_SimpleString`.
10497

@@ -132,18 +125,16 @@ SEXP IntVec_to_py_list(IntegerVector src) {
132125
{% endhighlight %}
133126

134127

135-
136128
{% highlight r %}
137129
IntVec_to_py_list(1:10)
138130
{% endhighlight %}
139131

140132

141133

142134
<pre class="output">
143-
&lt;pointer: 0x19d1f80&gt;
135+
&lt;pointer: 0x8e0b410&gt;
144136
</pre>
145137

146-
147138
The pointer refers to the memory of the transformed Python object.
148139

149140
## Call Python Function
@@ -180,7 +171,6 @@ void pyfun(std::string fun_name, SEXP fun_argument) {
180171
{% endhighlight %}
181172

182173

183-
184174
{% highlight r %}
185175
pycall("
186176
def print_list(src):
@@ -206,7 +196,6 @@ pyfun("print_list", a)
206196
10
207197
</pre>
208198

209-
210199
## Error Handling
211200

212201
Errors in the Python engine can be handled easily by the C++ `try/catch`
@@ -240,7 +229,6 @@ void pyfun(std::string fun_name, SEXP fun_argument) {
240229
{% endhighlight %}
241230

242231

243-
244232
{% highlight r %}
245233
pycall("
246234
def print_list(src):
@@ -257,21 +245,20 @@ pyfun("print_lists", a) # a typo of the function name
257245
KeyError: 'print_lists'
258246
Error in sys.excepthook:
259247
Traceback (most recent call last):
260-
File &quot;/usr/lib/python2.7/dist-packages/apport_python_hook.py&quot;, line 64, in apport_excepthook
248+
File &quot;/usr/lib/python2.7/dist-packages/apport_python_hook.py&quot;, line 63, in apport_excepthook
261249
from apport.fileutils import likely_packaged, get_recent_crashes
262250
File &quot;/usr/lib/python2.7/dist-packages/apport/__init__.py&quot;, line 5, in &lt;module&gt;
263251
from apport.report import Report
264252
File &quot;/usr/lib/python2.7/dist-packages/apport/report.py&quot;, line 16, in &lt;module&gt;
265253
from xml.parsers.expat import ExpatError
266254
File &quot;/usr/lib/python2.7/xml/parsers/expat.py&quot;, line 4, in &lt;module&gt;
267255
from pyexpat import *
268-
ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so: undefined symbol: _Py_ZeroStruct
256+
ImportError: /usr/lib/python2.7/lib-dynload/pyexpat.i386-linux-gnu.so: undefined symbol: _Py_ZeroStruct
269257

270258
Original exception was:
271259
KeyError: 'print_lists'
272260
</pre>
273261

274-
275262
## Summary
276263

277264
These examples show how to integrate Python and R with Rcpp and Boost.Python.

src/2014-04-06-rcpp-python.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Call Python from R through Rcpp
33
author: Wush Wu
44
license: GPL (>= 2)
5-
tags: featured boost
5+
tags: featured boost python
66
summary: Integrate Python into R via Rcpp and Boost.Python
77
---
88

0 commit comments

Comments
 (0)