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
315
votes
8 answers

YAML current date in rmarkdown

I'm wondering if there's a trick to put the current date in the YAML front-matter of a .rmd document to be processed by knitr and the rmarkdown package. I used to have the following line at the top of my wiki pages, _baptiste, `r…
baptiste
  • 75,767
  • 19
  • 198
  • 294
259
votes
3 answers

Set margin size when converting from Markdown to PDF with pandoc

I have created an RMarkdown file in RStudio and managed to knit it with knitr into an HTML and .md file. Next, I used pandoc to convert the .md file into a PDF file (I get an error if I try and convert from the .html file). However, the PDF that…
mchangun
  • 9,814
  • 18
  • 71
  • 101
238
votes
4 answers

R - Markdown avoiding package loading messages

I have been using Knitr via R-Studio, and think it is pretty neat. I have a minor issue though. When I source a file in an R-Chunk, the knitr output includes external comments as follows: + FALSE Loading required package: ggplot2 + FALSE Loading…
Roark
  • 2,575
  • 2
  • 14
  • 8
214
votes
9 answers

Difference between R MarkDown and R NoteBook

I am trying to understand at a high level what the differences between R Markdown and R NoteBook. I know they are interrelated but I would like to figure out how they are related. My understanding is this: I know R Notebooks are really R Markdown…
PagMax
  • 8,088
  • 8
  • 25
  • 40
185
votes
8 answers

How to set size for local image using knitr for markdown?

I have a local image that I would like to include in an .Rmd file which I will then knit and convert to HTML slides with Pandoc. Per this post, this will insert the local image : ![Image Title](path/to/your/image) Is there a way to modify this code…
Adam Smith
  • 2,584
  • 2
  • 20
  • 34
143
votes
5 answers

Comment out text in R Markdown (Rmd file)

In an R Markdown (.Rmd) file, how do you comment out unused text? I'm not referring to the text in the R code chunk, but the general texts, like % in LaTex for example.
aonan zhang
  • 1,503
  • 2
  • 10
  • 6
132
votes
3 answers

How to combine two RMarkdown (.Rmd) files into a single output?

I have two files in the same folder: chapter1.Rmd and chapter2.Rmd, with the following content: chapter1.Rmd --- title: "Chapter 1" output: pdf_document --- ## This is chapter 1. {#Chapter1} Next up: [chapter 2](#Chapter2) chapter2.Rmd --- title:…
RobinLovelace
  • 4,799
  • 6
  • 29
  • 40
132
votes
8 answers

How to convert R Markdown to PDF?

I've previously asked about the commands for converting R Markdown to HTML. What is a good way to convert R Markdown files to PDF documents? A good solution would preserve as much as possible of the content (e.g., images, equations, html tables,…
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
121
votes
1 answer

Relationship between R Markdown, Knitr, Pandoc, and Bookdown

What is the relationship between the functionality of R Markdown, Knitr, Pandoc, and Bookdown? Specifically what is the 'division of labour' between these packages in converting markup documents with embedded R code (e.g. .Rnw or .Rmd) into final…
RobinL
  • 11,009
  • 8
  • 48
  • 68
115
votes
6 answers

How to show code but hide output in RMarkdown?

I want my html file to show the code, but not the output of this chunk: ```{r echo=True, include=FALSE} fun <- function(b) { for(a in b) {print(a) return(a * a)} } y <- fun(b) ``` When I run the code, i need the print to…
Laurent
  • 1,914
  • 2
  • 11
  • 25
113
votes
6 answers

Rstudio rmarkdown: both portrait and landscape layout in a single PDF

I wonder how to use rmarkdown to generate a pdf which has both portrait and landscape layout in the same document. If there is a pure rmarkdown option that would be even better than using latex. Here's a small, reproducible example. First, rendering…
user3712688
  • 1,151
  • 2
  • 8
  • 5
113
votes
2 answers

knitr Markdown highlighting in Emacs?

Is there any modification of an existing tool or new tool that will syntax-highlight (colour) blocks of R code in a knitr markdown document when opened in Emacs? The knitr chunks I am looking at are triple-backtick blocks [```] that contain R code…
719016
  • 9,922
  • 20
  • 85
  • 158
108
votes
8 answers

Programmatically creating Markdown tables in R with KnitR

I am just starting to learn about KnitR and the use of Markdown in generating R documents and reports. This looks to be perfect for a lot of the day to day reporting that I have to do with my job. However, one thing that I'm not seeing is an easy…
TARehman
  • 6,659
  • 3
  • 33
  • 60
106
votes
14 answers

How to source R Markdown file like `source('myfile.r')`?

I often have a main R Markdown file or knitr LaTeX file where I source some other R file (e.g., for data processing). However, I was thinking that in some instances it would be beneficial to have these sourced files be their own reproducible…
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
95
votes
4 answers

Hiding the R code in Rmarkdown/knit and just showing the results

In my R Markdown documents, I sometimes want to just generate a report without showing the actual code (specially when I send it to my boss). How can I hide the R code and just show the graph and results? For example: --- output:…
Mark
  • 10,754
  • 20
  • 60
  • 81
1
2 3
99 100