<commit_before>---
layout: default
title: Sweave
subtitle: transition from Sweave to knitr
category: demo
---

I tried very hard to maintain compatibility with Sweave for easier transition from Sweave to **knitr**, but there are still chances that your old Sweave documents do not work with **knitr**.

## New syntax for chunk options

By default, **knitr** uses a [new syntax](/knitr/options) to parse chunk options (either local options in `<<>>=` or global options in `\SweaveOpts{}`), which is similar to _R function arguments_. This gives us much more power than the old Sweave syntax. You can use arbitrary objects in chunk options and make use of full power of R. Here is a trivial example of setting graphical parameters for base R graphics:

{% highlight r %}
<<par-hook, cache = FALSE>>=
knit_hooks$set(pars = function(before, options, envir)) {
  if (before) graphics::par(options$pars)
}
@

<<use-pars, pars = list(mar=c(4, 4, .1, .1), mgp=c(2, 1, 0))>>=
plot(mtcars[, 1:2])
@
{% endhighlight %}

First we have set a [chunk hook](/knitr/hooks) named `pars`, which uses the chunk option `pars` to set `par()`; then we pass a list of parameters to the chunk option `pars`, which will be passed to the hook function and used _before_ (`if (before)`) this chunk is evaluated. This enables us to hide the long and boring code to set graphical parameters in the output.

The chunk options can even make use of objects in a chunk dynamically. Here is another example of setting the caption for the figure environment:

{% highlight r %}
<<setup, cache = FALSE>>=
opts_knit$set(eval.after = 'fig.cap') # evaluate fig.cap after the chunk
@

<<t-test, fig.cap = paste("The P-value is", t.test(x)$p.value)>>=
x = rnorm(100)
boxplot(x)
@
{% endhighlight %}<commit_msg>more explanation on syntax of chunk options<commit_after>---
layout: default
title: Sweave
subtitle: transition from Sweave to knitr
category: demo
---

I tried very hard to maintain compatibility with Sweave for easier transition from Sweave to **knitr**, but there are still chances that your old Sweave documents do not work with **knitr**. You will see error messages or warnings in these cases, but please note where these messages come from: if they appear when **knitr** is parsing chunk options, they may not matter at all; when it comes to evaluating chunks (after you see the progress bar), the errors are usually fatal, e.g. `object 'foo' not found` which is often an indication that you used a character option without quoting the character string (e.g. `results=hide` should be `results='hide'`). Anyway, you are encouraged to switch to the new syntax described below.

## New syntax for chunk options

By default, **knitr** uses a [new syntax](/knitr/options) to parse chunk options (either local options in `<<>>=` or global options in `\SweaveOpts{}`), which is similar to _R function arguments_. This gives us much more power than the old Sweave syntax. You can use arbitrary objects in chunk options and make use of full power of R. Here is a trivial example of setting graphical parameters for base R graphics:

{% highlight r %}
<<par-hook, cache = FALSE>>=
knit_hooks$set(pars = function(before, options, envir)) {
  if (before) graphics::par(options$pars)
}
@

<<use-pars, pars = list(mar=c(4, 4, .1, .1), mgp=c(2, 1, 0))>>=
plot(mtcars[, 1:2])
@
{% endhighlight %}

First we have set a [chunk hook](/knitr/hooks) named `pars`, which uses the chunk option `pars` to set `par()`; then we pass a list of parameters to the chunk option `pars`, which will be passed to the hook function and used _before_ (`if (before)`) this chunk is evaluated. This enables us to hide the long and boring code to set graphical parameters in the output.

The chunk options can even make use of objects in a chunk dynamically. Here is another example of setting the caption for the figure environment:

{% highlight r %}
<<setup, cache = FALSE>>=
opts_knit$set(eval.after = 'fig.cap') # evaluate fig.cap after the chunk
@

<<t-test, fig.cap = paste("The P-value is", t.test(x)$p.value)>>=
x = rnorm(100)
boxplot(x)
@
{% endhighlight %}
{'fraction_non_alphanumeric': 0.10599415204678363, 'fraction_numerical': 0.005847953216374269, 'mean_word_length': 4.600272851296044, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3486489', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1162, 'n_words': 596}