[NTG-context] Filter module for R
Aditya Mahajan
adityam at umich.edu
Mon Jun 15 20:06:49 CEST 2020
On Mon, 15 Jun 2020, Fabrice L wrote:
> Dear Aditya,
>
>> Le 15 juin 2020 à 01:45, Aditya Mahajan <adityam at 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,
>> readcommand=\ShowOutput,
>> read=no,
>> cache=yes,
>> purge=yes, % THE COMMA WAS MISSING
>> ]
>>
>> \starttext
>>
>> Some text...
>>
>> \startR[read=yes]
>> 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
>>
>>
>
>
> Thanks so much for your answer. I had (of course) to install nvim in order the code to work, but this works !
Sorry, that was a leftover. I was testing in a docker container where I did not have vim installed. This will also work with regular vim.
> 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 ?
Definitely. I think there is already a short page. But it will be good to add more detailed instructions.
Aditya
More information about the ntg-context
mailing list