install.packages(r2resize)
library(r2resize)
add.resizer()
Images generated by knitr are saved in a figures folder.
However, they also appear to be represented in the HTML output using a
data URI
scheme. This means that you can paste the HTML into a blog post or
discussion forum and you don’t have to worry about finding a place to
store the images; they’re embedded in the HTML.
Here is a basic plot using base graphics:
```r
set.seed(1234)
library(ggplot2)
library(lattice)
x <- 1:10
y <- round(rnorm(10, x, 1), 2)
df <- data.frame(x, y)
plot(x)
```
<img src="resize_files/figure-html/unnamed-chunk-2-1.png" width="672" />
plot(x)
Note that unlike traditional Sweave, there is no need to write
fig=TRUE.
Note that unlike traditional Sweave, there is no need to print lattice plots directly.
The following code hides the command input (i.e.,
echo=FALSE), and outputs the content directly as code
(i.e., results=asis, which is similar to
results=tex in Sweave).
Here are some dot points
* The value of y[1] is -0.21
* The value of y[2] is 2.28
* The value of y[3] is 4.08
Here are some dot points
x | y
--- | ---
1 | -0.21
2 | 2.28
3 | 4.08
4 | 1.65
5 | 5.43
6 | 6.51
7 | 6.43
8 | 7.45
9 | 8.44
10 | 9.11
| x | y |
|---|---|
| 1 | -0.21 |
| 2 | 2.28 |
| 3 | 4.08 |
| 4 | 1.65 |
| 5 | 5.43 |
| 6 | 6.51 |
| 7 | 6.43 |
| 8 | 7.45 |
| 9 | 8.44 |
| 10 | 9.11 |
The folllowing code supresses display of R input commands (i.e.,
echo=FALSE) and removes any preceding text from console
output (comment=""; the default is
comment="##").
```
x y
1 1 -0.21
2 2 2.28
3 3 4.08
4 4 1.65
5 5 5.43
6 6 6.51
```
x y
1 1 -0.21
2 2 2.28
3 3 4.08
4 4 1.65
5 5 5.43
6 6 6.51
The following is an example of a smaller figure using
fig.width and fig.height options.
```r
plot(x)
```
<img src="resize_files/figure-html/unnamed-chunk-7-1.png" width="288" />
plot(x)
Caching analyses is straightforward. Here’s example code. On the first run on my computer, this took about 10 seconds. On subsequent runs, this code was not run.
If you want to rerun cached code chunks, just delete the contents of
the cache folder
```r
for (i in 1:5000) {
lm((i+1)~i)
}
```
For those not familiar with standard Markdown, the following may be useful. See the source code for how to produce such points. However, RStudio does include a Markdown quick reference button that adequatly covers this material.
Simple dot points:
and numeric dot points:
and nested dot points:
Equations are included by using LaTeX notation and including them either between single dollar signs (inline equations) or double dollar signs (displayed equations). If you hang around the Q&A site CrossValidated you’ll be familiar with this idea.
There are inline equations such as \(y_i = \alpha + \beta x_i + e_i\).
And displayed formulas:
\[\frac{1}{1+\exp(-x)}\]
knitr provides self-contained HTML code that calls a Mathjax script to display formulas. However, in order to include the script in my blog posts I took the script and incorporated it into my blogger template. If you are viewing this post through syndication or an RSS reader, this may not work. You may need to view this post on my website.
Tables can be included using the following notation
| A | B | C |
|---|---|---|
| 1 | Male | Blue |
| 2 | Female | Pink |
Here’s an example image:
image from redmond barry building unimelb
Here is Markdown R code chunk displayed as code:
```r
x <- 1:10
x
```
```
## [1] 1 2 3 4 5 6 7 8 9 10
```
And then there’s inline code such as x <- 1:10.
Let’s quote some stuff:
To be, or not to be, that is the question: Whether ’tis nobler in the mind to suffer The slings and arrows of outrageous fortune,
knitr. He has also posted this
example of R Markdown.The following are a few questions I encountered along the way that
might interest others. ### Annoying <br/>’s
Question: I asked on the Rstudio discussion site: Why
does Markdown to HTML insert <br/> on new
lines?
Answer: I just do a find and delete on this text for now.
Question: I asked on StackOverflow about How to set cache=FALSE for a knitr markdown document and override code chunk settings?
Answer: Delete the cache folder. But there are other possible workflows.
Question: I asked on Stack Overvlow about whether there an R Markdown equivalent to Sexpr in Sweave?.
Answer: Include the code between brackets of “backick r space” and “backtick”. E.g., in the source code I have calculated 2 + 2 = 4 .