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
44
votes
2 answers

including a interactive 3D figure with knitr

Using knitr it is possible to embed a rgl 3D graphics in a html document from a Rmarkdown source file: ```{r setup} library(rgl) knit_hooks$set(rgl = hook_rgl) x <- sort(rnorm(1000)) y <- rnorm(1000) z <- rnorm(1000) + atan2(x,y) ``` ```{r,…
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
43
votes
2 answers

Specifying multiple simultaneous output formats in knitr

I would like to be able to specify multiple output formats at the same time, for instance html_document and a pdf_document. I know that this can be done very simply with something like --- output: [html_document, pdf_document] --- I might have…
Justace Clutter
  • 2,097
  • 3
  • 18
  • 31
43
votes
2 answers

How to remove the % lines in xtable table output by Knitr

By using xtable and knitr, I add a table to my RMD document and export to PDF file. ```{r, results='asis'} library(xtable) xtable(matrixtable) ```` It looks great except there is a line % latex table generated in R 3.1.0 by xtable 1.7-3 package %…
tky
  • 687
  • 1
  • 5
  • 9
42
votes
5 answers

Changing chunk background color in RMarkdown

I would like to have a certain code chunk highlighted in a different color (e.g. red) to indicate that it is bad practice. If I was using .Rnw, I could add the chunk option background = 'red' and get what I want, but this does not seem to work in…
AmeliaMN
  • 787
  • 1
  • 8
  • 9
42
votes
5 answers

In `knitr` how can I test for if the output will be PDF or word?

I would like to include specific content based on which format is being created. In this specific example, my tables look terrible in MS word output, but great in HTML. I would like to add in some test to leave out the table depending on the…
Tom
  • 4,860
  • 7
  • 43
  • 55
42
votes
2 answers

RMarkdown in Shiny Application

Problem Is there a way to insert (and evaluate) an RMarkdown script in a shiny application. (Note, I am not looking for a shiny application in RMarkdown that is explained here, nor am I looking for Markdown scripts in shiny (see Shiny Gallery…
David
  • 9,216
  • 4
  • 45
  • 78
42
votes
3 answers

Dynamic height and width for knitr plots

In knitr, one can specify the size of plot by simply specifying it in the chunk options. For example: ```{r, fig.width=9,fig.height=3} plot(x) ``` I would like to be able to dynamically adjust the figure height and width on the basis of variables…
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
41
votes
3 answers

Use internal links in RMarkdown HTML output

I am using R Studio to create a Rmarkdown (.Rmd) document. I tried: Jump to [Header 1](#anchor) I would like to set up a link so when a reader clicks it, they jump to a specific point on the page. Let's say the point I want them to be directed to…
Joe Parzival
  • 557
  • 2
  • 5
  • 10
41
votes
1 answer

How to include RMarkdown file in r package?

I'm in the process of creating a package in R and I also want to include an R Markdown file. This RMarkdown template contains functions from my package, and is rendered to an html document via knitr. The goal is to regularly run a function (via a…
Alex Franz
  • 461
  • 6
  • 8
39
votes
1 answer

Get rid of \addlinespace in kable

In a knitr/Sweave report, the kable function of knitr with the option booktabs=TRUE adds a \addlinespace each fifth line: P1 & A & 10 & 11.016181 & 110.16181 & 44.95101 & 269.9745\\ P1 & B & 10 & 8.592386 & 85.92386 & 35.06083 & 210.5743\\ P1 & A &…
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
39
votes
1 answer

"Erroneous nesting of equation structures" in using "\begin{align}" in a multi-line equation in rmarkdown to knit+pandoc pdf

I am writing some multi-line equations in R Markdown - LaTeX, using auto-numbering and \begin{align}. Here's a working the example: --- title: "test" output: html_document --- (@eq01) $$ \begin{align} y = x^2 \\ y = x^3 \\ y =…
Hernando Casas
  • 2,837
  • 4
  • 21
  • 30
39
votes
5 answers

knitr gets tricked by data.table `:=` assignment

It seems that knitr doesn't understand that DT[, a:=1] should not result in an output of DT to the document. Is there a way to stop this behaviour? Example knitr document: Data.Table…
Corvus
  • 7,548
  • 9
  • 42
  • 68
39
votes
2 answers

How to import local image using knitr for markdown

I have an externally created png image in a local directory that I'd like to import to a report using knitr. The ultimate target filetype is html. I have no trouble getting my own figures to appear when I create them with R code, but I'm at a loss…
Steve Buyske
  • 393
  • 1
  • 3
  • 4
38
votes
3 answers

Include code from an external R script, run in, display both code and output

Is it possible to include code from an external R script in an .Rmd and simultaneously run the code, display the code, and display its results in the output .HTML file? For example, if I have x <- 1 y <- 3 z <- x + y z in external.R. In the output…
haff
  • 918
  • 2
  • 9
  • 20
38
votes
7 answers

Insert a logo in upper right corner of R markdown html document

I'm trying to put my company logo on the right corner of my html document Here my code: