vinalti
vinalti

Reputation: 1204

Print HTML formatted text in Jupyter with R from the list of citations of loaded packages

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.

Upvotes: 1

Views: 469

Answers (1)

vinalti
vinalti

Reputation: 1204

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

Upvotes: 3

Related Questions