1

I have functions that create html and markdown. For html I can make the html rendered correctly inside a Quarto or RMarkdown chunk by passing the created character strings through htmltools::HTML(), and to make them rendered from the console I just use htmltools::browsable(htmltools(HTML(strings))). When I produce markdown, e.g. a $\LaTeX$ math expression such as $\alpha$ I can render in a chunk by passing the string through knitr::asis_output. For both approaches I need not bother with results='asis' in the chunk header. When outside a chunk the html is rendered in the RStudio View panel or by popping up your default browser if not in RStudio.

Is there a corresponding way to render constructed markdown strings in a view window from the console?

shafee
  • 15,566
  • 3
  • 19
  • 47
Frank Harrell
  • 1,954
  • 2
  • 18
  • 36
  • 4
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 09 '23 at 14:57
  • 1
    You want to render it and view it at the same time or just render it into html? try the `rmarkdown::render` function – Onyambu Feb 09 '23 at 15:15
  • I want to render the small character vector containing markdown (especially math) and to have the result appear in a browser popup (if not under RStudio) or in the RStudio Viewer panel. – Frank Harrell Feb 09 '23 at 18:39
  • 1
    Use of `rmarkdown::render` as suggested by @onyambu worked great. I wrote a little function wrote the input strings to a temporary file, run that function, `readLines()` to get back into character vectors, and `htmltools::browsable(htmltools::HTML))` the result of that. I had to prepend a little `RMarkdown` YAML header to the strings before passing to `render`. – Frank Harrell Feb 09 '23 at 23:53
  • rendHTML <- function(x, html=TRUE) { x <- paste(x, collapse='\n') if(length(getOption('knitr.in.progress'))) { if(html) x <- paste0('```{=html}\n', x, '\n```') return(knitr::asis_output(x)) } if(! html) { # Convert from RMarkdown to html tf <- tempfile(fileext='.Rmd') o <- tempfile(fileext='.html') cat('---\ntitle: ""\npagetitle: x\noutput: html_document\n---\n', x, '\n', sep='', file=tf) rmarkdown::render(tf, output_file=o, quiet=TRUE) x <- readLines(o) } print(htmltools::browsable(htmltools::HTML(x))) } – Frank Harrell Feb 10 '23 at 20:53

0 Answers0