Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWeave with non-R code chunks?

Tags:

shell

r

sweave

I often use Sweave to produce LaTeX documents where certain chunks are produced dynamically by executing R code. This works well - but is it also possible to have code chunks that are executed in different ways, e.g. by executing the code in the shell, or by running Perl, and so on? It would be helpful to be able to mix things up, so I could do things like run some shell commands to fetch some data, run some perl commands to pre-process it, and then run R commands to analyze it.

Of course I could use all R chunks and use system() as a poor-man's substitute, but that doesn't make for very pleasant reading in the document.

like image 1000
Ken Williams Avatar asked Feb 17 '11 18:02

Ken Williams


People also ask

How do you make a code chunk not run in R?

Chunk Optionsinclude = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file.

How do I hide code chunks in R Markdown?

Hide source code: ```{r, echo=FALSE} 1 + 1 ``` Hide text output (you can also use `results = FALSE`): ```{r, results='hide'} print("You will not see the text output.") ``` Hide messages: ```{r, message=FALSE} message("You will not see the message.") ``` Hide warning messages: ```{r, warning=FALSE} # this will generate ...

What is an R code chunk?

A code chunk is a runable piece of R code. Re-producing the document will re-run calculations. Code chunk technology is beneficial because the risk of mismatch between the commentary in a paper and the results being discussed is reduced.

How do I add code chunks in R Markdown?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS). There are a large number of chunk options in knitr documented at https://yihui.name/knitr/options.


2 Answers

The new new thing (for multi-language, multi-format) docs may be dexy.it which for example these guys at opengamma.org use as the backend.

Ana, who is behind dexy, is also giving a lot of talks about it so also look at the dexy blog.

like image 155
Dirk Eddelbuettel Avatar answered Oct 09 '22 05:10

Dirk Eddelbuettel


It's not directly related to Sweave, but org-babel, which is part of Emacs org-mode, allows to mix code chunks of different languages in one file, pass data from one chunk to another, execute them, and generate LaTeX or HTML export from the output.

You can find more informations about org-mode here :

http://www.orgmode.org/

And to see how org-babel works :

http://orgmode.org/worg/org-contrib/babel/

like image 32
juba Avatar answered Oct 09 '22 06:10

juba