4

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 is finished. I would like to view the print statements as the python program is running inside R. I have also tried sys.stdout.write(), but the result is the same. Any help is greatly appreciated.

Here is my code for test.py:

import time

for i in range(10):
  print 'i=',i
  time.sleep(5)
4
  • it's working fine for me on Linux and R 3.0.1 - I see output as it's being generated Commented May 22, 2014 at 18:40
  • I (the original poster) am running it on MacOS Commented May 22, 2014 at 18:50
  • On Windows 7, I see the same behavior as the OP. If it were R code running, I'd put in a call to flush.console(), but of course that doesn't help here. Commented May 22, 2014 at 19:59
  • 1
    You can use rPython R package. Commented May 22, 2014 at 20:15

1 Answer 1

2

Tested on Windows 8 with R v3.0.1

Simply right click on the r console then untick/unselect the Buffered Output option (See image below). Now execute your code you shall see the output of print statements!

image-file

Update:

I forgot to mention that I also needed to add sys.stdout.flush() after the print statement in the python file.

import time
import sys

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

Also if you select the Buffered Output option then when you left click on the r console while your script is executing you shall see the output. Keep clicking and the output is shown. :)

Sign up to request clarification or add additional context in comments.

1 Comment

@user3433489 If my answer solved your problem then please accept it by clicking on the tick mark on the left side. It helps other users to know that this question is solved and this solution really works. It also provides you some rep points and me too.:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.