Skip to main content
AI in Production 2026 is now open for talk proposals.
Share insights that help teams build, scale, and maintain stronger AI systems.
items
Menu
  • About
    • Overview 
    • Join Us  
    • Community 
    • Contact 
  • Training
    • Overview 
    • Course Catalogue 
    • Public Courses 
  • Posit
    • Overview 
    • License Resale 
    • Managed Services 
    • Health Check 
  • Data Science
    • Overview 
    • Visualisation & Dashboards 
    • Open-source Data Science 
    • Data Science as a Service 
    • Gallery 
  • Engineering
    • Overview 
    • Cloud Solutions 
    • Enterprise Applications 
  • Our Work
    • Blog 
    • Case Studies 
    • R Package Validation 
    • diffify  

Default knitr options and hooks

Author: Colin Gillespie

Published: March 12, 2021

tags: r, knitr, rmarkdown

This is part four of our four part series

  • Part 1: Specifying the correct figure dimension in {knitr}.
  • Part 2: What image format should you use for graphics.
  • Part 3: Including external graphics in your document
  • Part 4: Setting default {knitr} options (this post).

As with many aspects of programming, when you are working by yourself you can be (slightly) more lax with styles and set-up. However, as you start working in a team, different styles can quickly become a hindrance and lead to errors.

Using {knitr} is no different. When you work on documents with different team members, it’s helpful to have a consistent set of settings. If the default for eval changes, this can easily waste time as you try to track down an error. At Jumping Rivers, we use {knitr} a lot. From our training courses, to providing feedback to clients, to constructing monthly reports on clients infrastructure. The great thing about {knitr} is it’s really easy to customise. The bad thing is that without some care, it’s really easy for every member of the team to have different default options. This proliferation of different default options, means that when we pick up someone else document, mistakes may creep in.

We’ve found that to work effectively as team, we need a consistent set of global settings. To be honest, it isn’t really that important what the options are, it’s more crucial that they exist. In this post, we’ll describe the standard {knitr} we have and use.

Do you use Professional Posit Products? If so, check out our managed Posit services

Code Chunks

These options ensure that code chunks perform in a standard manner. The options below are relatively standard

  • echo: for our training courses, this is set toTRUE. For reports to clients, setting this to FALSE usually makes more sense.
  • collapse = TRUE: if possible, collapse all the source/output blocks from multiple chunk into a single block
  • comment = "#>": pre-pend result with #>. Default is ##

Graphics Options

We’ve just had three blog posts on graphics options, so clearly standardisation is a good thing here! Having a consistent set of {knitr} options makes standardising figures across documents (slightly) easier.

These options control the graphics options.

  • fig.path = "graphics/knitr-": create a standard directory where generated figures are stored. For our Hugo blog posts, we set fig.path = "" so the images are stored in the same directory as the md file.
  • fig.width = 6: the default of 7 is little large for most purposes. This is coupled with fig.asp = 0.7 to set the aspect ratio. If you decide you want to set fig.height in a code chunk, you also need to set fig.asp = NULL in that chunk. See the note at the end of this post for a further discussion on aspect ratios.
  • fig.pos = "t": when creating PDF documents, place the figures at the top of the page. Having figures placed where they appear in text can result in poorly pages, with dangling sentences at the bottom/top of the page.
  • fig.align = "center": centre the figure in the document. This gives the graph a more prominent place.
  • fig.retina = 2: use figures suitable for a retina display. The default if 2.
  • dpi differs for HTML and PDF documents. For PDF documents, we set dpi = 300, otherwise, we leave it at 72 (see our previous blog post on optimal figures for information). We can be clever, and set dpi = if (knitr::is_latex_output()) 72 else 300
  • out.width = 100%: We typically set this on a graph by graph basis.

Graphics Device

The graphics device is the default device for used to create and provide graphics. This options is controlled via the dev argument. For our training courses, where we produce PDF booklets of notes we set dev = "cairo_pdf" as the default. For blog post and HTML documents, such as this, we set dev = "svg". We also set dev.args = list(png = list(type = "cairo-png")), so that when we set dev = "png", we use the cairo-png variety.

{knitr} hooks

In our recent blog post on optimising PNG images, we discussed the optipng utility for reducing your file size. It’s actually straightforward to use optipng as a {knitr} hook, i.e. whenever you generate a PNG file, optipng automatically runs.

If you have optipng installed, you simply add the hook

knitr::knit_hooks$set(optipng = knitr::hook_optipng)

to the top of your document. You can also tweak the options via

# The args `-o1 -quiet' are passed to optipng
knitr::opts_chunk$set(optipng = "-o1 -quiet")

Putting it all together

When we put all of the above together, we end up with

knitr::opts_chunk$set(echo = FALSE, 
                      collapse = TRUE,
                      comment = "#>",
                      fig.path = "graphics/knitr-",
                      fig.retina = 2, # Control using dpi
                      fig.width = 6,  # generated images
                      fig.pos = "t",  # pdf mode
                      fig.align = "center",
                      dpi = if (knitr::is_latex_output()) 72 else 300, 
                      out.width = "100%",
                      dev = "svg",
                      dev.args = list(png = list(type = "cairo-png")),
                      optipng = "-o1 -quiet")

This can then be placed in an simple helper package to avoid duplication.

A note on aspect ratio: fig.asp

As with all graphic related options, there isn’t one single setting that is suitable for all situations. While fig.asp=0.7 gives a sensible default, you shouldn’t be frighted to change it. I found this article on when to use different aspect ratios very informative. R for Data Science also has a short section on figure sizing that’s worth reading.


Jumping Rivers Logo

Recent Posts

  • Start 2026 Ahead of the Curve: Boost Your Career with Jumping Rivers Training 
  • Should I Use Figma Design for Dashboard Prototyping? 
  • Announcing AI in Production 2026: A New Conference for AI and ML Practitioners 
  • Elevate Your Skills and Boost Your Career – Free Jumping Rivers Webinar on 20th November! 
  • Get Involved in the Data Science Community at our Free Meetups 
  • Polars and Pandas - Working with the Data-Frame 
  • Highlights from Shiny in Production (2025) 
  • Elevate Your Data Skills with Jumping Rivers Training 
  • Creating a Python Package with Poetry for Beginners Part2 
  • What's new for Python in 2025? 

Top Tags

  • R (236) 
  • Rbloggers (182) 
  • Pybloggers (89) 
  • Python (89) 
  • Shiny (63) 
  • Events (26) 
  • Training (23) 
  • Machine Learning (22) 
  • Conferences (20) 
  • Tidyverse (17) 
  • Statistics (14) 
  • Packages (13) 

Authors

  • Amieroh Abrahams 
  • Aida Gjoka 
  • Shane Halloran 
  • Osheen MacOscar 
  • Keith Newman 
  • Tim Brock 
  • Russ Hyde 
  • Myles Mitchell 
  • Theo Roe 
  • Colin Gillespie 
  • Gigi Kenneth 
  • Sebastian Mellor 
  • Pedro Silva 

Keep Updated

Like data science? R? Python? Stan? Then you’ll love the Jumping Rivers newsletter. The perks of being part of the Jumping Rivers family are:

  • Be the first to know about our latest courses and conferences.
  • Get discounts on the latest courses.
  • Read news on the latest techniques with the Jumping Rivers blog.

We keep your data secure and will never share your details. By subscribing, you agree to our privacy policy.

Follow Us

  • GitHub
  • Bluesky
  • LinkedIn
  • YouTube
  • Eventbrite

Find Us

The Catalyst Newcastle Helix Newcastle, NE4 5TG
Get directions

Contact Us

  • hello@jumpingrivers.com
  • + 44(0) 191 432 4340

Newsletter

Sign up

Events

  • North East Data Scientists Meetup
  • Leeds Data Science Meetup
  • Shiny in Production
British Assessment Bureau, UKAS Certified logo for ISO 9001 - Quality management British Assessment Bureau, UKAS Certified logo for ISO 27001 - Information security management Cyber Essentials Certified Plus badge
  • Privacy Notice
  • |
  • Booking Terms

©2016 - present. Jumping Rivers Ltd