0

I have a R variable containing some html content inside, for exemple :

myvar = "<h3>Section Title</h3>"

I would like to print it not as <h3>Section Title</h3> but as a formated h3 title in html or markdown that jupyter notebook understands.

I have been looking a bit everywhere and found htmlwidget or knitr but I feel like it needs a HTML file. What I would like to do is just displaying a variable. I have tried also with htmltools package and the HTML() function, but no success...

myvar = "<h3>Section Title</h3>"
# I would expect that I can do something like:
print(HTML(myvar), format='html')
# But it doesn't work, I just get:
# => <h3>Section Title</h3>

And to be very clear, the goal is also that when I save the notebook as HTML or as PDF, the html is displayed formatted (and not as raw text)

In Fine the goal is to display the citations for loaded packages, in a aesthetic way. I know that I can use print(citation("packagename"), style='html') to output it as HTML but I can't find how to format the HTML properly. That's the reason my idea is to capture this output in a variable and output it as formatted HTML.

vinalti
  • 966
  • 5
  • 26
  • As an aside, you should rarely, *if ever*, explicitly call `print`. The function exists solely as a hook that can be overridden by custom S3 classes to make them auto-printable on the terminal. Unless ‘htmlwidgets’ is badly designed (and I don’t believe it is), just `HTML(myvar)` (instead of the entire `print(…)` expression you wrote) should do the trick. However, whether it actually works inside a Jupyter document I don’t know. – Konrad Rudolph Apr 20 '22 at 09:25
  • Thank you for your answer, Unfortunately `HTML("

    Test

    ")` only displays `'

    Test

    '`. Same result but without the quotes if used in terminal: `

    Test

    `. (just a note that the `HTML()` function comes from `htmltools` package)
    – vinalti Apr 20 '22 at 09:33
  • Like `IRdisplay::display_html(myvar)`? – krassowski Apr 20 '22 at 09:53
  • Does this answer your question? [R - how to embed HTML code into Jupyter notebook output?](https://stackoverflow.com/questions/37793888/r-how-to-embed-html-code-into-jupyter-notebook-output) – krassowski Apr 20 '22 at 10:12
  • Thanks for your tips, I found the solution by myself. I found the doc of [htmltools (pdf)](https://cran.r-project.org/web/packages/htmltools/htmltools.pdf) and the function `htmlTemplate()` is actually returning an object displayed as HTML in Jupyter. I self answered my question for anyone who would look for it. – vinalti Apr 20 '22 at 10:20

1 Answers1

1

So I found the simplest option (I think) which doesn't require any other packages than htmltools which I think is by default installed.

How to display and format HTML in R

The key is the function htmlTemplate(text_ = var) :

myvar ="<h3>Test Title</h3>"
htmlTemplate(text_ = var)

This will be displayed as raw HTML in the console, but on a Jupyter Notebook it will be displayed formatted. I didn't Try it on RStudio but I would expect it to be displayed correctly as well (anyone to confirm ?)

Another solution to display the html content (as mentionned by @krassowski) Nevertheless this works only in Jupyter Notebook.

# We still need to call `HTML()` function inside
IRdisplay::display_html( HTML(myvar) )

How to pretty print citations for loaded packages in R

My final goal was to display the citations for loaded R packages in HTML Using the .packages() I could retrieve the list of attached packages. Then I loop on the list and for each package name (str) I can call citation(package) on the top. To get it as html I only found the print(citation(package), style='html').
Then we need to capture the output of this into a variable. The function capture.output(func) do the trick. See below:

library('htmltools')

# Create a function to output the html of all packages
list_pkgs_html = function(){
    # Remove warning for the time of the function
    w = getOption("warn") 
    options(warn = -1)
    # For each package loaded
    for (p in .packages()){
       # Catch error in case there is no citation for one of them.
       tryCatch({
           # Display the name of the package as a title h3
           cat("<h3>", p, '</h3>\n')
           # Print the citation in html style
           print(citation(p), style = "html")
       })
    }
    # Restore the warning as before
    options(warn = w)
}

# Call the function and capture the output to a variable.
capture.output(list_pkgs_html()) -> myvar

# Display the html result.
htmlTemplate(text_ = myvar)

Output: Result

vinalti
  • 966
  • 5
  • 26
  • 1
    Beware that `.packages()` does *not* contain the loaded packages. It only contains *attached* packages, which is not the same (especially when following best practices and not attaching all packages). Use `loadedNamespaces()` to get all loaded packages. Unfortunately R doesn’t provide a way of distinguishing between packages that you directly depend on and transitive dependencies. – Konrad Rudolph Apr 20 '22 at 14:26