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
4
votes
1 answer

Knitr ignoring width and out.width options

I have a R Markdown that I knit to html from within RStudio. I can use fig.width and fig.height to adjust the resolution of my plots but I can't seem to widen the page output so that text output doesn't wrap or so that the plots appear physically…
Ryan Hope
  • 502
  • 2
  • 14
4
votes
0 answers

R - knitr - span across multiple output pages

I have a long Rmd document which I want to publish across multiple HTML documents (e.g. one page for data cleaning, one for exploratory analysis, one for confirmatory analysis). Is there a way to either have knitr output multiple HTML pages from…
Timm S.
  • 5,135
  • 6
  • 24
  • 38
4
votes
2 answers

Function to sanitize strings for LaTeX compilation?

While xtable() has a sanitize.text.function argument which allows to sanitize strings with special charaters to stop LaTeX compilation from breaking in Sweave/knitr documents, the package does not export the function to the userspace. How can I…
landroni
  • 2,902
  • 1
  • 32
  • 39
4
votes
2 answers

Error in UseMethod("select_") : no applicable method for 'select_' applied to an object of class "NULL"

I get a strange error when knitting this R Markdown into an HTML file. I think it has to do with some sort of incompatibility in the dplyr package with knitr. UPDATE: I replaced the cbind chunk with the dplyr::bind_cols command, as someone suggested…
Carmen Sandoval
  • 2,266
  • 5
  • 30
  • 46
4
votes
1 answer

Consistent figures size with gridExtra in Rmarkdown knitr-HTML

I am using the gridExtra package grid.arrange() function to plot 4 figures generated by ggplot2. The whole is html-rendered with the knitr package using RMarkdown working on RStudio. In a nutshell: g1 <- ggplot(); g2 <- ggplot(); g3 <- ggplot(); g4…
remi
  • 781
  • 2
  • 13
  • 22
4
votes
2 answers

Extract comments from R source files, keep function in which they occurs

I would like to extract comments (matching to patterns) from my R source script keeping the functions in which they occurs. The goal is to write documentation comments inside function body code using classic markdown checkboxes - [ ] or - [x] and…
jangorecki
  • 16,384
  • 4
  • 79
  • 160
4
votes
1 answer

If-Else Statement in knitr/Sweave using R variable as conditional (Part 2)

I am extending a question I posted here: If-Else Statement in knitr/Sweave using R variable as conditional I would like to use an if-else syntax in LaTeX so that, depending on the value of an R variable (say x), one of two LaTeX text paragraphs are…
user2808302
4
votes
1 answer

If-Else Statement in knitr/Sweave using R variable as conditional

I am currently using knitr in R and RStudio to produce a LaTeX output. My code is a .Rnw file (called, say, testKnitr.Rnw) that is compiled to a pdf file with: knit("testKnitr.Rnw") // in RStudio pdflatex testKnitr.tex // in terminal I would like…
user2808302
4
votes
1 answer

rendering markdown documents is changing HTML < tags to '<'

In my most recent build of the pixiedust package, my HTML tables stopped rend ering correctly. I seem to have isolated the problem to the tags being changed to <th>, and then I get a whole bunch of text in place of the table I should…
Benjamin
  • 16,897
  • 6
  • 45
  • 65
4
votes
0 answers

knitr/Rmarkdown image before title

I would like to add an image to the title page of a PDF document rendered using RStudio with knitr. How can I include an image on the first page above the title? I'm trying to use - \includegraphics{logo.png} which works, but then I cannot include…
Megatron
  • 15,909
  • 12
  • 89
  • 97
4
votes
1 answer

A Way in Knitr to Copy a Chunk?

Knitr Mavens, Background: Using knitr to report a report with many embedded graphs. In the body of the report, all that's appropriate is the graph, not the code. For example: ```{r graph_XYZ_subset, echo = FALSE, message = TRUE, …
Plane Wryter
  • 1,299
  • 1
  • 11
  • 14
4
votes
1 answer

toggle show/hide code in RMarkdown

I am generating an HTML report using Rmarkdown in which I want the user to have an option of looking at code (when they want by using a toggle show/hide code) before each section or figure. I am wondering if there is an option to do so globally and…
Satya
  • 1,708
  • 1
  • 15
  • 39
4
votes
1 answer

Put table captions on top when knitting Word .doc with R markdown

Table captions are positioned below the table when using R markdown to knit a Word document in RStudio. I need the table captions above the table. I have tried the kable() and pander() functions without success. A solution using xtable() when…
4
votes
0 answers

Knitr to docx - Increasing max fig.width beyond 6 inches?

I'm new to knitr, rmarkdown and pandoc, and am having trouble figuring out where to set options. I'd like to have a figure with a width wider than 6 inches. Even in a default word doc margins, it seems like there is at least 6.5 inches available,…
GregF
  • 1,292
  • 11
  • 14
4
votes
1 answer

R Markdown Link Using Code Chunk

I have an Rmd file which interacts with a number of scripts and variables. In these scripts, there is a section where I want to provide a link. That link address is field in a data frame. What I am trying to do is something like the…
jamesknix
  • 43
  • 4