Skip to content

Commit 3fbe0a4

Browse files
committed
support df-print option for knitr engine (defaults to kable)
1 parent a01a3cd commit 3fbe0a4

File tree

11 files changed

+135
-11
lines changed

11 files changed

+135
-11
lines changed

src/config/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const kFigFormat = "fig-format";
1919
export const kFigDpi = "fig-dpi";
2020

2121
export const kMermaidFormat = "mermaid-format";
22+
export const kDfPrint = "df-print";
2223

2324
export const kCache = "cache";
2425
export const kFreeze = "freeze";
@@ -93,6 +94,7 @@ export const kExecuteDefaultsKeys = [
9394
kFigFormat,
9495
kFigDpi,
9596
kMermaidFormat,
97+
kDfPrint,
9698
kError,
9799
kEval,
98100
kEngine,

src/config/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
kCrossrefTblTitle,
4747
kCrossrefThmTitle,
4848
kCss,
49+
kDfPrint,
4950
kEcho,
5051
kEngine,
5152
kEnvironmentProofTitle,
@@ -344,7 +345,8 @@ export interface FormatExecute {
344345
[kFigHeight]?: number;
345346
[kFigFormat]?: "retina" | "png" | "jpeg" | "svg" | "pdf";
346347
[kFigDpi]?: number;
347-
[kMermaidFormat]?: "png" | "svg";
348+
[kMermaidFormat]?: "png" | "svg" | "js";
349+
[kDfPrint]?: "default" | "kable" | "tibble" | "paged";
348350
[kCache]?: true | false | "refresh" | null;
349351
[kFreeze]?: true | false | "auto";
350352
[kExecuteEnabled]?: true | false | null;

src/format/formats-shared.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
kCodeOverflow,
1616
kCodeTools,
1717
kDefaultImageExtension,
18+
kDfPrint,
1819
kEcho,
1920
kError,
2021
kEval,
@@ -54,6 +55,7 @@ import {
5455
kLinkExternalIcon,
5556
kLinkExternalNewwindow,
5657
kMergeIncludes,
58+
kMermaidFormat,
5759
kOutput,
5860
kOutputDivs,
5961
kOutputExt,
@@ -173,6 +175,8 @@ function defaultFormat(): Format {
173175
[kFigHeight]: 5,
174176
[kFigFormat]: "png",
175177
[kFigDpi]: 96,
178+
[kMermaidFormat]: undefined,
179+
[kDfPrint]: "kable",
176180
[kError]: false,
177181
[kEval]: true,
178182
[kCache]: null,

src/resources/editor/tools/vs-code.mjs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15594,7 +15594,27 @@ var require_yaml_intelligence_resources = __commonJS({
1559415594
description: "Change the presentation direction to be RTL\n"
1559515595
}
1559615596
],
15597-
"schema/document-tables.yml": [],
15597+
"schema/document-tables.yml": [
15598+
{
15599+
name: "df-print",
15600+
schema: {
15601+
enum: [
15602+
"default",
15603+
"kable",
15604+
"tibble",
15605+
"paged"
15606+
]
15607+
},
15608+
tags: {
15609+
engine: "knitr"
15610+
},
15611+
default: "kable",
15612+
description: {
15613+
short: "Method used to print tables in Knitr engine documents (`default`,\n`kable`, `tibble`, or `paged`). Defaults to `kable`.\n",
15614+
long: "Method used to print tables in Knitr engine documents:\n\n- `default`: Use the default S3 method for the data frame.\n- `kable`: Markdown table using the `knitr::kable()` function.\n- `tibble`: Plain text table using the `tibble` package.\n- `paged`: HTML table with paging for row and column overflow.\n\nThe default printing method is `kable`.\n"
15615+
}
15616+
}
15617+
],
1559815618
"schema/document-text.yml": [
1559915619
{
1560015620
name: "wrap",
@@ -18960,7 +18980,11 @@ var require_yaml_intelligence_resources = __commonJS({
1896018980
"Download buttons for other formats to include on navbar or sidebar\n(one or more of <code>pdf</code>, <code>epub</code>, and\n<code>docx</code>)",
1896118981
"Download buttons for other formats to include on navbar or sidebar\n(one or more of <code>pdf</code>, <code>epub</code>, and\n<code>docx</code>)",
1896218982
"Custom tools for navbar or sidebar",
18963-
"internal-schema-hack"
18983+
"internal-schema-hack",
18984+
{
18985+
short: "Method used to print tables in Knitr engine documents\n(<code>default</code>, <code>kable</code>, <code>tibble</code>, or\n<code>paged</code>). Defaults to <code>kable</code>.",
18986+
long: "Method used to print tables in Knitr engine documents:"
18987+
}
1896418988
],
1896518989
"schema/external-schemas.yml": [
1896618990
{

src/resources/editor/tools/yaml/web-worker.js

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/resources/editor/tools/yaml/yaml-intelligence-resources.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8573,7 +8573,27 @@
85738573
"description": "Change the presentation direction to be RTL\n"
85748574
}
85758575
],
8576-
"schema/document-tables.yml": [],
8576+
"schema/document-tables.yml": [
8577+
{
8578+
"name": "df-print",
8579+
"schema": {
8580+
"enum": [
8581+
"default",
8582+
"kable",
8583+
"tibble",
8584+
"paged"
8585+
]
8586+
},
8587+
"tags": {
8588+
"engine": "knitr"
8589+
},
8590+
"default": "kable",
8591+
"description": {
8592+
"short": "Method used to print tables in Knitr engine documents (`default`,\n`kable`, `tibble`, or `paged`). Defaults to `kable`.\n",
8593+
"long": "Method used to print tables in Knitr engine documents:\n\n- `default`: Use the default S3 method for the data frame.\n- `kable`: Markdown table using the `knitr::kable()` function.\n- `tibble`: Plain text table using the `tibble` package.\n- `paged`: HTML table with paging for row and column overflow.\n\nThe default printing method is `kable`.\n"
8594+
}
8595+
}
8596+
],
85778597
"schema/document-text.yml": [
85788598
{
85798599
"name": "wrap",
@@ -11939,7 +11959,11 @@
1193911959
"Download buttons for other formats to include on navbar or sidebar\n(one or more of <code>pdf</code>, <code>epub</code>, and\n<code>docx</code>)",
1194011960
"Download buttons for other formats to include on navbar or sidebar\n(one or more of <code>pdf</code>, <code>epub</code>, and\n<code>docx</code>)",
1194111961
"Custom tools for navbar or sidebar",
11942-
"internal-schema-hack"
11962+
"internal-schema-hack",
11963+
{
11964+
"short": "Method used to print tables in Knitr engine documents\n(<code>default</code>, <code>kable</code>, <code>tibble</code>, or\n<code>paged</code>). Defaults to <code>kable</code>.",
11965+
"long": "Method used to print tables in Knitr engine documents:"
11966+
}
1194311967
],
1194411968
"schema/external-schemas.yml": [
1194511969
{

src/resources/rmd/execute.R

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,20 @@ execute <- function(input, format, tempDir, libDir, dependencies, cwd, params, r
101101
NULL
102102
}
103103

104+
# determine df_print
105+
df_print <- format$execute$`df-print`
106+
if (df_print == "paged" && !is_html_output(format) && !format$render$`prefer-html`) {
107+
df_print <- "kable"
108+
}
109+
104110
# synthesize rmarkdown output format
105111
output_format <- rmarkdown::output_format(
106112
knitr = knitr,
107113
pandoc = pandoc_options(format),
108114
post_knit = post_knit,
109115
keep_md = FALSE,
110-
clean_supporting = TRUE
116+
clean_supporting = TRUE,
117+
df_print = df_print
111118
)
112119

113120
# FIXME this test isn't failing in shiny mode, but it doesn't look to be
@@ -137,6 +144,11 @@ execute <- function(input, format, tempDir, libDir, dependencies, cwd, params, r
137144
else
138145
character()
139146

147+
# ammend knit_meta with paged table if df_print == "paged"
148+
if (df_print == "paged") {
149+
knit_meta <- append(knit_meta, list(rmarkdown::html_dependency_pagedtable()))
150+
}
151+
140152
# see if we are going to resolve knit_meta now or later
141153
if (dependencies) {
142154
engineDependencies <- NULL
@@ -470,6 +482,9 @@ extract_preserve_chunks <- function(output_file, format) {
470482
}
471483
}
472484

485+
is_html_output <- function(format) {
486+
knitr::is_html_output(format$pandoc$to, c("markdown", "epub", "gfm", "commonmark", "commonmark_x", "markua"))
487+
}
473488

474489
# apply patches to output as required
475490
apply_patches <- function(format, includes) {

src/resources/rmd/hooks.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,16 @@ normalize_options <- function(options) {
655655
"attr-message",
656656
"attr-warning",
657657
"attr-error",
658+
# Paged tables
659+
"max-print",
660+
"sql-max-print",
661+
"paged-print",
662+
"rows-print",
663+
"cols-print",
664+
"cols-min-print",
665+
"pages-print",
666+
"paged-print",
667+
"rownames-print",
658668
# Code decoration
659669
"tidy-opts",
660670
"class-source",

src/resources/rmd/patch.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ wrap_asis_output <- function(options, x) {
101101
# (necessary b/c we no longer do this by overriding kable_html,
102102
# which is in turn necessary to allow kableExtra to parse
103103
# the return value of kable_html as valid xml)
104-
if (grepl("^<\\w+[ >]", x) && grepl("<\\/\\w+>\\s*$", x)) {
104+
if (grepl("^<\\w+[ >]", x) && grepl("<\\/\\w+>\\s*$", x) &&
105+
!grepl('^<div class="kable-table">', x)) {
105106
x <- paste0("`````{=html}\n", x, "\n`````")
106107
}
107108

src/resources/rmd/rmd.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
postprocess <- function(input, format, output, preserved_chunks) {
3535

3636
# check for html output
37-
isHTML <- knitr::is_html_output(format$pandoc$to, c("markdown", "epub"))
37+
isHTML <- is_html_output(format)
3838

3939
# bail if we don't have any perserved chunks and aren't doing code linking
4040
code_link <- isHTML && isTRUE(format$render$`code-link`)

0 commit comments

Comments
 (0)