Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
Knitr tutorial
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
'''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 == If you are install it locally you need to run R and then install the package within R: <syntaxhighlight lang="r"> install.packages("knitr") </syntaxhighlight> If you are running it from within Overleaf, you just need to make sure that your file is named so that it ends with <code>.rtex</code> == Compiling Knitr == 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: <syntaxhighlight lang="r"> Rscript -e "library(knitr); knit('main.tex')" </syntaxhighlight> If you use RStudio: * Name your file as ".Rnw" * Put <code>% !Rnw weave = knitr</code> as the first line of your file == Using Knitr in RWeb/Sweave mode == There are only really two tricks. Using Knitr in an inline expression: <syntaxhighlight lang="r"> \Sexpr{} </syntaxhighlight> Using Knitr in a block: <syntaxhighlight lang="r"> <<blockname>>= '''R CODE HERE''' @ </syntaxhighlight> 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 == 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>
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information