Knitr tutorial: Difference between revisions

From CommunityData
(Created page with "'''knitr''' is "an engine for dynamic report generation with R. It is a package in the statistical programming language R that enables integration of R code into LaTeX, LyX, H...")
 
 
(One intermediate revision by the same user not shown)
Line 32: Line 32:
Using Knitr in an inline expression:
Using Knitr in an inline expression:


<syntaxhighlight lang="r">
   \Sexpr{}
   \Sexpr{}
</syntaxhighlight>
</syntaxhighlight>
Line 44: Line 45:


After this, it's all about options. The most important is <code lang="r">results</code> and the default is <code lang="r">"results="markup"</code> but the other critical one is <code lang="r">"results="asis"</code>.
After this, it's all about options. The most important is <code lang="r">results</code> and the default is <code lang="r">"results="markup"</code> but the other critical one is <code lang="r">"results="asis"</code>.


== Nice to have ==
== Nice to have ==
Line 50: Line 50:
I usually use the following preamble:
I usually use the following preamble:


<<init, echo=FALSE>>=
<syntaxhighlight lang="r">
knit_hooks$set(document = function(x) {
<<init, echo=FALSE>>=
  sub('\\usepackage[]{color}',
knit_hooks$set(document = function(x) {
'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE)
  sub('\\usepackage[]{color}',
})
'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE)
opts_chunk$set(fig.path="figures/knitr-")
})
 
opts_chunk$set(fig.path="figures/knitr-")
source("resources/preamble.R")
@
source("resources/preamble.R")
@
</syntax>

Latest revision as of 18:36, 14 November 2019

knitr is "an engine for dynamic report generation with R. It is a package in the statistical programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents." This page will focus on integrating knitr into LaTeX documents.

Setup[edit]

If you are install it locally you need to run R and then install the package within R:

 install.packages("knitr")

If you are running it from within Overleaf, you just need to make sure that your file is named so that it ends with .rtex

Compiling Knitr[edit]

On Overleaf, you just hit compile, although it can be tricky to debug and see errors unless you dig into the logs.

On your computer, you can use the Rscript command to just run it from R:

 Rscript -e "library(knitr); knit('main.tex')"

If you use RStudio:

  • Name your file as ".Rnw"
  • Put % !Rnw weave = knitr as the first line of your file

Using Knitr in RWeb/Sweave mode[edit]

There are only really two tricks.

Using Knitr in an inline expression:

  \Sexpr{}
Using Knitr in a block:
 <<blockname>>=
 '''R CODE HERE'''
 @

After this, it's all about options. The most important is results and the default is "results="markup" but the other critical one is "results="asis".

Nice to have[edit]

I usually use the following preamble:

<syntaxhighlight lang="r">

<<init, echo=FALSE>>=
knit_hooks$set(document = function(x) {
  sub('\\usepackage[]{color}',
'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE)
})
opts_chunk$set(fig.path="figures/knitr-")

source("resources/preamble.R")
@

</syntax>