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
84
votes
6 answers

R Markdown - changing font size and font type in html output

I am using R Markdown in RStudio and the knit HTML option to create HTML output. However, the font used in the ouput for plain text blocks is rather small and I would like to change it to a differnt font and increase the font size. Can someone show…
Bärbel
  • 995
  • 1
  • 8
  • 7
81
votes
2 answers

Adjust the output width of R Markdown HTML output

I am producing an HTML output but I am having issues with the output width of R code output. I'm able to adjust the figure width with no difficulty but when I try to write a data table or the factor loadings, R is outputting at a fixed width which…
Matt Weller
  • 2,684
  • 2
  • 21
  • 30
78
votes
2 answers

Remove Hashes in R Output from R Markdown and Knitr

I am using RStudio to write my R Markdown files. How can I remove the hashes (##) in the final HTML output file that are displayed before the code output? As an example: --- output: html_document --- ```{r} head(cars) ```
mchangun
  • 9,814
  • 18
  • 71
  • 101
77
votes
3 answers

install.packages fails in knitr document: "trying to use CRAN without setting a mirror"

Using the following code I got the data I wanted, but for some reason I can't figure out knitr doesn't let me compile a PDF document, as shown further below: My code: install.packages("weatherData") library(weatherData) istanbul <-…
Locksmith
  • 875
  • 1
  • 6
  • 7
76
votes
5 answers

R knitr Markdown: Output Plots within For Loop

I would like to create an automated knitr report that will produce histograms for each numeric field within my dataframe. My goal is to do this without having to specify the actual fields (this dataset contains over 70 and I would also like to reuse…
bnjmn
  • 4,508
  • 4
  • 37
  • 52
74
votes
2 answers

"long vectors not supported yet" error in Rmd but not in R Script

I am operating matrices with R 3.1 and RStudio 0.99. I have my R Script and with cmd+enter it works without problem. I created an Rmd for reporting but I have this error Error in lazyLoadDBinsertVariable(vars[i], from, datafile, ascii, compress, :…
pachadotdev
  • 3,345
  • 6
  • 33
  • 60
73
votes
2 answers

Plot size and resolution with R markdown, knitr, pandoc, beamer

Doesn't fit on the slide by default, doesn't even print by any other means. Here's the .Rmd: Edit: it seems you have to use plot() in every chunk. Second plot now prints. # Plot should show at high resolution ```{r echo=FALSE, comment = ""} # load…
nacnudus
  • 6,328
  • 5
  • 33
  • 47
73
votes
4 answers

How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?

What commands are run when pressing "Knit HTML" on an R Markdown file in Rstudio 0.96? My motivation is that I might want to run the same command when I'm in another text editing environment or I might want to combine the command in a larger…
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
69
votes
2 answers

Simple manual RMarkdown tables that look good in HTML, PDF and DOCX

How can I manually and simply format a table in RMarkdown that will look good when converted to HTML (using the knitr and markdown packages), PDF (using pandoc and miktex) and docx (using pandoc)? I want to be able to write small tables in…
Ben
  • 41,615
  • 18
  • 132
  • 227
69
votes
1 answer

knitr/Rmd: page break after n lines/n distance

Let me caveat by saying that this may be better suited as an html/css question but (a) I'm not too familiar with those techniques and (b) I wanted to keep it all in the family (R family) if possible. I would like to use knitr to write academic style…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
68
votes
4 answers

knitr/rmarkdown/Latex: How to cross-reference figures and tables?

I'm trying to cross-reference figures and tables in a PDF produced with knitr/rmarkdown. There are some questions on SO and tex.stackexchange (here and here, for example), that suggest the way to do this inline is to add \ref{fig:my_fig}, where…
eipi10
  • 91,525
  • 24
  • 209
  • 285
68
votes
8 answers

How to use objects from global environment in Rstudio Markdown

I've seen similar questions on Stack Overflow but virtually no conclusive answers, and certainly no answer that worked for me. What is the easiest way to access and use objects (regression fits, data frames, other objects) that are located in the…
Adam Robinsson
  • 1,651
  • 3
  • 17
  • 29
67
votes
3 answers

R markdown: Accessing variable from code chunk (variable scope)

In R markdown (knitr package), can I access a variable within the body of the document that was calculated in a code chunk?
m.templemire
  • 725
  • 1
  • 6
  • 8
66
votes
7 answers

two-column layouts in RStudio presentations/slidify/pandoc

I'm trying to come up with a good system for generating slides and accompanying handouts. The ideal system would have the following properties: beautiful in both presentation (PDF/HTML) and handout (PDF) layouts (handouts should have room for…
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
65
votes
1 answer

preventing a chunk run in rmarkdown

I am using Rmarkdown and Knitr using Rstudio. The following both print script and output to html. ```{r} summary(cars) ``` However the following will only print the output, that is embedded plot. ```{r, echo=FALSE} plot(cars) ``` I have…
SHRram
  • 4,127
  • 7
  • 35
  • 53