Skip to content

use knit_print methods for inline R #1179

@maxheld83

Description

@maxheld83

Update: On further inspection, the below is a feature request, not a bug report, I think.
Reworded accordingly.


It would be great if inline dynamic R code would respect the knitr::knit_print() method, which in turn can have special behavior for inline=TRUE.

I don't think it currently does.

Here's a reprex:

#' Reprex
#' @inheritParams knitr::knit_print
#' @param inline logical flag, whether print method is used from inside inline or a chunk.
#' @export
#' @section Let's test the print method:
#' 
#' Setting things up:
#' ```{r}
#' library(knitr)
#' x <- structure("I'm the default printing method.", class = "foo")
#' ```
#' 
#' Here's the chunk output:
#' 
#' ```{r}
#' x
#' ```
#' 
#' And inline with manual invocation: `r knitr::knit_print(x, inline = TRUE)`.
#' 
#' And inline without manual invocation: `r x`.
knit_print.foo <- function(x, inline = FALSE, ...) {
  out <- ifelse(inline, "I'm inline R output", "I'm an R chunk output")
  knitr::asis_output(out)
}

And the resulting knit_print.foo.Rd:

% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/reprex.R
\name{knit_print.foo}
\alias{knit_print.foo}
\title{Reprex}
\usage{
\method{knit_print}{foo}(x, inline = FALSE, ...)
}
\arguments{
\item{x}{An R object to be printed}

\item{inline}{logical flag, whether print method is used from inside inline or a chunk.}

\item{...}{Additional arguments passed to the S3 method. Currently ignored,
except two optional arguments \code{options} and \code{inline}; see
the references below.}
}
\description{
Reprex
}
\section{Let's test the print method}{


Setting things up:\if{html}{\out{<div class="r">}}\preformatted{library(knitr)
x <- structure("I'm the default printing method.", class = "foo")
}\if{html}{\out{</div>}}

Here's the chunk output:\if{html}{\out{<div class="r">}}\preformatted{x
}\if{html}{\out{</div>}}

I'm an R chunk output

And inline with manual invocation: I'm inline R output.

And inline without manual invocation: I'm the default printing method..
}

As can be seen in the above *.Rd output, all works fine, except for the inline R code.
Here, roxygen2 seems to fall back to the print() generic, not knit_print() method.

sessioninfo::session_info():

─ Session info ─────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       macOS  11.1                 
 system   x86_64, darwin17.0          
 ui       X11                         
 language en_US.UTF-8 git             
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       Europe/Berlin               
 date     2021-01-08                  

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────
 ! package     * version     date       lib source                            
   assertthat    0.2.1       2019-03-21 [1] CRAN (R 4.0.2)                    
   backports     1.2.1       2020-12-09 [1] CRAN (R 4.0.2)                    
 P biblids     * 0.0.0.9000  2020-12-11 [?] Github (subugoe/biblids@d44010e)  
   callr         3.5.1       2020-10-13 [1] CRAN (R 4.0.2)                    
   checkmate     2.0.0       2020-02-06 [1] CRAN (R 4.0.2)                    
   cli           2.2.0       2020-11-20 [1] CRAN (R 4.0.2)                    
   commonmark    1.7         2018-12-01 [1] CRAN (R 4.0.2)                    
   crayon        1.3.4       2017-09-16 [1] CRAN (R 4.0.2)                    
   desc          1.2.0       2018-05-01 [1] CRAN (R 4.0.2)                    
   devtools    * 2.3.2       2020-09-18 [1] CRAN (R 4.0.2)                    
   digest        0.6.27      2020-10-24 [1] CRAN (R 4.0.2)                    
   ellipsis      0.3.1       2020-05-15 [1] CRAN (R 4.0.2)                    
   evaluate      0.14        2019-05-28 [1] CRAN (R 4.0.1)                    
   fansi         0.4.1       2020-01-08 [1] CRAN (R 4.0.2)                    
   fastmap       1.0.1       2019-10-08 [1] CRAN (R 4.0.2)                    
   fs            1.5.0       2020-07-31 [1] CRAN (R 4.0.2)                    
   glue          1.4.2       2020-08-27 [1] CRAN (R 4.0.2)                    
   htmltools     0.5.0.9003  2020-12-03 [1] Github (rstudio/htmltools@d18bd8e)
   httpuv        1.5.4       2020-06-06 [1] CRAN (R 4.0.2)                    
   httr          1.4.2       2020-07-20 [1] CRAN (R 4.0.2)                    
   jsonlite      1.7.2       2020-12-09 [1] CRAN (R 4.0.2)                    
   knitr       * 1.30        2020-09-22 [1] CRAN (R 4.0.2)                    
   later         1.1.0.1     2020-06-05 [1] CRAN (R 4.0.2)                    
   lifecycle     0.2.0       2020-03-06 [1] CRAN (R 4.0.2)                    
   magrittr      2.0.1       2020-11-17 [1] CRAN (R 4.0.2)                    
   memoise       1.1.0       2017-04-21 [1] CRAN (R 4.0.2)                    
   mime          0.9         2020-02-04 [1] CRAN (R 4.0.2)                    
   muggle      * 0.1.0-2     2020-12-21 [1] Github (subugoe/muggle@756b353)   
   pkgbuild      1.2.0       2020-12-15 [1] CRAN (R 4.0.2)                    
   pkgload       1.1.0       2020-05-29 [1] CRAN (R 4.0.2)                    
   prettyunits   1.1.1       2020-01-24 [1] CRAN (R 4.0.2)                    
   processx      3.4.5       2020-11-30 [1] CRAN (R 4.0.2)                    
   promises      1.1.1.9001  2020-12-11 [1] Github (rstudio/promises@bbadb3d) 
   ps            1.5.0       2020-12-05 [1] CRAN (R 4.0.2)                    
   purrr         0.3.4       2020-04-17 [1] CRAN (R 4.0.2)                    
   R6            2.5.0       2020-10-28 [1] CRAN (R 4.0.2)                    
   Rcpp          1.0.5       2020-07-06 [1] CRAN (R 4.0.2)                    
   remotes       2.2.0       2020-07-21 [1] CRAN (R 4.0.2)                    
   reprex        0.3.0       2019-05-16 [1] CRAN (R 4.0.2)                    
   rlang         0.4.10.9000 2021-01-05 [1] Github (r-lib/rlang@b8d14c3)      
   roxygen2      7.1.1       2020-06-27 [1] CRAN (R 4.0.2)                    
   rprojroot     2.0.2       2020-11-15 [1] CRAN (R 4.0.2)                    
   rstudioapi    0.13        2020-11-12 [1] CRAN (R 4.0.2)                    
   sessioninfo   1.1.1       2018-11-05 [1] CRAN (R 4.0.2)                    
   shiny         1.5.0.9006  2020-12-30 [1] Github (rstudio/shiny@2590cf3)    
   stringi       1.5.3       2020-09-09 [1] CRAN (R 4.0.2)                    
   stringr       1.4.0       2019-02-10 [1] CRAN (R 4.0.2)                    
   testthat    * 3.0.1       2020-12-17 [1] CRAN (R 4.0.2)                    
   usethis     * 2.0.0       2020-12-10 [1] CRAN (R 4.0.2)                    
   vctrs         0.3.6       2020-12-17 [1] CRAN (R 4.0.2)                    
   withr         2.3.0       2020-09-22 [1] CRAN (R 4.0.2)                    
   xfun          0.19        2020-10-30 [1] CRAN (R 4.0.2)                    
   xml2          1.3.2       2020-04-23 [1] CRAN (R 4.0.2)                    
   xtable        1.8-4       2019-04-21 [1] CRAN (R 4.0.2)  

Perhaps this is because roxygen2 is using it's own inline rendering engine?
The docs maybe suggest this:

The value of the R expression is converted to a character string, with paste(collapse = "\n").
So you don’t need explicitly convert to a character value, numeric values or any R object with an as.character() S3 method is fine.
Also, you can insert multiple lines by returning a character vector.
If you want to run R code without inserting any output, return an empty string or NULL.

However, R chunks do seem rely on knitr.

Either way, it'd be great to use the knitr generic here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementrd ✍️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions