Include same chunk twice with different paramters - r

I have a long .Rnw document which consists mostly of text (typeset in LaTeX) with a few chunks here and there. I have also written a chunk which outputs a specific figure. The figure contains a plot, the values for the plot are currently read from a .csv file and some parameters like colors defined manually within the chunk.
Now I want to have the same figure in a different place in the document, but with different values for the plot and a few other parameters different. Ideally, I would like to include the chunk as a child twice, and pass parameters to it somehow, including the name of the .csv to be used for the plot values. I would hate to copy paste the chunk code with hardcoded parameters, as it is complex enough that potential changes will be difficult to synchronize.
How can I do such "parameterized reuse" of chunks?
update
As requested, a small example
This is saved as include-chunk-reuse.Rnw
<<toReuse, echo=FALSE, result='asis'>>=
l <- 25
#
\newlength{\mylength}
\setlength{\mylength}{\Sexpr{l}pt}
%Omitted: a lot of complicated LaTeX commands
\rule{\mylength}{1pt}
This is the document which is supposed to reuse the chunk. It doesn't even compile, as it complains that the same label is used twice: Error in parse_block(g[-1], g[1], params.src) : duplicate label 'toReuse'
\documentclass{article}
\begin{document}
This is some text. And now comes a 25 pt wide line.
<<first-figure, child='include-chunk-reuse.Rnw'>>=
#
This is some text. The next line is also 25 pt wide. But I would like to call the chunk in a way which makes it 50 pt wide instead.
<<second-figure, child='include-chunk-reuse.Rnw'>>=
#
\end{document}

For the knitr part to work simply leave out the chunk-name in the child document, then you don't have the duplicated label and the knitr part works.
Passing Parameters does not really work as far as I know, but you can just set a global variable before including the child. (For example \Sexpr{l <- 200}
You are still redefining \mylength which is why LaTeX will throw an error, so move the first definition of \mylength from the child to the main document.

The example below demonstrates two ways to reuse and parametrize a chunk.
Reusing Chunks
The mechanism is explained here. Basically, the simplest way to reuse a chunk is to add another empty chunk with the same label. Alternatively, the chunk option ref.label lets a chunk inherit another chunks code.
Both approaches of reusing chunks are largely equivalent – with one exception: figures generated in chunks are saved as chunklabel-i.pdf, where i is the figure index counted by chunk. Therefore, if a chunk is reused by repeating its label, figure i from the second use will overwrite figure i from the first use. This is the reason why I use ref.label (and thus distinct chunk labels) in the example below (otherwise, the points on both plots would be green).
In the example below, I used eval = FALSE in order to prevent evaluation of the masterchunk where it is defined. An alternative would be to externalize the chunk and read it by read_chunk().
Parameterizing Chunks
The two most straightforward options to "pass" parameters to a chunk are
chunk options and
global variables
Also when reusing chunks, each use can set different chunk options. The example below exploits this to set different captions.
As all chunks run in the same environment, setting a variable in an early chunk affects subsequent chunks accessing this variable. In the example below, mycolor is modified this way.
\documentclass{article}
\begin{document}
<<masterchunk, eval = FALSE>>=
plot(1:10, col = mycolor)
#
<<config1>>=
mycolor <- "red"
#
<<use1, ref.label = "masterchunk", fig.cap = "Red dots">>=
#
<<config2>>=
mycolor <- "green"
#
<<use2, ref.label = "masterchunk", fig.cap = "Green dots">>=
#
\end{document}

Related

With knitr, preserve chunk options when purling chunks into separate files

For teaching purposes, I would like to purl the chunks of my .Rnw file into separate files. This answer explains how to do it:
How to purl each chunks in .Rmd file to multiple .R files using Knitr
BUT the method does not preserve the chunk options. Since the chunks I have produce plots, it's important to preserve the fig.width and fig.height options. Ideally I would like a chunk that looks like this:
<<plot, fig.width = 3, fig.height = 5, outwidth = '.75\\textwidth'>>=
plot (1,1)
#
to become a file named plot.R that looks like this:
#+ fig.width = 3, fig.height = 5
plot (1,1)
That is, turn the chunk options fig.width and fig.height into a format that will be recognized by spin(), as purl() does, and get rid of the chunk options that are irrelevant, or create problems for spin() into Word, such as out.width. All in the spirit of creating code files that are user-friendly.
Since the answer you refer to doesn't copy the header line from the results of purl, you lose everything besides the chunk name. While you could adapt it to paste in the headers, it's actually not hard to build a function to parse the output of purl—much easier than trying to parse a Rmd or Rnw document, anyway, and easier than sorting out exactly how knitr does so.
purl_chunks <- function(input_file){
purled <- knitr::purl(input_file) # purl original file; save name to variable
lines <- readLines(purled) # read purled file into a character vector of lines
starts <- grep('^## ----.*-+', lines) # use grep to find header row indices
stops <- c(starts[-1] - 1L, length(lines)) # end row indices
# extract chunk names from headers
names <- sub('^## ----([^-]([^,=]*[^,=-])*)[,-][^=].*', '\\1', lines[starts])
names <- ifelse(names == lines[starts], '', paste0('_', names)) # clean if no chunk name
# make nice file names with chunk number and name (if exists)
file_names <- paste0('chunk_', seq_along(starts), names, '.R')
for(chunk in seq_along(starts)){ # loop over header rows
# save the lines in the chunk to a file
writeLines(lines[starts[chunk]:stops[chunk]], con = file_names[chunk])
}
unlink(purled) # delete purled file of entire document
}
A couple notes:
While the regex works for what I've thrown at it, it may yet be fallible. Tested:
no chunk name
no name but chunk settings
name with hyphens
single character names
names with spaces, including after (before the comma/brace)
As .Rnw and .Rmd files both purl the same, it works for either.
It uses the default setting (1L) for purl's documentation parameter. 0L wouldn't have chunk headers and is thus pointless here anyway, but it would handle 2L (which would include text chunks as roxygen comments) stupidly. It could be rebuilt for such, though, if you wanted text chunks with the following code chunk.
While the output header lines of purl don't look exactly like your example above (they start with ## ---- and are filled with hyphens), they spin properly; results obey chunk options.

highlight all inline \Sexpr{} outputs in the generated pdf

I am using knitr for a report wherein I have a lot of inline output text, mostly numeric values, using \Sexpr{}. I want to highlight All these inline outputs in my generated pdf.
Example code:
\documentclass[12pt]{article}
\begin{document}
<<echo=FALSE, include=FALSE>>=
N <- 100 # Total
N_f <- 60 # Women
#
There were \Sexpr{N} people in the company, \Sexpr{N_f} women and \Sexpr{N - N_f} men.
\end{document}
Hence, in the output all the number should be highlighted, i.e. with a shaded background (similar to using with \hl{} with the \usepackage{soul}).
It seems to me that the solution would use one of the inline output hooks. Another possibility might be to write a LaTeX function which search all the \Sexpr{...} expressions in the entire document and highlights them in the generated pdf. I am still learning and can not figure out how to implement these.
Thanks for any help or hints.
Note: The knitr page by yihui talks about manipulation of the numeric value (scientific notation, digits after decimal points) which I have got covered.
The output hook inline can be used to style output from \Sexpr{}. This is as simple as
knit_hooks$set(inline = function(x) { sprintf("\\textbf{%s}", x)})
Just define an arbitrary function that takes an argument x and returns the string to be printed. In this example I used \textbf to make the output bold, but this can be extended to any LaTeX commands.
In this answer, Yihui suggests an improvement that still takes the default inline hook into account. This ensures rounding as usually performed by the default hook:
hook_inline <- knit_hooks$get('inline')
knit_hooks$set(inline = function(x) { sprintf("\\textbf{%s}", hook_inline(x))})

inline Latex code inside knitr R block

I am looking for a way to put inline latex code into a R code chunk in Knitr.
Here is my example code from the knitr example site :
\documentclass{article}
\begin{document}
Example text outside R code here; we know the value of pi is \Sexpr{pi}.
<<my-label, echo=FALSE, eval=TRUE>>=
set.seed(1213) # for reproducibility
x = cumsum(rnorm(100))
m <- mean(x) # mean of x
print(m)
cat(m)
plot(x, type = 'l') # Brownian motion
#
\textit{Mean is :} \textbf{\Sexpr{m}}
\end{document}
For something simple like this is I could use result='asis' but for a more complicated piece of code, where you want to periodically write the result out to the document, (especially you have complex ggplot graphs), that solution does not work very well.
In the given example, I have 3 queries :
How would I use inline latex code for the output from line 8, in case I wanted to color, bold etc. that text.
Can one eliminate the grey box that appears when we use the cat or print command.
Can the numbering which appears with the print command, which is eliminated with the cat command be eliminated for the print command as well, since print has many variants in many packages for data frames data tables etc. and might be more commonly used to print a portion of data.
In summary, I am mainly looking for the inverse of line 12 in the code.
I have also unsuccessfully tried knit_print with printr, and asis_output, in lieu of print. Although I may have been incorrectly using them.
Thanks!

A Way in Knitr to Copy a Chunk?

Knitr Mavens,
Background: Using knitr to report a report with many embedded graphs. In the body of the report, all that's appropriate is the graph, not the code.
For example:
```{r graph_XYZ_subset, echo = FALSE, message = TRUE,
fig.cap = "Text that explains the graph"}
graph.subset <- ggplot() + ...
```
This part works just fine.
However, there is a need to display the key parts of the code (e.g., key statistical analyses and key graph generations)...but in an Addendum.
Which leads to this question: is there a way to copy a knitr chunk from the early parts of a script to a later part?
To ensure accuracy, it's ideal that the code in the Addendum list (display) all the code that was actually executed in the report.
For example:
# ADDENDUM - Code Snippets
### Code to Generate Subset Graph
\\SOMEHOW COPY the code from graph_XYZ_subset to here without executing it.
### Code to Compute the Mean of Means of the NN Factors
\\Copy another knitr chunk which computes the mean of means, etc.
### And So On...
\\Copy chunks till done
* * * * * * * *
Any ideas? Is there a way in knitr to perform these types of chunk copies?
There are several options, four of them listet and shortly explained below. Yihui's explanations in How to reuse chunks might also help.
\documentclass{article}
\begin{document}
\section{Output}
<<mychunk, echo = FALSE>>=
print("Hello World!")
#
\section{Source code}
Option 1: Use an empty chunk with the same label.
<<mychunk, eval = FALSE>>=
#
Option 2: Embed in other chunk (no advantage in this case). Note that there is no equality sign and no at for the inner chunk.
<<myOtherChunk, eval = FALSE>>=
<<mychunk>>
#
Option 3: Use \texttt{ref.label}.
<<ref.label = "mychunk", eval = FALSE>>=
#
Option 4: Define the chunk in an external file you read in using \texttt{read\_chunk}. Then use Option 1--3 to execute the chunk (with \texttt{eval = TRUE}; default) or show it's code (with \texttt{eval = FALSE}).
\end{document}
I usually prefer Option 4. This allows you to separate the programming logic from writing the document.
At the place mychunk is to be exectued and the graph will appear in the PDF, you only have <<mychunk>>= in your Rnw file and don't have to bother with all the code that generates your graph. Developing your code is also easier, because in an interactive session you have all your code at one spot and don't have to scroll through all the text of the report when going from one chunk to the next one.
EDIT:
The options mentioned above have in common that you need to manually maintain a list of the chunks to show in the appendix. Here two options to avoid this; unfortunately, both have some drawbacks:
Option 1: Automatically create a list of all chunks that have been executed and show their code.
This can be achieved using a chunk hook that registers all chunk names. Include the following chunk before all other chunks in the document:
<<echo = FALSE>>=
library(knitr)
myChunkList <- c()
listChunks <- function(before, options, envir) {
if (before) {
myChunkList <<- c(myChunkList, options$label)
}
return(NULL)
}
knit_hooks$set(recordLabel = listChunks) # register the new hook
opts_chunk$set(recordLabel = TRUE) # enable the new hook by default
#
Where you want to show the code (for example in the appendix), insert the following chunk:
<<showCode, ref.label = unique(myChunkList), eval = FALSE>>=
#
Unfortunately, there will be no margin or any other visual separation between the chunks.
Option 2: Using the chunk hook is not always necessary because there is the function all_labels() that returns a list of all chunk labels. However, there might be chunks in your file that don't get executed and you probably don't want to see their code. Moreover, option 1 allows skipping certain chunks simply by setting recordLabel = FALSE in their chunk options.

Referring to other figures from within a chunk

Inside the plot generated in a chunk in a knitr/LaTeX document I want to position a text string with a reference to a particular figure elsehwere in the document, something like "See Fig. 10", based on the label of a that particular figure, lets say "fig:sim1". So I wrote the following R function which reads the .aux file of the LaTeX-document I'm writing and extracts the counter of the figure matching a specific label
ref <- function(label) {
lines <- scan("mismatch-final.aux","character",sep="\n")
line <- grep(paste("\\{",label,"\\}",sep=""),lines,value=TRUE)
strsplit(strsplit(line,"\\{\\{")[[1]][2],"\\}\\{")[[1]][1]
}
I can then generate the desired text string from within the chunk and place it inside the plot with something like
text(5,10,paste("See Fig.~",ref("fig:sim1")))
This should work but perhaps there is a cleaner way of doing this? Also, is there a way to get at the .aux filename from within the chunk such that the ref function above would be made more general?
Another way to do that is to use the tikzDevice package and TikZ plots, so you can write raw LaTeX expressions in your plot, e.g
\begin{figure}
....
\caption{Simulation of something. \label{fig:sim1}}
\end{figure}
<<test, dev='tikz', external=FALSE>>=
plot(rnorm(10), rnorm(10))
text(0, 0, 'See Fig \\ref{fig:sim1}')
#
Depending on the complexity of your plot, this chunk may take a few seconds to compile each time. The plot is saved as a LaTeX file, and any LaTeX commands will work in it.

Resources