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. 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 ? 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 ? Here is a minimal code: % ————————————————— \usemodule[filter] \defineexternalfilter [R] [ filtercommand={R CMD BATCH -q --no-timing --save --restore \externalfilterinputfile\space \externalfilteroutputfile}, output=\externalfilterbasefile.out, directory=output, readcommand=\typefile, read=no, cache=yes, purge=yes ] \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[MyHistogram.pdf][width=.5\textwidth] \stoptext % ————————————————— Sorry for the three questions… and thanks for any help. Fabrice.
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: \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
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. Aditya
On Mon, 15 Jun 2020, Aditya Mahajan wrote:
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.
I can reproduce this output side the filter module. If I directly run os.execute(...) from a lua session, the output is correct but running it through context gives the wrong output. I am guessing this is something to do with locale. I notice that the output of os.execute("locale") from context is different from that from my shell. But I don't know why locale should affect UTF characters in R. Perhaps someone with more knowledge of R can comment on that. \starttext \startluacode lfs.mkdir("output/") \stopluacode \startbuffer[code] pdf("output/MyHistogram.pdf",5,5) X <- rnorm(200,mean=10,sd=2) hist(X, col = "red3" , xlab="Score QI" , main="", ylab="fréquence") \stopbuffer \savebuffer[prefix=no, list=code, file={output/code.r}] \startluacode print(">>>>", "RUNNING R CMD") os.execute("R CMD BATCH --no-timing --save --restore output/code.r output/out") \stopluacode \externalfigure[RPlots/MyHistogram.pdf][width=.5\textwidth] \stoptext Aditya
On Mon, 15 Jun 2020, Aditya Mahajan wrote:
On Mon, 15 Jun 2020, Aditya Mahajan wrote:
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.
I can reproduce this output side the filter module. If I directly run os.execute(...) from a lua session, the output is correct but running it through context gives the wrong output. I am guessing this is something to do with locale. I notice that the output of os.execute("locale") from context is different from that from my shell. But I don't know why locale should affect UTF characters in R.
It is indeed related to locale. Here is a minimal example that works. I use a beforebuffer to set the locale so that you don't have to do it in each snippet and then filter out the first line in the typeset code listing and filter out the first two lines in the output. \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] [ vimcommand=nvim, syntax=r, directory=output, strip=yes, escape=yes, before={\blank[quarterline]\startleftbartext}, after={\stopleftbartext}, ] \define[1]\ShowOutput {\typeRCODEfile[start=2]{\externalfilterinputfile} \startleftbartext[framecolor=darkgreen, backgroundcolor=lightgreen] \setupalign[flushleft, verytolerant] \typefile[range={3,-1}]{#1} \stopleftbartext} \startbuffer[locale] Sys.setlocale(category="LC_ALL", locale="en_US.UTF-8") \stopbuffer \defineexternalfilter [R] [ filtercommand={R CMD BATCH -q --no-timing --save --restore \externalfilterinputfile\space \externalfilteroutputfile}, output=\externalfilterbasefile.out, directory=output, readcommand=\ShowOutput, read=no, bufferbefore=locale, cache=yes, purge=yes, ] \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 Aditya
Dear Aditya,
Le 15 juin 2020 à 01:45, Aditya Mahajan
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 ! 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.
On Mon, 15 Jun 2020, Fabrice L wrote:
Dear Aditya,
Le 15 juin 2020 à 01:45, Aditya Mahajan
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
participants (2)
-
Aditya Mahajan
-
Fabrice L