Control from R ============== Contents: .. contents:: :local: :depth: 2 Installation ------------ Download mPsy with the sample experiment in R from `here `_. In the following the R environment is used only to define stimulus parameters and the timeline of the experiment. Presentation of stimuli requires a working Python installation as described in :doc:`tutorials`. Once Python is installed, extract the downloaded mPsy package into an empty directory. The bridge between mPsy and R uses two R packages: ``rjson`` and ``ttpRequest``. Make sure you have them. Sample experiment ----------------- In this experiment R is used to remotely control the experiment run in Python. The following Python program "listens" to R commands and it presents no stimuli. .. literalinclude:: ../../mPsy/examples/exp_tut_webremote.py The remote control is enabled by: .. literalinclude:: ../../mPsy/examples/exp_tut_webremote.py :lines: 14-19 When the Python experiment is already running, we use R to define what stimuli appear on the screen and when. mPsy will take care of stimulus presentation and interaction with the subject (i.e., it will collect responses and send this information back to R). Here is the code in R: .. highlight:: r .. literalinclude:: ../../mPsy/examples/r/dots_tut3.r To run this demonstration, download the file and run ``exp_tut_webremote.py`` by * either double-clicking the file name * or executing ``python exp_tut_webremote.py`` in the Terminal. A successfully executed Python program will create an empty black screen or an empty black window if the ``fullscreen`` option is off. At this point you can go to R, change the working directory to the one containing the file ``dots_tut3.r`` and run ``source('dots_tut3.r')``. The syntax of the sample experiment in R is modelled after the sample example in Python. See main :doc:`tutorials` for an explanation of how to control the parameters of stimuli and the timeline. Recording responses ------------------- The events triggered by mPsy or the subject are described in the main Python tutorial :doc:`tut_python`. To catch events in R we regularly check for messages accumulating in the Python presentation program: .. literalinclude:: ../../mPsy/examples/r/dots_tut3.r :lines: 80-100