You would need to create an advanced work flow. It doesn't need to be too "advanced".
Your program R needs to be invoked by a chunk of Java code. The Java code needs to transport F to the normal file space of the server, invoke R on the transported copy of F with the appropriate parameters, and then load the resulting file, S, back into Alfresco.
I realize that this is lacking in details. I still need to experiment with this myself. However, you might look into the implementations of data transformers supplied with Alfresco to convert, for instance, Word documents into PDF documents. Alfresco uses Open Office services to do the transformation, but I'm sure there is good educational material there.
(Fellow student.)