Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call python with system() in R to run a python script emulating the python console

Tags:

python

r

I want to pass a chunk of Python code to Python in R with something like system('python ...'), and I'm wondering if there is an easy way to emulate the python console in this case. For example, suppose the code is "print 'hello world'", how can I get the output like this in R?

>>> print 'hello world'
hello world

This only shows the output:

> system("python -c 'print \"hello world\"'")
hello world

Thanks!

BTW, I asked in r-help but have not got a response yet (if I do, I'll post the answer here).

like image 401
Yihui Xie Avatar asked Apr 14 '12 17:04

Yihui Xie


People also ask

How do I call a Python script in R?

You can use the import() function to import any Python module and call it from R. For example, this code imports the Python os module and calls the listdir() function: library(reticulate) os <- import("os") os$listdir(".")

How do I run a Python script from the console?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!

Can we run Python code in R?

You can import any Python library and write any Python code you want, and then access the variables and functions declared with R. And that's how you can run Python code in R and R Markdown.

How do I run Python code in R markdown?

To add a Python code chunk to an R Markdown document, you can use the chunk header ```{python} , e.g., ```{python} print("Hello Python!") ```


2 Answers

Do you mean something like this?

export NUM=10
R -q -e "rnorm($NUM)"

You might also like to check out littler - http://dirk.eddelbuettel.com/code/littler.html

UPDATED

Following your comment below, I think I am beginning to understand your question better. You are asking about running python inside the R shell.

So here's an example:-

# code in a file named myfirstpythonfile.py

a = 1 
b = 19
c = 3 
mylist = [a, b, c]
for item in mylist:
    print item

In your R shell, therefore, do this:

> system('python myfirstpythonfile.py')
1
19
3

Essentially, you can simply call python /path/to/your/python/file.py to execute a block of python code.

In my case, I can simply call python myfirstpythonfile.py assuming that I launched my R shell in the same directory (path) my python file resides.

FURTHER UPDATED

And if you really want to print out the source code, here's a brute force method that might be possible. In your R shell:-

> system('python -c "import sys; sys.stdout.write(file(\'myfirstpythonfile.py\', \'r\').read());"; python myfirstpythonfile.py')
a = 1
b = 19
c = 3
mylist = [a, b, c]
for item in mylist:
    print item
1
19
3

AND FURTHER FURTHER UPDATED :-)

So if the purpose is to print the python code before the execution of a code, we can use the python trace module (reference: http://docs.python.org/library/trace.html). In command line, we use the -m option to call a python module and we specify the options for that python module following it.

So for my example above, it would be:-

$ python -m trace --trace myfirstpythonfile.py
 --- modulename: myfirstpythonfile, funcname: <module>
myfirstpythonfile.py(1): a = 1
myfirstpythonfile.py(2): b = 19
myfirstpythonfile.py(3): c = 3
myfirstpythonfile.py(4): mylist = [a, b, c]
myfirstpythonfile.py(5): for item in mylist:
myfirstpythonfile.py(6):     print item
1
myfirstpythonfile.py(5): for item in mylist:
myfirstpythonfile.py(6):     print item
19
myfirstpythonfile.py(5): for item in mylist:
myfirstpythonfile.py(6):     print item
3
myfirstpythonfile.py(5): for item in mylist:
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

Which as we can see, traces the exact line of python code, executes the result immediately after and outputs it into stdout.

like image 121
Calvin Cheng Avatar answered Nov 10 '22 06:11

Calvin Cheng


The system command has an option called intern = FALSE. Make this TRUE and Whatever output was just visible before, will be stored in a variable.

Now run your system command with this option and you should get your output directly in your variable. Like this

tmp <- system("python -c 'print \"hello world\"'",intern=T)
like image 42
Avinash Avatar answered Nov 10 '22 06:11

Avinash