Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use knit_print methods for inline R #1179

Closed
maxheld83 opened this issue Jan 7, 2021 · 1 comment · Fixed by #1193
Closed

use knit_print methods for inline R #1179

maxheld83 opened this issue Jan 7, 2021 · 1 comment · Fixed by #1193
Labels
feature a feature request or enhancement rd ✍️

Comments

@maxheld83
Copy link
Member

maxheld83 commented Jan 7, 2021

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.

@maxheld83 maxheld83 changed the title inline knit_print methods are ignored use knit_print methods for inline R Jan 20, 2021
@maxheld83
Copy link
Member Author

Think this confirms my above hypothesis (well, it says so in the docs, duh 😼):
Inline R is just eval()d, chunks are passed to knitr.

roxygen2/R/markdown.R

Lines 99 to 113 in b3053b6

eval_code_node <- function(node, env) {
if (xml_name(node) == "code") {
text <- str_replace(xml_text(node), "^r ", "")
paste(eval(parse(text = text), envir = env), collapse = "\n")
} else {
text <- paste0("```", xml_attr(node, "info"), "\n", xml_text(node), "```\n")
opts_chunk$set(
error = FALSE,
fig.path = "man/figures/",
fig.process = function(path) basename(path)
)
knit(text = text, quiet = TRUE, envir = env)
}
}

So above is absolutely intended behavior, making this a feature request.

maxheld83 added a commit to maxheld83/roxygen that referenced this issue Jan 21, 2021
maxheld83 added a commit to maxheld83/roxygen that referenced this issue Jan 21, 2021
@hadley hadley added feature a feature request or enhancement rd ✍️ labels Apr 16, 2021
hadley pushed a commit that referenced this issue Apr 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement rd ✍️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants