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.
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)
: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.
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 callcitation(package)
on the top. To get it as html I only found theprint(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:Output: