Call python from R with instant console output

I run python scripts from R using the R command:

system('python test.py')

But my print statements in test.py do not appear in the R console until the python program finishes. I would like to look at print instructions since the python program works inside R. I also tried sys.stdout.write(), but the result is the same. Any help is appreciated.

Here is my code for test.py:

import time

for i in range(10):
  print 'i=',i
  time.sleep(5)
+4
source share
1 answer

Tested on Windows 8 with R v3.0.1

Just right-click on the r console, then disable / cancel the option Buffered Output(see image below). Now execute your code, you will see the output of the operators print!

image-file

Update:

, sys.stdout.flush() print python.

import time
import sys

for i in range(5):
    print 'i=',i
    sys.stdout.flush()
    time.sleep(1)

, Buffered Output, , r script, . .:)

+2

Source: https://habr.com/ru/post/1541663/


All Articles