Skip to content

Commit

Permalink
switch some vignettes to litedown
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Sep 19, 2024
1 parent b4e20e7 commit eced8f5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 41 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: knitr
Type: Package
Title: A General-Purpose Package for Dynamic Report Generation in R
Version: 1.48.3
Version: 1.48.4
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre"), email = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666")),
person("Abhraneel", "Sarma", role = "ctb"),
Expand Down Expand Up @@ -134,6 +134,7 @@ Suggests:
jpeg,
JuliaCall (>= 0.11.1),
magick,
litedown,
markdown (>= 1.3),
png,
ragg,
Expand All @@ -156,7 +157,7 @@ License: GPL
URL: https://yihui.org/knitr/
BugReports: https://github.com/yihui/knitr/issues
Encoding: UTF-8
VignetteBuilder: knitr
VignetteBuilder: litedown, knitr
SystemRequirements: Package vignettes based on R Markdown v2 or reStructuredText require Pandoc (http://pandoc.org).
The function rst2pdf() requires rst2pdf (https://github.com/rst2pdf/rst2pdf).
Collate:
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

- Changed the format of the reference card from PDF to HTML so building this package will not require LaTeX. See `vignette('knitr-refcard', package = 'knitr')`.

- Switched the vignette engine from **knitr** to `litedown::vignette` for some package vignettes.

# CHANGES IN knitr VERSION 1.48

## BUG FIXES
Expand Down
2 changes: 1 addition & 1 deletion R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ knit = function(
oconc = knit_concord$get(); on.exit(knit_concord$set(oconc), add = TRUE)

if (child_mode()) {
setwd(opts_knit$get('output.dir')) # always restore original working dir
setwd(opts_knit$get('output.dir') %n% '.') # always restore original working dir
# in child mode, input path needs to be adjusted
if (in.file && !is_abs_path(input)) {
input = paste0(opts_knit$get('child.path'), input)
Expand Down
21 changes: 11 additions & 10 deletions vignettes/datatables.Rmd
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
---
title: Display Tables with the JavaScript Library DataTables
author: Yihui Xie
date: "`r Sys.Date()`"
date: "`{r} Sys.Date()`"
output:
markdown::html_format:
litedown::html_format:
meta:
css: ["@default", "@prism-xcode", "https://cdn.datatables.net/1.13.7/css/jquery.dataTables.min.css"]
css: ["@default", "https://cdn.datatables.net/1.13.7/css/jquery.dataTables.min.css"]
js: ["@npm/[email protected]/dist/jquery.min.js", "https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js"]
---

<!--
%\VignetteEngine{knitr::knitr_notangle}
%\VignetteEngine{litedown::vignette}
%\VignetteIndexEntry{Display Tables with the JavaScript Library DataTables}
-->

## jQuery DataTables

We can use the JavaScript library [**DataTables**](https://datatables.net) to generate enhanced tables in HTML. In the example below, we create a table for the `mtcars` data:

```{r cool, results='asis'}
library(knitr)
kable(mtcars, 'html', table.attr='id="mtcars_table"')
::: {#mtcars-table}
```{r cool, print.args=list(data.frame=list(limit=NULL))}
mtcars
```
:::

Note we assigned an `id` to the table, and next we use the **DataTables** library to initialize the table and you will get an interactive table.

```{js}
window.addEventListener('load', () => {
$('#mtcars_table').DataTable();
$('#mtcars-table > table').DataTable();
});
```

By comparison, below is an ordinary table:

```{r boring, results='asis'}
kable(head(mtcars), 'html')
```{r boring}
mtcars
```

This vignette is only a toy example. I'd recommend you to use the **DT** package instead: https://github.com/rstudio/DT
20 changes: 9 additions & 11 deletions vignettes/knit_expand.Rmd
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
title: Templating with knit_expand()
author: Yihui Xie
date: "`r Sys.Date()`"
date: "`{r} Sys.Date()`"
output:
markdown::html_format:
litedown::html_format:
meta:
css: ["@default", "@prism-xcode"]
css: ["@default"]
---

<!--
%\VignetteEngine{knitr::knitr_notangle}
%\VignetteEngine{litedown::vignette}
%\VignetteIndexEntry{Templating with knit_expand()}
-->

Expand Down Expand Up @@ -56,14 +56,12 @@ res = knit_expand(text = c('{{i=0;h2=function(x){i<<-i+1;sprintf("<h2>%d. %s</h2
cat(res)
```

Build regression models based on a template; loop through all variables in `mtcars`:
Build regression models based on a template; loop through some variables in `mtcars`:

```{r results='hide'}
src = lapply(names(mtcars)[-1], function(i) {
knit_expand(text=c("# Regression on {{i}}", '```{r lm-{{i}}}', 'lm(mpg~{{i}}, data=mtcars)', '```'))
```{r, comment=''}
src = lapply(names(mtcars)[2:5], function(i) {
knit_expand(text=c("# Regression on {{i}}", '```{r lm-{{i}}}', 'lm(mpg~{{i}}, data=mtcars)', '```', ''))
})
# knit the source
res = knit_child(text = unlist(src))
res = paste('<pre><code>', gsub('^\\s*|\\s*$', '', res), '</code></pre>', sep = '')
litedown::fuse(unlist(src), 'markdown')
```
`r res`
4 changes: 2 additions & 2 deletions vignettes/knit_print.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ title: "Custom Print Methods"
author: "Yihui Xie"
date: '`r Sys.Date()`'
output:
markdown::html_format:
litedown::html_format:
options:
toc: true
meta:
css: ["@default", "@prism-xcode"]
css: ["@default"]
vignette: >
%\VignetteEngine{knitr::knitr_notangle}
%\VignetteIndexEntry{Custom Print Methods}
Expand Down
21 changes: 10 additions & 11 deletions vignettes/knitr-intro.Rmd
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
---
title: "Not An Introduction to knitr"
author: "Yihui Xie"
date: "`r Sys.Date()`"
date: "`{r} Sys.Date()`"
output:
markdown::html_format:
litedown::html_format:
meta:
css: ["@default", "@prism-xcode"]
js: ["@npm/@xiee/utils/js/center-img.min.js"]
css: ["@default"]
vignette: >
%\VignetteEngine{knitr::knitr_notangle}
%\VignetteEngine{litedown::vignette}
%\VignetteIndexEntry{Not an Introduction to knitr}
---

Expand All @@ -24,18 +23,18 @@ and knitr" for this package, too.
Anyway, here is a code chunk that shows you can compile vignettes with **knitr**
as well using R 3.0.x, which supports non-Sweave vignettes:

```{r show-off, tidy=TRUE}
options(digits=4)
```{r show-off}
options(digits = 4)
rnorm(20)
fit=lm(dist~speed, data=cars)
b=coef(fit)
fit = lm(dist ~ speed, data = cars)
b = coef(fit)
```

```{r echo=FALSE}
knitr::kable(summary(fit)$coefficients)
summary(fit)$coefficients
```

The fitted regression equation is $Y=`r b[1]`+`r b[2]`x$.
The fitted regression equation is $Y=`{r} b[1]`+`{r} b[2]`x$.

```{r graphics, fig.cap='A scatterplot with a regression line.'}
par(mar=c(4, 4, 1, .1))
Expand Down
12 changes: 8 additions & 4 deletions vignettes/knitr-markdown.Rmd
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
---
title: An R Markdown (v1) Vignette with knitr
title: R Markdown (v1) Vignettes with knitr
author: Yihui Xie
date: "`r Sys.Date()`"
date: "`{r} Sys.Date()`"
output:
litedown::html_format:
meta:
css: ["@default"]
---

<!--
%\VignetteEngine{knitr::knitr_notangle}
%\VignetteEngine{litedown::vignette}
%\VignetteIndexEntry{An R Markdown Vignette with knitr}
-->

This is an example of Markdown vignettes in R. Before R 3.0.0, only Sweave/PDF vignettes were supported in R. Markdown is gaining popularity over the years due to its simplicity, and R 3.0.0 starts to support package vignettes written in R Markdown.
Before R 3.0.0, only Sweave/PDF vignettes were supported in R. Markdown is gaining popularity over the years due to its simplicity, and R 3.0.0 starts to support package vignettes written in R Markdown.

> Please note this example is for [R Markdown v1](https://cran.r-project.org/package=markdown) only. If you use [R Markdown v2](https://rmarkdown.rstudio.com), you should use the vignette engine `knitr::rmarkdown` instead of `knitr::knitr`.
Expand Down

0 comments on commit eced8f5

Please sign in to comment.