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
3
votes
1 answer

Can knitr's cache work with pointer objects?

I'm using knitr to make a document that uses the terra package to draw maps. Here's my minimal .Rmd file: ```{r init, echo=FALSE} library(knitr) opts_chunk$set(cache=TRUE) ``` ```{r maps} library(terra) r = rast(matrix(1:12,3,4)) plot(r) ``` ```{r…
Spacedman
  • 92,590
  • 12
  • 140
  • 224
3
votes
1 answer

I am having trouble while Knitting RMarkdown file

I am getting following error while Knit my RMarkdown file. Parser error: while parsing a block mapping at line 1, column 1 did not find expected key at line 2, column 11 Calls: ... parse_yaml_front_matter -> yaml_load ->…
3
votes
1 answer

How do I insert externally-generated images into R markdown programatically?

Instead of having to include the relative path to an image like so: ![\label{fig:R1amb}R1 Ambient RNA Contamination](../outs/pre-processing/ambientRNA_R1.png) I would like to bring in an image from the yaml object like so: ![\label{fig:R1amb}R1…
CelineDion
  • 906
  • 5
  • 21
3
votes
1 answer

Adding a LaTeX reference label to a table in modelsummary

I'm trying to produce tables in R (not RMarkdown, as this question helps answer) with modelsummary and I'm having a tough time adding reference labels (e.g., tab:hello). As Vincent points out in this answer, extra arguments to modelsummary should be…
Julian
  • 451
  • 5
  • 14
3
votes
1 answer

RMarkdown, knitr::kable shows "\begin{table}" after knitting to pdf document

One of cells in my RMarkdown document ```{r echo=FALSE} head(data,3) %>% knitr::kable(caption = "Pierwsze 3 wiersze ze zbioru danych Lista_1.csv", digits = 2, booktabs = T) gives weird result after knitting to pdf: Of course there shouldn't be…
Brzoskwinia
  • 371
  • 2
  • 11
3
votes
1 answer

Programmatically add tags to yaml header during knitting R markdown file

I'd like to programmatically add tags to the YAML header of a blogdown post when clicking the "knit" button. For example something like this: --- title: This is a post author: brshallo date: '2022-02-11' tags: "`r stringr::str_c('\n - ',…
Bryan Shalloway
  • 748
  • 7
  • 15
3
votes
2 answers

Limit column width for paged rmarkdown tables

Let's say I have an Rmarkdown document. In that document, I create an data frame with two columns, and each of them is quite long. Also I have the "paged" output setting. --- title: "Long Tables" output: html_document: toc: true toc_depth:…
Migwell
  • 18,631
  • 21
  • 91
  • 160
3
votes
0 answers

Set line spacing to 1.15 in R Markdown

In Markdown, after a heading, my knitted document will revert to 1.0 line spacing even though I want it as 1.15. If I add a space after a header (with
), the 1.15 spacing is maintained. If I don't add the
, everything that follows is 1.0…
Mal Vane
  • 75
  • 4
3
votes
2 answers

Rmarkdown HTML rendering issue

I am trying to print a list of HTML tables, but for some reason when I knit the document, I get the raw HTML code for output instead of the rendered table. Example: --- title: "html-render-issue" output:…
Hank Lin
  • 5,959
  • 2
  • 10
  • 17
3
votes
1 answer

Write pdf to figure folder, but delete pngs

This is a follow-up question about this answer. I have set the knitr chunk options to output a png and pdf version of plots in a folder, as well as use the pngs in the knitted report. However, I'd only like to keep the pdf version of the figure and…
teunbrand
  • 33,645
  • 4
  • 37
  • 63
3
votes
2 answers

Rmarkdown is printing the latex code in the knited PDF

I am running in an odd result. I have inserted a number of tables in my document in Rmarkdown. This is the YAML: --- output: pdf_document: toc: true toc_depth: 2 number_sections: true header-includes: \usepackage{float} …
Daniel Estévez
  • 113
  • 1
  • 11
3
votes
0 answers

Broken relative links in RSS feed for blogdown posts using Hugo asset bundling

TLDR: links to assets (such as images) inside Hugo page bundles are not rendered relative to the blog's root by blogdown/knitr, instead they are produced relative to the blog post's root, which works in the browser but breaks in RSS feed…
solarchemist
  • 438
  • 3
  • 13
3
votes
1 answer

dplyr function is displaying an output in R markdown

I am knitting a R markdown and the output looks fine for now, except for the problem that for some reason the knitted html shows the dplyr::summarise output too. Even after turning off show messages and show warnings the output persists. Please note…
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
3
votes
1 answer

Looping variables in the parameters of the YAML header of an R Markdown file and automatically outputting a PDF for each variable

I am applying for junior data analyst positions and have come to the realization that I will be sending out a lot of cover letters. To (somewhat) ease the pain and suffering that this will entail, I want to automate the parts of the cover letter…
R.W.
  • 99
  • 5
3
votes
0 answers

knitr - inline code chunk of foreign engine

I've written a knitr engine to process Maxima code (as part of a package), which works for "regular" chunks just fine, e.g.: ```{maxima} 1+1; ``` results in (%i1) 1+1; # (%o1) 2 However, when I try to get the output printed inline, such…
rcst
  • 175
  • 9
1 2 3
99
100