# Re: [NTG-context] Filter module for R

```Dear Aditya,

> Le 15 juin 2020 à 01:45, Aditya Mahajan <adit...@umich.edu> a écrit :
>
> This is an adapted version of the code that I use for my course where I use
> the vim module to do the typesetting:
>
> \usemodule[filter]
> \usemodule[vim]
>
> \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]
>
> \defineframedtext
>  [leftbartext]
>  [
>    width=local,
>    frame=off,
>    framecolor=darkblue,
>    leftframe=on,
>    rulethickness=1ex,
>    offset=0.25ex,
>    loffset=3ex,
>    background=color,
>    backgroundcolor=lightblue,
>  ]
>
> \definevimtyping
>  [RCODE]
>  [
>    syntax=r,
>    directory=output,
>    strip=yes,
>    escape=yes,
>    before={\blank[quarterline]\startleftbartext},
>    after={\stopleftbartext},
>  ]
>
> \define[1]\ShowOutput
>    {\typeRCODEfile{\externalfilterinputfile}
>     \startleftbartext[framecolor=darkgreen, backgroundcolor=lightgreen]
>       \setupalign[flushleft, verytolerant]
>       % range={1,-1} not currently supported
>       % See below for details
>       \typefile[range={1,-1}]{#1}
>     \stopleftbartext}
>
>
> \defineexternalfilter
>  [R]
>  [ filtercommand={R CMD BATCH -q --no-timing --save --restore
> \externalfilterinputfile\space \externalfilteroutputfile},
>     output=\externalfilterbasefile.out,
>     directory=output,
>     cache=yes,
>     purge=yes, % THE COMMA WAS MISSING
>   ]
>
> \starttext
>
> Some text...
>
>   pdf("RPlots/MyHistogram.pdf",5,5)
>   X <- rnorm(200,mean=10,sd=2)
>   hist(X, col =  "red3" ,  xlab="Score QI" , main="", ylab="fréquence")
> \stopR
>
> \externalfigure[RPlots/MyHistogram.pdf][width=.5\textwidth]
>
> \stoptext
>
> ```
```

code to work, but this works ! Thanks also for your additional work regarding
the locale setting.. this is really deep magic for me.

I think the facility to run R code directly from context can open ConTeXt to
new users. The alliance between R and LaTeX is a winning combination to a lot
of my colleagues. Do you think this would be appropriate to do a simple page on
the wiki explaining how R code can be done in ConTeXt, with eventual
formatting, with some simple examples, or this is too much noise ?

Thanks. Fabrice.

___________________________________________________________________________________