knitr - inline code chunk of foreign engine - r

I've written a knitr engine to process Maxima code (as part of a package), which works for "regular" chunks just fine, e.g.:
```{maxima}
1+1;
```
results in
(%i1) 1+1;
# (%o1) 2
However, when I try to get the output printed inline, such as
`maxima 1+1;`
It gets printed literally: maxima 1+1;
The R Markdown Cookbook explicitly says
inline: processing output from inline R expressions.
So I guess this is not meant be working (yet), but I wanted to ask here if there is a way to do this/ workaround before filing a feature request at github.

Related

Stop Sweave code chunks in RMarkdown from displaying execution output whilst still displaying the code

I'm writing a publication manuscript for a new R package. The author guidelines expect a self-contained Sweave (Latex+R) project with self-contained executable code within R code chunks in the Sweave document. This allows for seamless reviewing. Recommendations are to use RStudio.
All is going well. However, some of my packaged R code prints to the terminal intermediate steps; notably in the parts that setup and execute parallel code. In terms of use, this is great. However, the intermediate output is bulking out the code chunks in the compiled PDF. Not great for a scientific manuscript with a limited page count (fine elsewhere, e.g., Github wiki etc).
I'm using the code chunk options:
<<eval=T, echo=T>>=
#R code to execute AND to display code here.
#But this print all internal R print() statements to the pdf document.
#
Is there a Sweave code chunk option (not a global option, as for some code chunks the current behaviour is fine) that executes and displays the code itself but halts the printing of any internal print statements in my R package?
In answer to my own question I figured this out through a process of elimination; I could have continued scrolling through online blogs and tutorial but I'm very much pressed for time.
To suppress the output of a calculation in an R code chunk whilst displaying the R code in the compiled pdf:
<<eval=T, echo=T,results=hide>>=
eval=T -- evaluate the code
echo=T -- spits the code into the pdf (and the code output)
results=hide -- overrides echo=T to prevent the code's output whilst maintaining the code display.

Execution of Rcpp chunks in RStudio

The knitr language engines makes it possible to include an Rcpp chunk in your R Markdown document. This works perfectly when knitting the entire document.
However, it doesn't seem to be possible to execute (that is, compile) the Rcpp chunk interactively in RStudio (v. 1.1.364), or am I missing something?
I could keep the C++ code in a separate file and use sourceCpp in a chunk, which also works fine. However, for small examples I use in teaching it's more convenient to have everything in one document. I could then use cppFunction, but that doesn't give proper syntax highlighting.
I'm either looking for an answer that shows that I, indeed, missed how to interactively compile Rcpp chunks in RStudio, or answers that suggest good practices for i) having all code in one file, and ii) being able to execute chunks interactively.

Chunk with eval=FALSE still evaluates on R Sweave

I'm working on some education manuals in spanish for a course, so I'm making a Sweave document with some chunks and I'm trying to make an example of an error message. But first I need to show the souce of that error, so I'm using this code, since I don't want the code evaluating I'm using eval=FALSE:
<<eval=FALSE, error=TRUE,tidy=FALSE>>=
c(1,2 3)
#falta una coma
#
But the code is still evaluating and it's not letting me print the document, giving me this error message
(chunk 306) 5305:7: unexpected numeric constant
Your code is being parsed, not evaluated. If you have current versions of the knitr and evaluate, this should result in a warning in the knitr log, it won't stop the run. I'm using knitr 1.16 and evaluate 0.10.1 and things are fine. See knitr: knitting chunks with parsing errors for a bit more on this.
(BTW, I think you're using knitr, not Sweave. They're different. Sweave can't handle this. If you really are using Sweave, switch to knitr. The switch is not hard, and brings a lot of benefits.)

Write markdown documents with R code that doesn't work, on purpose

I'm experimenting with using Markdown to write homework problems for a course that involves some R coding. Because these are homework sets, I intentionally write code that throws errors;. Is it possible to use Markdown to display R code in the code style without evaluating it (or to trap the errors somehow)?
If you're using R markdown, putting eval=FALSE in the chunk options should work. Or use try(). Or, if you're using knitr as well, I believe that the default chunk option error=FALSE doesn't actually stop the compilation when it encounters an error, but just proceeds to the next chunk (which sometimes drives me crazy).

Using Sexpr{} within \SweaveInput{}

I have this 2 lines of code which I run with R Sweave function.
\SweaveInput{samples.rnw}
\SweaveInput{\Sexpr{args$samples}}
The first line leads to the inclusion of the content of the corresponding file, while the second just causes evaluation of the Sexpr{} term but nothing else.
What I want is both: first let evaluate the Sexpr{} term and afterwards do the inclusion of the respective file content.
How do solve this ?
Thanks
If you use the knitr package, the solution would be simply
<<child='samples.rnw'>>=
<<child=args$samples>>=
#
Sweave is much weaker than knitr in terms of programmability. For example, knitr allows the chunk options to be any valid R expressions, which is the reason why we can write child=args$samples here; knitr will evaluate the chunk options just like function arguments.
BTW, the child option is equivalent to \SweaveInput{}, but I strongly discourage the use of the pseudo LaTeX command. For more about Sweave vs knitr, see http://yihui.name/knitr/demo/sweave/

Resources