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

\usepackage{Sweavel} produces error: It seems you are using the Sweave-specific syntax

If I include \usepackage{Sweavel} in my .rnw file, I get an X11 popup error "It seems you are using the Sweave-specific syntax; you may need Sweave2knitr("IPT-baseline-test.rnw") to convert it to knitr" when I compile in RStudio (Version 0.98.484).…
Eric Green
  • 7,385
  • 11
  • 56
  • 102
4
votes
4 answers

Markdown in other statistics packages than R

I'm a great fan of R markdown, finding it even easier than weaving LaTeX for quick project documentation (less than 15 pages). However, I also have to support sometimes other Statistics packages (SPSS, Stata + SAS) and was wondering for equivalent…
Joanne Demmler
  • 1,406
  • 11
  • 31
4
votes
1 answer

Knitr emptyline after \include graphics

I have a little question, I'm not sure of the cause, but the following knitr code : <>= #Data a<- c(1:100) b<- c(100:200) #Plot1 plot(a) #Plot2 plot(b) @ produce this LaTeX…
Hedjour
  • 101
  • 1
  • 8
4
votes
1 answer

knitr chunk options to control line spacing, font size in output

I'd like to have more control over the font size and line spacing used for code chunks and R output via LaTeX. Using the default render_latex(), I see I can set both globally using (in my…
user101089
  • 3,756
  • 1
  • 26
  • 53
4
votes
2 answers

Display subset of R output with knitR

Is there a way to display only part of the R output with knitR? I want to display only part of the summary output from an lm model in a beamer presentation so that it doesn't run off the slide. (As a side note, why is my code not wrapping?) A…
jpfrench
  • 43
  • 5
4
votes
1 answer

Producing the same number of digits in a chunk as inline code

How can I get R chunk code to produce the same number of digits as r inline code in RStudio RMarkdown file? For example: ```{r} x=1:30 sigma.sq=sum((x-mean(x))^2)/30 sigma.sq ``` Thus, $\sigma^2=`r sigma.sq`$. The chunk produces the number…
David
  • 981
  • 1
  • 15
  • 27
4
votes
1 answer

trouble finding file source in .rmd chunk when knitting .rmd from master .R file

Let's say I have a project directory called testknit (and I do, see github for MRE), and inside this I have several subdirectories, including scripts where I keep .R and .rmd files. In RStudio, I create a project and select this testknit directory…
Eric Green
  • 7,385
  • 11
  • 56
  • 102
4
votes
2 answers

kable function: "id" in the columns

When I trying print table with knitr::kable function "id" word apperas in the column names. How can I change it? Example: > x <- structure(c(42.3076923076923, 53.8461538461538, 96.1538461538462, 2.56410256410256, 1.28205128205128,…
Artem Klevtsov
  • 9,193
  • 6
  • 52
  • 57
4
votes
1 answer

bad font alignment using knitr and a non default font

I have some graphics that prints nicely directly typed as R code, but that generates a bad text alignment when generated by…
user2147028
  • 1,281
  • 13
  • 19
4
votes
1 answer

Avoid code-chunks from breaking in Knitr? ( preferably using a chunk option )

Using knitr to create a pdf, codechunks break according to page breaks. Usually this is exactly what I want, but in some cases I would like to be able to avoid this. E.g. by making a code-chunk jump to the next page if it does not fit the current…
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
4
votes
3 answers

knitr xtable highlight and add horizontal lines for the same row,

I am using knitr and xtable to automate my reporting procedure. I want to highlight a few rows of a table and have a horizontal line right above each row highlighted. The .Rnw file I am using reads as below: \usepackage{colortbl,…
user2854008
  • 1,161
  • 4
  • 17
  • 23
4
votes
1 answer

Successive lines of a comment are split up in knitr using R 3, but not R 2.15.3

I have an R script that I call from a python pipeline that knits a given .Rmd file. However, the behavior of output from code blocks changes between versions of R, and I want the old behavior! Here is the script I use to knit my .Rmd…
scooteR
  • 185
  • 7
4
votes
2 answers

placing knitr chunks including plots inside a latex layout (table)

I'm having difficulties inserting R output within a layout based on tabular. \documentclass{article} \usepackage{float} \begin{document} \begin{table} \begin{tabular}{ll} A & <>= plot(1,1) @…
baptiste
  • 75,767
  • 19
  • 198
  • 294
4
votes
1 answer

Imaginary units of complex numbers dropped by 'knitr'

I came across a strange issue in 'knitr' with R code containing complex numbers entered directly using the "x + yi" notation. For an illustration see the minimal example: http://goo.gl/Yj77kI The sample code evaluates correctly, both in R console…
aoles
  • 1,525
  • 10
  • 17
4
votes
1 answer

Table with diacritics with Pandoc&Knitr via R

I have hard time to get my table printed with diacritics via knitr package and pandoc. I believe the Name.md file is produced correctly, but gives me error at the pandoc level. What I'm doing wrong? Without diacritics it works perfectly. Here is…
Maximilian
  • 4,177
  • 7
  • 46
  • 85