Skip to content
Aditya Mahajan edited this page Mar 27, 2021 · 5 revisions

R

Execute R code from inside ConTeXt

\startbuffer[locale]
   Sys.setlocale(category="LC_ALL", locale="en_US.UTF-8")
\stopbuffer

\defineexternalfilter
  [R]
  [
     filtercommand={R CMD BATCH -q --save --restore \externalfilterinputfile\space \externalfilteroutputfile},
     output=\externalfilterbasefile.out,
     bufferbefore=locale,
     directory=output,
     readcommand=\typefile,
     read=no,
     cache=yes,
   ]

Key features

  • The output files are written to output directory, which must exist beforehand.

  • The environment

      \startR
       ....
      \stopR
    

    executes its contents using R.

  • The command

      \processRfile{...}
    

    runs the specified file throught R.

  • By default, the output is not read back. If you want to typeset the output of the R environment, use

       \startR[read=yes]
        ....
       \stopR
    

The line Sys.setlocale(category="LC_ALL", locale="en_US.UTF-8") sets the locale to UTF, which ensures that accented letters labels in plots work. See this thread on the context mailing list for details.

Clone this wiki locally