2
votes

I am writing a small exercise book with Markdown. I would like to have the final output with the plots on a column and the document text on the other. Similar problems are addressed here and here. Unfortunately, they mainly desire one output per column. I would like to produce the output on a column and the text on the other. Really interesting is Docco, but it apprently shows the code output with the text.

A possible solution would be the RPres markdown horizontal rule: using ***, it creates two easy to use columns. But I do find documentation on its implementation in Markdown documents.

Here an image showing my results so far and an example of my code:

enter image description here

```{r setoption, cache=TRUE, warning=FALSE, message=FALSE, fig.width=12}
knitr::opts_chunk$set(cache=TRUE, warning=FALSE, message=FALSE, fig.width=4,     echo = FALSE)
```

```{r, loadlibraries}
library(knitr)
library(lattice)
```

### Exercise 1 - 22/4/'16

 Is the data set shown in the following figure symmetric or skewed? How many modes does this data set have?

```{r 1.1}
e1 <- rep(seq(1, 6, 1), c(6, 4, 2, 2, 4, 6))
barchart(table(e1), horizontal = FALSE, xlab = "", ylab = "Frequency")
```
**Solution:**
The data set is symmetric. Furthermore, it has two modes.

### Exercise 2 - 22/4/'16

Describe the shape of the dataset shown in the following figure.
```{r 2.1}
e2 <- rep(seq(1, 9, 1), c(6, 5, 4, 3, 2, 1, 1, 1, 1))
barchart(table(e2), ylab = "Frequency", horizontal = FALSE)
```
**Solution:**
The dataset is right skewed, also said right skewed, with one mode.
1
You probably want the reverse of this?Roman Luštrik
Yes, something like that.Worice

1 Answers

3
votes

As you're asking for columns, my answer will be: table.

Using pipe_tables, figure and text can be alinged next to each other. However, this comes at a price:

The cells of pipe tables cannot contain block elements like paragraphs and lists, and cannot span multiple lines.

If this limitation is acceptable, pipe_tables provide a quite straightforward solution:

```{r img, fig.show = "hide", echo = FALSE}
library(knitr)
hist(rnorm(1000))    
```

Figure|Explanation
-------------------------------|-------------------------
`r include_graphics(paste0(opts_chunk$get("fig.path"), "img-1.png"))`|Histogram of 1000 draws from a standard normal density.

Result

Although the column headers cannot be omitted, you can leave them blank if desired.

Note that I initially suppress the plot (fig.show = "hide") and use include_graphics to include it afterwards. Otherwise, there would be a newline after the plot which disrupts the table.

(In knitr 1.12.3, include_graphics doesn't seem to work properly with inline code chunks. However, the current development version 1.12.25 works well.)

Extension

I hacked together an extension that allows to use a single chunk to generate and show the plots and some more features:

```{r setup, echo = FALSE}
library(knitr)
FigureNextToText <- function(number, # number of plot in chunk
                             text, 
                             alt = "", # alternative text for image
                             label = opts_current$get("label"), # set explicitly when using inline!
                             ext = ".png",
                             headerL = "&nbsp;", headerR = "&nbsp;", # empty string confuses pandoc if only right header is set
                             widthL = 30, widthR = 30,
                             ...) {
  path <- fig_chunk(label = label, ext = ext, number = number, ...)
  template <- "%s|%s
%s|%s
![%s](%s)|%s\r\n\r\n"
  output <- sprintf(
    template,
    headerL, headerR,
    paste0(rep("-", widthL), collapse = ""), paste0(rep("-", widthR), collapse = ""),
    alt, path, text
    )
  return(asis_output(output))
}
```

```{r img, fig.show = "hide", echo = FALSE, results = "asis"}
library(knitr)
hist(rnorm(1000))
hist(runif(n = 1000, min = 0, max = 10))

FigureNextToText(1, text = "Histogram of draws from standard normal density.", widthL = 50, widthR = 10)
FigureNextToText(2, text = "Histogram of draws from uniform distribution.", headerR = "Explanation", alt = "Histogram 2.") 
```

Some text.

`r FigureNextToText(2, text = "The same plot, this time inline.", label = "img", headerR = "Explanation", alt = "Histogram 2.")`

Some more text.

Output

I know that the setup looks a little bit scary, but once FigureNextToText is defined, it can be called quite simply, e.g.:

FigureNextToText(2, text = "Histogram of draws from uniform distribution.", headerR = "Explanation", alt = "Histogram 2.")

Finding the correct values for widthL and widthR is somewhat cumbersome. This is because their effect depends on the number of characters in the cell, i.e. the filename of the image in the MD file and the alt text, too.