knitr - How to align code and plot side by side - r

is there a simple way (e.g., via a chunk option) to get a chunk's source code and the plot it produces side by side, as on page 8 (among others) of this document?
I tried using out.width="0.5\\textwidth", fig.align='right', which makes the plot correctly occupy only half the page and be aligned to the right, but the source code is displayed on top of it, which is the normal behaviour.
I would like to have it on the left side of the plot.
Thanks
Sample code:
<<someplot, out.width="0.5\\textwidth", fig.align='right'>>=
plot(1:10)
#

Well, this ended up being trickier than I'd expected.
On the LaTeX side, the adjustbox package gives you great control over alignment of side-by-side boxes, as nicely demonstrated in this excellent answer over on tex.stackexchange.com. So my general strategy was to wrap the formatted, tidied, colorized output of the indicated R chunk with LaTeX code that: (1) places it inside of an adjustbox environment; and (2) includes the chunk's graphical output in another adjustbox environment just to its right. To accomplish that, I needed to replace knitr's default chunk output hook with a customized one, defined in section (2) of the document's <<setup>>= chunk.
Section (1) of <<setup>>= defines a chunk hook that can be used to temporarily set any of R's global options (and in particular here, options("width")) on a per-chunk basis. See here for a question and answer that treat just that one piece of this setup.
Finally, Section (3) defines a knitr "template", a bundle of several options that need to be set each time a side-by-side code-block and figure are to be produced. Once defined, it allows the user to trigger all of the required actions by simply typing opts.label="codefig" in a chunk's header.
\documentclass{article}
\usepackage{adjustbox} %% to align tops of minipages
\usepackage[margin=1in]{geometry} %% a bit more text per line
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
## These two settings control text width in codefig vs. usual code blocks
partWidth <- 45
fullWidth <- 80
options(width = fullWidth)
## (1) CHUNK HOOK FUNCTION
## First, to set R's textual output width on a per-chunk basis, we
## need to define a hook function which temporarily resets global R's
## option() settings, just for the current chunk
knit_hooks$set(r.opts=local({
ropts <- NA
function(before, options, envir) {
if (before) {
ropts <<- options(options$r.opts)
} else {
options(ropts)
}
}
}))
## (2) OUTPUT HOOK FUNCTION
## Define a custom output hook function. This function processes _all_
## evaluated chunks, but will return the same output as the usual one,
## UNLESS a 'codefig' argument appeared in the chunk's header. In that
## case, wrap the usual textual output in LaTeX code placing it in a
## narrower adjustbox environment and setting the graphics that it
## produced in another box beside it.
defaultChunkHook <- environment(knit_hooks[["get"]])$defaults$chunk
codefigChunkHook <- function (x, options) {
main <- defaultChunkHook(x, options)
before <-
"\\vspace{1em}\n
\\adjustbox{valign=t}{\n
\\begin{minipage}{.59\\linewidth}\n"
after <-
paste("\\end{minipage}}
\\hfill
\\adjustbox{valign=t}{",
paste0("\\includegraphics[width=.4\\linewidth]{figure/",
options[["label"]], "-1.pdf}}"), sep="\n")
## Was a codefig option supplied in chunk header?
## If so, wrap code block and graphical output with needed LaTeX code.
if (!is.null(options$codefig)) {
return(sprintf("%s %s %s", before, main, after))
} else {
return(main)
}
}
knit_hooks[["set"]](chunk = codefigChunkHook)
## (3) TEMPLATE
## codefig=TRUE is just one of several options needed for the
## side-by-side code block and a figure to come out right. Rather
## than typing out each of them in every single chunk header, we
## define a _template_ which bundles them all together. Then we can
## set all of those options simply by typing opts.label="codefig".
opts_template[["set"]](
codefig = list(codefig=TRUE, fig.show = "hide",
r.opts = list(width=partWidth),
tidy = TRUE,
tidy.opts = list(width.cutoff = partWidth)))
#
A chunk without \texttt{opts.label="codefig"} set...
<<A>>=
1:60
#
\texttt{opts.label="codefig"} \emph{is} set for this one
<<B, opts.label="codefig", fig.width=8, cache=FALSE>>=
library(raster)
library(RColorBrewer)
## Create a factor raster with a nice RAT (Rast. Attr. Table)
r <- raster(matrix(sample(1:10, 100, replace=TRUE), ncol=10, nrow=10))
r <- as.factor(r)
rat <- levels(r)[[1]]
rat[["landcover"]] <- as.character(1:10)
levels(r) <- rat
## To get a nice grid...
p <- as(r, "SpatialPolygonsDataFrame")
## Plot it
plot(r, col = brewer.pal("Set3", n=10),
legend = FALSE, axes = FALSE, box = FALSE)
plot(p, add = TRUE)
text(p, label = getValues(r))
#
\texttt{opts.label="codefig"} not set, and all settings back to ``normal''.
<<C>>=
lm(mpg ~ cyl + disp + hp + wt + gear, data=mtcars)
#
\end{document}

I see 3 possibilities
for beamer presentations, I'd go for \begin{columns} ... \end{columns} as well.
If it is only one such plot: Minipages
Here I used a table (column code and column result). (This example is "normal" Sweave)
For all three, the chunk options would have include = FALSE, and the plot would "manually" be put to the right place by \includegraphics[]{}.

You can display the text in a 'textplot' from package PerformanceAnalytics or gplots.
(Little) downside: To my knowledge there is no Syntax highlighting possible.
Sample Code:
```{r fig.width=8, fig.height=5, fig.keep = 'last', echo=FALSE}
suppressMessages(library(PerformanceAnalytics))
layout(t(1:2))
textplot('plot(1:10)')
plot(1:10)
```

Related

I have a for loop of qplots, how can you structure the plots individually using LaTeX code e.g adding captions to the plots

\begin{figure}[h]
\centering
<<echo=FALSE>>=
for(i in 2:38){
print(my_plot_function(i))
}
#
\end{figure}
This is my code, but what is happening is that when I compile my PDF I only get the first two plots that fit on the first page and I do not get the rest of the plots.
I would like to have all of the plots on separate pages.
And how would I go about adding captions to each individual plot in the for loop.
\documentclass{article}
\begin{document}
<<echo = FALSE, fig.cap = c("First, Second, Third"), results = "asis">>=
library(ggplot2)
for (i in 1:3) {
print(qplot(x = 1, y = i))
cat("Arbitrary \\LaTeX code! \\clearpage")
}
#
\end{document}
You can use the chunk option fig.cap to add captions to your plots. Note that this will wrap your figure in a figure environment, turning it into a float.
Use the chunk option results="asis" to be able to print arbitrary text (including LaTeX markup) into your document using cat().

knitr: add to previous plot in new code chunk

I am using the knitr package for R to produce a LaTeX document combining text with embedded R plots and output.
It is common to write something like this:
We plot y vs x in a scatter plot and add the least squares line:
<<scatterplot>>=
plot(x, y)
fit <- lm(y~x)
abline(fit)
#
which works fine.
(For those not familiar with knitr or Sweave, this echos the code and output in a LaTeX verbatim environment and also adds the completed plot as a figure in the LaTeX document.)
But now I would like to write more detailed line-by-line commentary like:
First we plot y vs x with a scatterplot:
<<scatterplot>>=
plot(x, y)
#
Then we regress y on x and add the least squares line to the plot:
<<addline>>=
fit <- lm(y~x)
abline(fit)
#
The problem is that there are now two knitr code chunks for the same plot. The second code chunk addline fails because the plot frame created in the first code chunk scatterplot is not visible to the code in the second code chunk. The plotting window doesn't seem to be persistent from one code chunk to the next.
Is there any way that I can tell knit() to keep the plot window created by plot() active for the second code chunk?
If that is not possible, how else might I achieve LaTeX-style commentary on code lines that add to existing plots?
One Day Later
I can now see that essentially the same question has been asked before, see:
How to build a layered plot step by step using grid in knitr? from 2013 and
Splitting a plot call over multiple chunks from 2016.
Another question from 2013 is also very similar:
How to add elements to a plot using a knitr chunk without original markdown output?
You can set knitr::opts_knit$set(global.device = TRUE), which means all code chunks share the same global graphical device. A full example:
\documentclass{article}
\begin{document}
<<setup, include=FALSE>>=
knitr::opts_knit$set(global.device = TRUE)
#
First we plot y vs x with a scatterplot:
<<scatterplot>>=
x = rnorm(10); y = rnorm(10)
plot(x, y)
#
Then we regression y and x and add the least square line to the plot:
<<addline>>=
fit <- lm(y~x)
abline(fit)
#
\end{document}
You can show code without evaluating it by adding the chunk option eval=FALSE. If you only want to show the final version of the plot with the regression line added, then use eval=FALSE for the first plot(x,y).
Then we add two chunks for the regression line: One is the complete code needed to render the plot, but we don't want to display this code, because we don't want to repeat the plot(x,y) call. So we add a second chunk that we echo, to display the code, but don't evaluate.
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r data}
set.seed(10)
x=rnorm(10)
y=rnorm(10)
```
First we plot y vs x with a scatterplot:
```{r scatterplot, eval=FALSE}
plot(x, y)
```
Then we regress y on x and add the least squares line to the plot:
```{r addline, eval=FALSE}
fit <- lm(y~x)
abline(fit)
```
```{r echo=FALSE}
plot(x,y)
fit <- lm(y~x)
abline(fit)
```
Here's what the output document looks like:

knitr adds an empty figure with ssplot from seqHMM package

I have the following chunk in RStudio:
<<sumfig,dependson='data',fig.cap="Summary of sequences">>=
ssplot(smult)
#
ssplot is a function in seqHMM package which creates a frequency graph and smult is my sequence data.
When I run my code, I get two figures in my pdf: The first one is an empty white figure with label {fig:sumfig1} and the second one is the real figure with label {fig:sumfig1}. I have similar experience with other plots from this package. I also have some other graphs in my file from other packages which work just fine.
Is it something wrong with the package or I am doing something wrong?
The root of this issue seems to be seqHMM:ssplot, not knitr: Even in an interactive sesion, ssplot generates two plots, an empty one and the actual plot.
If there is only one plot generated in the chunk with ssplot, the chunk option fig.keep = "last" can be used to disregard the first plot and show only the second (last) one.
\documentclass{article}
\begin{document}
<<echo = FALSE, message = FALSE, fig.keep = "last">>=
library(seqHMM)
# from ?ssplot
data("biofam3c")
# Creating sequence objects
child_seq <- seqdef(biofam3c$children, start = 15)
marr_seq <- seqdef(biofam3c$married, start = 15)
left_seq <- seqdef(biofam3c$left, start = 15)
## Choosing colors
attr(child_seq, "cpal") <- c("#66C2A5", "#FC8D62")
attr(marr_seq, "cpal") <- c("#AB82FF", "#E6AB02", "#E7298A")
attr(left_seq, "cpal") <- c("#A6CEE3", "#E31A1C")
# Plotting state distribution plots of observations
ssplot(list("Children" = child_seq, "Marriage" = marr_seq,
"Residence" = left_seq))
#
\end{document}
As of knitr 1.14 (the current development version, available on GitHub), you can also use fig.keep to specify which plots exactly you want to keep: fig.keep = c(1,3) will keep the first and the third plot.

How to place xtable object to the left side of page

Question: How to place xtable object to the left side of page or how to disable centering globally.
I'm struggling to figure out how to place xtable object on the left side. I have got a *.Rmd file and all this goes to the relevant r chunk.
require(xtable)
df <- data.frame(x=seq(1,10,1),y=rnorm(10))
Xtab <- xtable(df, digits=0, caption="\\textbf{MINIMAL/IDEAL}",
floating=FALSE, latex.environments = c("left"))
print(Xtab, size = "small", include.colnames=FALSE)
I have included the following after reading various sources (print.xtable; xtable manual etc.)
1) floating=FALSE
2) latex.environments = c("left")
I have searched SO and used some of the hints but all failed.
It seems to make quite a difference whether you pass a parameter to xtable() or to print(xtable()). The following chunk creates a table according to your data that is aligned at the left of the page in the pdf file.
```{r, results='asis',echo=FALSE}
library(xtable)
df <- data.frame(x=seq(1, 10, 1),y = rnorm(10))
print(xtable(df,digits=0, caption="\\textbf{MINIMAL/IDEAL}"), include.colnames=FALSE, size = "small", comment=FALSE, latex.environments="flushleft")
```
However, as you can see, the caption remains at the center of the page.

Reusing chunks in Knitr

I'm having lots of fun with Knitr but noticed I am reusing code in a bad way - cut and paste. In my example I want to load a dataset, calculate some statistics and print those, and plot the dataset -- easy to do with a few chunks, but if I want to do the same thing with another dataset I have to copy and paste the chunks and change only the name of the dataset.
Suppose I have something like this :
<p>Load the dataset <tt>dataset01</tt></p>
<!--begin.rcode load-dataset01
# Create an alias so there is no need to change it several times in the
# chunks
myDataset <- dataset01
a <- calcSomeStats(myDataset)
input <- myDataset[,1:2]
ideal <- class.ind(myDataset$label)
end.rcode-->
<p>Now let's plot it</p>
<!--begin.rcode plot-dataset01, fig.width=10, fig.height=10
neurons <- 1
NNET = nnet(input, ideal, size=neurons,softmax=TRUE)
plotnet(NNET)
par(pty="s",xpd=T, mar=par()$mar+c(0,0,0,2))
axis(1, at = seq(bbox[1],bbox[2], by = 2), las=1)
axis(2, at = seq(bbox[1],bbox[2], by = 2), las=2)
points(myDataset$x,myDataset$y,
col=myPal[unclass(myDataset$label)],cex=2,pch=16)
legend("topright", levels(factor(myDataset$label)),fill=myPal,inset=c(-0.1,0))
end.rcode-->
Code is not really complete, there are other parts that I am still developing, but it is working.
My question is, considering the two chunks shown as code above, which is the best (or Riest) way to reuse it? Suppose I have a list of dozens of datasets and I want to run the same chunks on them, is possible even substituting the non-R, HTML parts. Is it possible?
I've naively tried to create a function but since it starts with this:
<!--begin.rcode
abc <- function(n)
{
<!--begin.rcode howdoInamethischunkwithanuniquename
n <- n*2
end.rcode-->
}
end.rcode-->
it did not work (error: unexpected end of input)
thanks
Rafael
Edit: there are similar questions with answers in Using loops with knitr to produce multiple pdf reports... need a little help to get me over the hump and https://github.com/yihui/knitr/issues/435 but they cover LaTeX and/or R markdown, not HTML.
Another edit: things I've tried after #Yuhui comment:
Using the same label for both chunks
<!--begin.rcode chunkA, echo=TRUE, results='hide'
x <- rnorm(100)
end.rcode-->
<p>Plot it?</p>
<!--begin.rcode chunkA, echo=FALSE, results='markup'
mean(x)
end.rcode-->
With this I get the "Error in parse_block(g[-1], g[1], params.src) : duplicate label 'chunkA'" message.
Using chunk option ref.label
<!--begin.rcode chunkA, echo=TRUE, results='hide'
x <- rnorm(100)
end.rcode-->
<p>Plot it?</p>
<!--begin.rcode chunkB, ref.label='chunkA', echo=FALSE, results='markup'
mean(x)
end.rcode-->
With this I get the R code (x <- rnorm(100)), "Plot it?" and then nothing. Changing echo to TRUE just repeat (x <- rnorm(100)).
More information
My scenario is having several small data frames that have the same structure (x,y,label) and I want to process them in a chunk "A" and plot them with similar parameters in another chunk "B". If I do this without reusing code, I have to copy-and-paste chunks "A" and "B" several times, which is not a really good idea.
I know I cannot pass a parameter to a HTML chunk, and the recipes at http://yihui.name/knitr/demo/reference/ seems close to what I need, but I cannot figure out how to do them in R+HTML.
OK, I got it, and am posting this to serve as an example.
From what I understand, it is not possible to create a knitr chunk that works as a function. So, this is not possible:
<!--begin.rcode fakeFunction
# do something with myData, assume it is defined!
end.rcode-->
<!--begin.rcode myPlot1 ref.label='fakeFunction'
myData <- iris
# Assume fakeFunction will be executed somehow with iris
end.rcode-->
<!--begin.rcode myPlot2 ref.label='fakeFunction'
myData <- cars
# Assume fakeFunction will be executed somehow with cars
end.rcode-->
What will work is something like this:
<!--begin.rcode
myData <- iris
end.rcode-->
<!--begin.rcode plot
summary(myData)
end.rcode-->
<!--begin.rcode
myData <- cars
end.rcode-->
<!--begin.rcode plot2, ref.label='plot'
end.rcode-->
Basically we're saying that chunk plot2 will "paste" the code from chunk plot. We don't need to define anything else in plot2, and I guess it will be ignored anyway.
I haven't figured out a detail, though. Suppose I have the chunk plot working OK (imagine dozens of lines of R code) and want a slight different behavior in plot2, that would impact a single line of code. From what I understand I won't be able to do this with knitr -- anyone knows how to reuse code by writing chunks as procedures or functions?
I got similar ERRORs.
What I did was to name the chunk differently or not name them at all.
For example
{r, echo=F }
some code here
this is an example of a default code chunk without a name
{r setup, echo=F }
some code here
this is a chunk with name "setup."
Basically, if you have all chunk unnamed, or have all different named chunk, youll be fine.

Resources