$ show yihui/knitr-examples
This repository is a comprehensive collection of examples for the **knitr** R package, demonstrating features such as chunk options, hooks, multiple language engines (Python, Bash, C++, etc.), caching, child documents, and output formats (Markdown, LaTeX, HTML, reST, etc.). It serves as both a tutorial for new users and an integration test suite for the knitr package itself. The examples are self-contained, using built-in R datasets (e.g., iris, mtcars, cars) and simple computational tasks. While not a production application or data pipeline, the repo provides a valuable reference for literate programming and reproducible research with R. It is actively maintained by the knitr author and community.
The LLM identified 3 files worth highlighting for reuse:
Demonstrates how to create a custom engine for knitr using the `exec` engine, which allows executing arbitrary commands on code chunk content. Includes a detailed example of building a `gcc` engine that compiles and runs C code, showcasing the use of options like `command`, `ext`, `args`, and `output`. This pattern is reusable for integrating other command-line tools into knitr documents.
Shows how to redefine knitr's output hook to truncate long output to a specified number of lines via a custom chunk option `out.lines`. The hook uses `head()` to keep only the first `n` lines and appends '....' to indicate truncation. This pattern is reusable in any knitr document to control verbosity of long outputs.
Illustrates the use of `knit_expand()` to create parameterized LaTeX templates with placeholders like `{{i}}` and `{{xvar}}`. The example expands a template for each row of the iris dataset and for each predictor variable in mtcars, demonstrating how to generate repetitive reports programmatically. This pattern is directly reusable for generating multiple similar sections or analyses.