[IPython-dev] use of InteractiveShell and R magic

Jonathan Taylor jonathan.taylor at stanford.edu
Thu Jun 14 18:48:13 EDT 2012


I should say that what I really want to do is to take an .ipynb file and
execute it in such a way that I can take some things from its namespace into
the namespace of the python process. For example, I might have a function
or a class defined in the .ipynb file that I want to pull into the python
process.

Using InteractiveShell or TerminalInteractiveShell seems to work for this,
except for raising errors about not finding the %R magic.

On Thu, Jun 14, 2012 at 3:44 PM, Jonathan Taylor <
jonathan.taylor at stanford.edu> wrote:

> Actually, I want to create an InteractiveShell inside another python
> process. Maybe I should use embed?
>
> Inside ipython this doesn't work, but maybe it would inside another python
> process?
>
> In [12]: import IPython.frontend.terminal as IT
>
> In [13]: shell = IT.embed.InteractiveShellEmbed()
>
> In [14]: shell.run_cell('%R X=rnorm(20)')
> ERROR: Line magic function `%R` not found.
>
> On Thu, Jun 14, 2012 at 3:35 PM, MinRK <benjaminrk at gmail.com> wrote:
>
>>
>>
>> On Thu, Jun 14, 2012 at 3:23 PM, Jonathan Taylor <
>> jonathan.taylor at stanford.edu> wrote:
>>
>>> I'm trying to run some cells through InteractiveShell and am getting an
>>> unexpected error message which is that my shell
>>> can't find the %R line magic:
>>>
>>>
>>> In [1]: import IPython.frontend.terminal.interactiveshell as IS
>>>
>>> In [2]: shell = IS.InteractiveShell()
>>>
>>> In [3]: shell.run_cell('%load_ext rmagic')
>>>
>>> In [4]: shell.run_cell('%R X=rnorm(20)')
>>> ERROR: Line magic function `%R` not found.
>>> In [5]:
>>>
>>> In [5]: load_ext rmagic
>>>
>>> In [6]: %R X=rnorm(20)
>>> Out[6]:
>>> array([ 0.08724969,  2.03344439, -0.82111458, -2.07136391, -0.11913743,
>>>         0.19598438,  0.58574042, -0.33684877, -0.9659225 , -0.68404775,
>>>        -0.0278716 ,  1.34169008, -0.6034878 ,  1.76146422, -0.19225874,
>>>        -1.15088263, -1.10644146, -1.27298014, -1.18212805,  1.60194601])
>>>
>>> Is this expected? I can't tell.
>>>
>>
>> One thing that's not at all expected is creating a new InteractiveShell
>> inside an InteractiveShell.  Do you mean to be creating a whole new
>> InteractiveShell object in which to run these commands?
>>
>> If you want a handle on the*existing* shell, you can use: shell =
>> get_ipython().
>>
>> If you do that, your run_cell calls above seem to behave as you are
>> expecting.
>>
>> -MinRK
>>
>>
>>>
>>>
>>> --
>>> Jonathan Taylor
>>> Dept. of Statistics
>>> Sequoia Hall, 137
>>> 390 Serra Mall
>>> Stanford, CA 94305
>>> Tel:   650.723.9230
>>> Fax:   650.725.8977
>>> Web: http://www-stat.stanford.edu/~jtaylo
>>>
>>> _______________________________________________
>>> IPython-dev mailing list
>>> IPython-dev at scipy.org
>>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>>>
>>>
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
>
>
> --
> Jonathan Taylor
> Dept. of Statistics
> Sequoia Hall, 137
> 390 Serra Mall
> Stanford, CA 94305
> Tel:   650.723.9230
> Fax:   650.725.8977
> Web: http://www-stat.stanford.edu/~jtaylo
>



-- 
Jonathan Taylor
Dept. of Statistics
Sequoia Hall, 137
390 Serra Mall
Stanford, CA 94305
Tel:   650.723.9230
Fax:   650.725.8977
Web: http://www-stat.stanford.edu/~jtaylo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20120614/aa2c3999/attachment.html>


More information about the IPython-dev mailing list