[NTG-context] Filter module for R

Aditya Mahajan adityam at umich.edu
Mon Jun 15 07:45:09 CEST 2020

On Sun, 14 Jun 2020, Fabrice L wrote:

> Hi,
> Following a question about the broken R module (R the statistical software), Aditya suggested me the filter module (thanks to him). I’m about to distribute a set of course notes to colleagues, and I need to deal with details now. I have one problem and two questions.
> 1) the filter collect R code between \startR / \stopR and submit this code to R. As it can be seen in the following minimal example, when a label contains an accented character (« Fréquence » in my example), the pdf graphic does not contains the « é ». Strangely, the snippet of code (which is saved locally as « test2-temp-R-0.tmp » (for test2.tex)) when submitted with the same command as the filtercommand, works correctly; that means the same file works when submitted to R outside of ConTeXt. I have no idea how to solve this.

Are you running the exact same command as the `filtercommand`. If so, I don't know why running the `filtercommand` through context vs directly typing it on the terminal should behave differently. The filter module effectively just runs os.execute("filtercommand"). I don't use R so I am unable to debug further.

> 2) In a teaching context, we may want sometimes to show the code. The filter module actually permits to show the output of the code submitted to R (which can include or not the input commands); would it be possible to typeset the submitted code ?

This is an adapted version of the code that I use for my course where I use the vim module to do the typesetting:


\definecolor  [darkblue]   [r=0.1,  g=0.11, b=0.84]
\definecolor  [darkgreen]  [g=0.7]
\definecolor  [lightblue]   [h=87cefa]
\definecolor  [lightgreen]  [g=0.95,b=0.8,r=0.8]



      \startleftbartext[framecolor=darkgreen, backgroundcolor=lightgreen]
        \setupalign[flushleft, verytolerant]
        % range={1,-1} not currently supported
        % See below for details

   [ filtercommand={R CMD BATCH -q --no-timing --save --restore
\externalfilterinputfile\space \externalfilteroutputfile},
      purge=yes, % THE COMMA WAS MISSING


Some text...

    X <- rnorm(200,mean=10,sd=2)
    hist(X, col =  "red3" ,  xlab="Score QI" , main="", ylab="fréquence")



> 3) Finally, using "R CMD BATCH… » as the filtercommand add an extra line at the end of the output file which can be typeset to visualize results. Would it be possible by a magic trick to deleter the last line of the file ?

It is possible to filter lines using `\typefile[range={start, stop}]{filename}`. You can use `range={1,3}` to only include lines 1 upto 3 or `range={1, +2}` to include line 1 followed by 2 lines. What you want is something like `range={1,-1}`, i.e., include from the first to last-1 line, but this is not currently supported. You could ask Hans to do so, as it is relatively simple to add. Of course, you will then need to document this on the wiki.


More information about the ntg-context mailing list