Questions tagged [knitr]

knitr is an R package for dynamic report generation based on the concept of literate programming.

knitr is an package for dynamic report generation based on the concept of . That is, you can mix text, R code, and its output into a single document.

It combines features of and related R packages like weaver and R2HTML. It has built-in support for cache, graphics via the tikzDevice package, syntax highlighting via the highlight package and code reformatting via the formatR package.

Unlike Sweave, the document format for knitr does not have to be : , , , and are also supported. Both the syntax and the output are customizable via pattern lists and hook functions respectively, for example, it is easy to make use of the listings package to mark up R code and output by setting up a series of output hooks. A rich set of chunk options and package options are documented in the package website. There are also demos showing possible applications of knitr.

Below is a "Hello World" output from knitr taken from the package manual:

knitr hello world example code and output

Repositories

Vignettes

Book

Other resources

Related tags

5297 questions
60
votes
1 answer

Difference: "Compile PDF" button in RStudio vs. knit() and knit2pdf()

TL;DR What are the (possibly unwanted) side-effects of using knit()/knit2pdf() instead of the "Compile PDF"1 button in RStudio? Motivation Most users of knitr seem to write their documents in RStudio and compile the documents using the "Compile PDF"…
CL.
  • 14,577
  • 5
  • 46
  • 73
60
votes
5 answers

Code chunk font size in Rmarkdown with knitr and latex

In knitr, the size option works fine in a .Rnw file, the following code generates: \documentclass{article} \begin{document} <>= summary(mtcars) @ \end{document} However, I can't get it to work in Rmarkdown. The following…
Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66
59
votes
4 answers

Producing subscripts in R markdown

I'm aware that R markdown can produce superscripts: text^superscript But is it possible to produce proper subscripts? Or is the only way to do so to cheat and use LaTeX math mode: $\sf{text_{subscript}}$ The intended final output is HTML.
sebastian-c
  • 15,057
  • 3
  • 47
  • 93
58
votes
2 answers

How to send R markdown report in body of email?

update: Brandon Bertelsen's answer: Brandon's answer produces the following output. It doesn't produce nice tables or highlight code like Rstudio does, and it crashes on some html files with unicode, so I'm not using it to automate my email…
Nelson Auner
  • 1,421
  • 1
  • 15
  • 21
58
votes
1 answer

Animated rgl graphs with knitr

I want to include animated rgl graphs in my .Rnw document through knitr. Here is my MWE: \documentclass{article} << label = setup, include = FALSE>>= opts_chunk$set(fig.path = 'figure/', cache = FALSE, dev = "pdf", fig.align = 'center', fig.show…
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
58
votes
4 answers

What is the knitr equivalent of `R CMD Sweave myfile.rnw`?

What is the command-line knitr equivalent of R CMD Sweave myfile.rnw?
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
57
votes
4 answers

R Markdown - variable output name

With one R markdown file, I would like to create different possible output pdf documents, where the output file name should be defined within the document. Is there any way to convince markdown to manipulate the output filename in such a way?…
Sosel
  • 1,678
  • 1
  • 16
  • 31
55
votes
5 answers

How do I add a URL to R markdown?

I want to refer a URL link to the R markdown and I already tried the input such as "addlink" or "address <- ...." Both of them do not work. How can I add a link in R markdown? Thanks! EDIT: op's code from comment was {r} [linked phrase]…
Jennifer Liu
  • 571
  • 1
  • 5
  • 6
55
votes
7 answers

How to hold figure position with figure caption in pdf output of knitr?

I am using knitr (1.9.5 and 1.9.17) and rmarkdown (0.5.3.1), and would like to hold figure position in the pdf output. The generated pdf file is working fine when chunk option fig.pos="H" is used. However, the figure position is not hold when…
Bangyou
  • 9,462
  • 16
  • 62
  • 94
55
votes
8 answers

Figure position in markdown when converting to PDF with knitr and pandoc

I'm trying to control the position of a plot when converting to PDF using knitr and pandoc. My .Rmd file looks this: # My report Some text some text some text some text some text some text some text some text some text ```{r myplot, echo=FALSE,…
luciano
  • 13,158
  • 36
  • 90
  • 130
54
votes
3 answers

R: In RStudio how do I make knitr output to a different folder to avoid cluttering up my drive?

I am using RStudio's knit HTMl function to output some presentations. But it always outputs the files to my current work directory. How can I make it output to another directory so that my directory is clean with only the original .rmd files?
xiaodai
  • 14,889
  • 18
  • 76
  • 140
52
votes
5 answers

Adjusting width of tables made with kable() in RMarkdown documents

Is it possible to adjust the width of columns when making tables with the kable() function in knitr? A chunk like this for a table with two columns produces a table that takes up the entire width of the document. I'd like to make the columns…
N Brouwer
  • 4,778
  • 7
  • 30
  • 35
51
votes
2 answers

How to request an early exit when knitting an Rmd document?

Let's say you have an R markdown document that will not render cleanly. I know you can set the knitr chunk option error to TRUE to request that evaluation continue, even in the presence of errors. You can do this for an individual chunk via error =…
jennybryan
  • 2,606
  • 2
  • 18
  • 33
51
votes
4 answers

R knitr: Possible to programmatically modify chunk labels?

I'm trying to use knitr to generate a report that performs the same set of analyses on different subsets of a data set. The project contains two Rmd files: the first file is a master document that sets up the workspace and the document, the second…
aaronwolen
  • 3,723
  • 1
  • 20
  • 21
49
votes
7 answers

Setting work directory in knitr using opts_chunk$set(root.dir = ...) doesn't work

My R project is structured like a package with directories /R, /vignettes, /data etc. In one of my Rmd docs in /vignettes I source a script which in located in /R. Inside this script I use read.csv() to load a file located in inst/extdata/. The…
Thomas Neitmann
  • 2,552
  • 1
  • 16
  • 31