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

Print HTML or Word table in knitr so that whitespaces in strings are respected

Using knitr and Rstudio, I'm trying to print a dataframe to HTML or Word, so that the leading whitespaces in versicolor, will push versicolor to the right. #data library(knitr ) library(xtable) df <- iris[c(1,51),c(5,1)] df$Species <-…
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
4
votes
2 answers

R Markdown (Rmd) hide a paragraph of general text based on condition

I am working on an Rmd down report and depending on an R variable I want to decide whether to include a paragraph or not e.g. ##Abstract paragraph Blurb If result type is 1 then another paragraph of blurb I can't find any easy way to do this. …
Gordon
  • 73
  • 7
4
votes
1 answer

Sparklines in R for use in Latex table (using Knitr)

I would like to create a sparkline using R and include it in a Latex table (using knitr) . I have found the SparkTable package but have been unable to work out how to specify (based on my own set of rules) which data points are classified as…
user1420372
  • 2,077
  • 3
  • 25
  • 42
4
votes
1 answer

Global variable in knitr for both R code Chunks and LaTeX

I am trying to create a numeric variable (in code: called nClusters) that can be used in a knitr document both in R code chunks and LaTeX. An example is in the code below. Here, I initialize and assign the numeric variable nClusters to a value of 7.…
user1830307
4
votes
1 answer

How to elegantly + robustly cache external script in knitr rmd document?

Say, I have an external R script external.R: df.rand <- data.frame(rnorm(n = 100), rnorm(n = 100)) Then there's a main.Rmd: \documentclass{article} \begin{document} <>= library(knitr) library(ggplot2) # global chunk…
maxheld
  • 3,963
  • 2
  • 32
  • 51
4
votes
1 answer

Print a frequency table of a factor with kable in rmarkdown

I have a factor in R, that consists of the levels a, b and c. The data consists of 2 a's, one b and no c. I want to get an output like this (frequency of the elements according to the levels): fac <- factor(c("a", "b", "a"), levels=c("a", "b",…
BumbleBee
  • 986
  • 9
  • 19
4
votes
1 answer

function comments not displaying in knitr (r studio)

So I am making an .rmd file to document the development of some functions I am building. I am working in R studio. I noticed when I typed ```{r echo=TRUE, tidy=FALSE } createExamData ``` it resulted in this in the knitted document ##…
4
votes
1 answer

Problems cross referencing in LaTex with knitr and xtable

I am currently working with R Studio to produce PDF documents with R/knitr in LaTex. Within these documents I want to present part of my results in tables that I want to refer to in the text. I produce these tables using the xtable package within R…
Sarina
  • 548
  • 3
  • 10
4
votes
0 answers

RStudio: Code sections and compiled notebooks

I really like RStudio's code sections because they help me keep my code organized and makes navigation very easy. So, the code/comment line for code sections look like this (the key for RStudio to recognize the code section is that the comment…
Hernando Casas
  • 2,837
  • 4
  • 21
  • 30
4
votes
3 answers

Character vector in R as itemized list with knitr

I have a vector (e.g. letters) that I want to incorporate into my .Rnw knitr document as an itemized list. How do I do this? I have tried \Sexpr{letters} but it merely places commas between each: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r,…
CephBirk
  • 6,422
  • 5
  • 56
  • 74
4
votes
1 answer

Unwanted white space with knitr+booktabs package

I want to include a table in my latex document that I create using knitr package. I "call" the table using: \begin{table}[H] \begin{centering} <>= kable(Per_cond,…
Guillaume
  • 43
  • 2
4
votes
2 answers

How to generate a GitHub flavoured markdown file using knitr?

I love using knitr to generate dynamic reports and share them with my co-workers using GitHub. What I usually do is to knit my Rmarkdown script --knit ('myfile.Rmd')-- and generate a markdown (myfile.md) version that can be directly seen on GitHub.…
4
votes
3 answers

Stargazer options: resizebox and label

Is it possible to incorporate resizebox into stargazer in R? I'm trying to create a table that is too wide to fit, even on landscape perspective. I manually added resizebox{\textwidth}{!} { \begin{tabular} \end{tabular} } to the .tex file, and I…
Shffl
  • 396
  • 3
  • 18
4
votes
2 answers

RMarkdown with knitr to HTML: How to hide bullets in TOC (table of contents)?

How can I suppress the bullet points in front of the TOC items in the created HTML file? I want to see only the headline numbers... Example Test.Rmd file: --- title: "Untitled" author: "Author" date: "01/25/2015" output: html_document: …
R Yoda
  • 8,358
  • 2
  • 50
  • 87
4
votes
1 answer

knitr plot not showing - basic example

I am trying out Knitr with RStudio. The output of plot() is now showing up in the compiled PDF. Below is an example. The code shows up in the PDF, but not the plot.…
Ravi
  • 119
  • 1
  • 5