Knitr - Define Latex commands that call R functions using Sexpr - r

I'm basically wondering how to define a new Latex command such that it allows the nesting of Sexpr and some other R function, where the Latex argument is an R object.
As fortunute happenstance, the idea somewhat is transmitted by the new command structure given below:
\newcommand{\SomeLatexCommand}[1]{\Sexpr{"#1"}}
Where fortunately the argument is indeed shown, albeit in string. With this in mind, I was hoping upon the following command:
\newcommand{\SweetLatexCommand}[1]{\Sexpr{SomeRFunction(get("#1"))}}
However, once inside nested inside an R function, #1 is not read as a placeholder for the Latex argument, but instead as an existing R variable.
Is there a way to make the last comand work? Or else, are there also other neat ways to define Latex commands which in turn can call on any R function through R objects?
Good day,

No, you can't do that. The problem is the way knitr works:
R runs the knit() function (or some other knitr function). That function looks through the source for code chunks and \Sexpr calls, executes them, and replaces them with the requested output, producing a .tex file.
Then LaTeX processes that .tex file. R is no longer involved.
Since \newcommand is a LaTeX command, it is only handled in the final stage, after all R evaluation is done.
There may be a way in knitr to specify another "macro" that works the way \Sexpr works, but I don't think there's a way to have several of them.
So what you should do is write multiple functions in R, and call those to do what you want, as \Sexpr{fn1(...)}, \Sexpr{fn2(...)}, etc.
I suppose if you were really determined, you could add an extra preprocessor stage at the beginning, that went through your Rnw file and replaced all strings that looked like \SweetLatexCommand{blah} with \Sexpr{SomeRFunction(get("blah"))} and then called knit(), but that seems like way too much work.

Related

is it possible to compile R latex via knitr in a modula way

Is there any way to compile knitr subfiles separately? What I have in mind is something like the package subfiles for latex just in combination with R/knitr/Sweave?
This would be great in case one has two exercises a first exercise with heavy computations and
don't want to compile the entire exercise always while working and testing the second one.
The patchDVI package does this for Sweave. I imagine it would be possible (maybe even easy) to modify it to do the same for knitr.
For example, in Sweave, you define variables in a chunk like so:
<<>>=
.TexRoot <- "main.tex"
.SweaveFiles <- c("subfile1.Rnw", "subfile2.Rnw")
#
and after Sweave is finished running that file, patchDVI will check whether the files subfile1.Rnw and subfile2.Rnw also need to be run, then will run LaTeX on the main.tex file once everything is up to date.
You don't need to do anything difficult, just use the cache options. Lots of details here, but it's probably as simple as specifying cache = T in the chunk options of your first exercise.

Passing arguments to interactive mode

I need to understand an R script. Since I did not use R until now, I try to understand the script step by step. At the beginning of the script command line arguments (input files) are passed with commandArgs(). I know that one can access additional arguments for an R script with commandArgs().
But I just cannot figure out how to run a script with arguments in the interactive mode, so that I can print all variables used in the script later on. For example source("script.R") does not seem to take arguments.
My apologies if I am just not capable of using the right search query...
I think you're misunderstanding the use of commandArgs - it's for getting the arguments supplied when run through the command line... not the interpreter. If you just want to "supply arguments" when sourcing a file then just put those into the global namespace (just create the variables you want to use). Using source is almost just like copying the script and pasting it into the interpreter.

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).

Dribble in R: how to duplicate all i/o into a file?

Is there something like the Common Lisp function DRIBBLE or Unix command tee in R?
Specifically, I want everything I type and everything R prints back to me to be appended to a file (tee only captures stdout; I want everyting: errors, warnings, print, cat, my input).
I found a 10 year old message on the subject which offers a weak version of that (it does not capture the output from cat/print).
The standard function sink only captures (not duplicates) the R's output; and it does not capture my input.
Is there a better way?
Look at the txtStart function (and related functions) in the TeachingDemos package. I think that it does everything you want except capturing errors (and the TaskCallback system in the R guts needs to be updated for that to happen).
The other option is to run R inside of another environment such as ESS (inside of Emacs) (there are others, but I am less familiar with them). Then everything is captured in the editor/buffer and can be saved to a file.
Note that the sink function does have a split argument that works like tee to show the output on screen as well as duplicate it to the file, but it still only does the output, not the input commands.
You probably want sink() -- see help(sink) for examples.

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