Questions tagged [knitr]

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

knitr is an R package for dynamic report generation based on the concept of literate programming. It combines features of and related packages like pgfSweave, cacheSweave, weaver and R2HTML. It has built-in support for cache, TikZ 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 LaTeX, both HTML and markdown 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:

enter image description here

369 questions
21
votes
2 answers

knitr & xcolor: incompatible color definition

I'm using knitr and RStudio to compile LaTeX. In my real project I'd like to use some options from the LaTeX package 'xcolor', but when I do include \usepackage[options]xcolor, I get the error message "Incompatible color definition on line xx" where…
LimnoLeon
  • 311
  • 2
  • 5
12
votes
1 answer

Plot two figures side by side in knitr without changing mfrow?

I would like to plot 2 figures side-by-side using knitr. I know I could use par(mfrow=c(1,2)), but I've done a lot of work making the figures just right as they are. I've seen this (Two column layout with text and knitr chunk), and it's not working…
ldecicco
  • 223
5
votes
1 answer

Use a LaTeX expression in a knitr figure caption

I am writing an .Rnw document in RStudio. I would like to be able to use latex expressions in Figure captions created by knitr. First a code chunk for the caption: <>= cap <- "My figure. Some \textit{italics}. A reference to…
tsawallis
  • 153
4
votes
1 answer

Escaping % symbol when passed as a string from R chunk to knitr

I can't get the following to work: \begin{minipage} <>= your_rank <- percentile ## calculated earlier rank_message <- ifelse(is.na(score), ' ', paste0("You are in the top ", your_rank, "% of…
4
votes
1 answer

Input Tex file in knitr file

I am showing a simple example here just to frame the question. \documentclass{article} \begin{document} <<>>= library('ggplot2') dataset <- diamonds @ \begin{table}[htbp] \centering \begin{tabular}{c|c|c|c} \textbf{Name} & \textbf{Columns}…
4
votes
1 answer

display \Sexpr{} in Latex without evaluation

I am working on a tutorial for the knitr package using the beamer class. I was able to display latex code and knitr chunks inside my presentation without evaluation but I additionally want to add inline code, namely \Sexpr{} without evaluation. So…
beginneR
  • 185
4
votes
1 answer

Is it possible to \include a .TeX file compiled from .Rnw?

I have a document with this structure: main.TeX which (ideally) includes bodytext.TeX compiled with knitr from bodytext.Rnw. I say ideally because apparently it is not that simple. I noted that knitr adds lines to the preamble of the compiled .TeX.…
Francesco
  • 4,624
  • 4
  • 35
  • 66
3
votes
1 answer

Setting Knitr options

The following code chunk doesn't work I get an error message and it disrupts the compilation of my document. Not sure if anyone has any ideas? Very M WE below. As @Speravir pointed out, not sure it adds much to enlighten. This is really about…
3
votes
1 answer

Knitr adding part of preamble twice

I am working on my dissertation and received a .tex document with a lot of the formatting requirements included. I have started adding items, importing additional .Rnw files for different chapters. However, when I compile the PDF, I get the…
3
votes
1 answer

Latex code in knitr script?

I'm using knitr to rewrite some documents originally written in latex. Here is the preamble of the knitr --- title: "Blabla" date: "May 2, 2017" bibliography: allpapers.bib output: word_document: fig_caption: true reference_docx:…
user1933
  • 335
3
votes
2 answers

Embedding knitr tex files on a bigger latex file

There are already some question about about how to use Knitr's latex output within a new knitr document, using Rstudio and include or input and declaring some chunks as child. But what if I want to use a simple knitr output (such as an R xtable)…
skan
  • 1,008
3
votes
2 answers

Knitr: Referencing data from a figure later in the paper

I'm working with knitr Is it possible to reference data from a figure in the paper? I have a code chunk that produces a figure in the appendix of my paper. I know how to reference the figure itself "figure 5 shows a histogram..." with "figure…
2
votes
1 answer

How to remove the blank page when omitting comments and messages in knitr

I am using the following code to create a plot, I want to omit message, comments and warnings and everything else but the plot. \documentclass{article} \usepackage{pdflscape} \pagestyle{empty} \begin{document} \begin{landscape} <
Lin
  • 145
2
votes
2 answers

knitr Caption figure without number

I am looking for a way to caption my figures without numbering them (like \caption*{}). I am not sure how to do this in knitr. The following example shows my first attempt: <
2
votes
2 answers

shrink knitr output to multiple columns

I am providing a very simple example here. In my dataset, I have 50+ names in column. I get annoyed by the fact that colnames() output takes 50+ lines too! Is it possible to divide the output over multiple columns? Just by using some simple…
1
2