From c24a7f78ce8b3d07744ee9ac66bbd1959c1db90e Mon Sep 17 00:00:00 2001 From: Vincent Arel-Bundock Date: Thu, 14 Nov 2024 18:30:35 -0500 Subject: [PATCH] Issue282 (#386) * print() inline in posit notebooks * positron support * posit.R * rstudio vs positron * positron doesn't work * rbuildignore --- .Rbuildignore | 2 +- DESCRIPTION | 3 ++- NEWS.md | 1 + R/posit.R | 17 +++++++++++++++++ R/print.R | 11 ++++++++++- man-roxygen/global_options.R | 9 +++++---- man/format_tt.Rd | 1 + man/tt.Rd | 1 + vignettes/options.qmd | 16 +++++++++------- 9 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 R/posit.R diff --git a/.Rbuildignore b/.Rbuildignore index 46ab0604..0c7f534c 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -20,4 +20,4 @@ tinytable_assets ^man/figures/.*\\.png$ ^man/figures/gallery/.*\\.png$ sandbox/ - +vignettes/ diff --git a/DESCRIPTION b/DESCRIPTION index f8cadd13..acd8ed23 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: tinytable Type: Package Title: Simple and Configurable Tables in 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', and 'Typst' Formats Description: Create highly customized tables with this simple and dependency-free package. Data frames can be converted to 'HTML', 'LaTeX', 'Markdown', 'Word', 'PNG', 'PDF', or 'Typst' tables. The user interface is minimalist and easy to learn. The syntax is concise. 'HTML' tables can be customized using the flexible 'Bootstrap' framework, and 'LaTeX' code with the 'tabularray' package. -Version: 0.5.0.6 +Version: 0.5.0.7 Imports: methods Depends: @@ -20,6 +20,7 @@ Suggests: pandoc, quarto, rmarkdown, + rstudioapi, scales, stringi, tibble, diff --git a/NEWS.md b/NEWS.md index e5563557..42f03f03 100644 --- a/NEWS.md +++ b/NEWS.md @@ -7,6 +7,7 @@ * `style_tt()`: the `i` and `j` indices are now consistent in all formats. They refer to rows *after* the insertion of row groups. * `save_tt()` respects `options(tinytable_save_overwrite=TRUE)` * LaTeX: Guard header rows when using `style_tt(align="d")`. Issue #367 +* Inline display in Quarto and Rmarkdown notebooks. `options(tinytable_print_rstudio_notebook = "inline")` or `"viewer"` Bugs: diff --git a/R/posit.R b/R/posit.R new file mode 100644 index 00000000..9d184dbc --- /dev/null +++ b/R/posit.R @@ -0,0 +1,17 @@ +# hack to detect Rstudio vs. Positron +is_rstudio <- function() { + flag <- tryCatch(rstudioapi::getThemeInfo(), error = function(e) FALSE) + !isFALSE(flag) +} + +is_rstudio_notebook <- function() { + flag <- FALSE + # inline doesn't work in Positron + if (is_rstudio()) { + con <- rstudioapi::getActiveDocumentContext()[["path"]] + if (isTRUE(grepl("\\.qmd$|\\.Rmd$", con))) { + flag <- TRUE + } + } + return(flag) +} diff --git a/R/print.R b/R/print.R index ae104caa..4a1b430c 100644 --- a/R/print.R +++ b/R/print.R @@ -36,7 +36,6 @@ knit_print.tinytable <- function(x, return(out) } - #' Print, display, or convert a tinytable object #' #' This function is called automatically by `R` whenever a `tinytable` object is anprinted to the console or in an HTML viewer pane. @@ -74,6 +73,16 @@ print.tinytable <- function(x, if (output %in% c("latex", "typst", "markdown", "gfm")) { cat(tab, "\n") } else if (output == "html") { + if (is_rstudio_notebook()) { + tinytable_print_rstudio <- getOption("tinytable_print_rstudio_notebook", default = "inline") + assert_choice(tinytable_print_rstudio, c("inline", "viewer")) + if (tinytable_print_rstudio == "inline") { + tab = sprintf("\n```{=html}\n%s\n```\n`", tab) + print(knitr::asis_output(tab)) + return(invisible(x)) + } + } + # need to change the output directory to a temporary directory # for plot_tt() inline plots to show up in RStudio htmlFile <- file.path(dir, "index.html") diff --git a/man-roxygen/global_options.R b/man-roxygen/global_options.R index 499abf8b..520fe5af 100644 --- a/man-roxygen/global_options.R +++ b/man-roxygen/global_options.R @@ -7,11 +7,12 @@ #' https://vincentarelbundock.github.io/tinytable/vignettes/options.html #' #' ## Quarto -#' +#' #' ### Figure environment -#' -#' * `options("tinytable_quarto_figure" = FALSE)`: Typst only. Normally, it is best to allow Quarto to define the figure environment, so the default behavior is to not include one. -#' +#' +#' * `options("tinytable_quarto_figure" = FALSE)`: Typst only. Normally, it is best to allow Quarto to define the figure environment, so the default behavior is to not include one. +#' * `options(tinytable_print_rstudio_notebook = "inline")`: Display tables "inline" or in the "viewer" in RStudio notebooks. +#' #' ### Data Processing #' #' The `format_tt(quarto=TRUE)` argument activates Quarto data processing for specific cells. This funcationality comes with a few warnings: diff --git a/man/format_tt.Rd b/man/format_tt.Rd index d294f19a..a3d50df9 100644 --- a/man/format_tt.Rd +++ b/man/format_tt.Rd @@ -107,6 +107,7 @@ https://vincentarelbundock.github.io/tinytable/vignettes/options.html \subsection{Figure environment}{ \itemize{ \item \code{options("tinytable_quarto_figure" = FALSE)}: Typst only. Normally, it is best to allow Quarto to define the figure environment, so the default behavior is to not include one. +\item \code{options(tinytable_print_rstudio_notebook = "inline")}: Display tables "inline" or in the "viewer" in RStudio notebooks. } } diff --git a/man/tt.Rd b/man/tt.Rd index 4a64036d..6228189c 100644 --- a/man/tt.Rd +++ b/man/tt.Rd @@ -100,6 +100,7 @@ https://vincentarelbundock.github.io/tinytable/vignettes/options.html \subsection{Figure environment}{ \itemize{ \item \code{options("tinytable_quarto_figure" = FALSE)}: Typst only. Normally, it is best to allow Quarto to define the figure environment, so the default behavior is to not include one. +\item \code{options(tinytable_print_rstudio_notebook = "inline")}: Display tables "inline" or in the "viewer" in RStudio notebooks. } } diff --git a/vignettes/options.qmd b/vignettes/options.qmd index d15f7897..28917663 100644 --- a/vignettes/options.qmd +++ b/vignettes/options.qmd @@ -103,16 +103,18 @@ The `format_tt(quarto=TRUE)` argument activates Quarto data processing for speci * `options(tinytable_quarto_disable_processing = TRUE)` - disables Quarto processing of cell content. +* `options(tinytable_print_rstudio_notebook = "inline")` or `"viewer"` + - Display tables inline in the notebook or in the viewer. Inline is an RStudio-only feature. * `options(tinytable_quarto_figure = FALSE)` - by default, do not include Typst tables in a figure environment in Quarto. - Setting this global option to `FALSE` may lead to conflicts with some `tinytable` features, but it also allows use of markdown and Quarto-specific code in table cells, such as cross-references. +Setting this global option to `FALSE` may lead to conflicts with some `tinytable` features, but it also allows use of markdown and Quarto-specific code in table cells, such as cross-references. - ```r - x <- data.frame(Math = "x^2^", Citation = "@Lovelace1842") - fn <- function(z) sprintf("", z) - tt(x) |> format_tt(i = 1, fn = fn) - ``` +```r +x <- data.frame(Math = "x^2^", Citation = "@Lovelace1842") +fn <- function(z) sprintf("", z) +tt(x) |> format_tt(i = 1, fn = fn) +``` - See this link for more details: https://quarto.org/docs/authoring/tables.html#disabling-quarto-table-processing +See this link for more details: https://quarto.org/docs/authoring/tables.html#disabling-quarto-table-processing