Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render LaTeX / HTML in Jupyter (R)?

Tags:

html

r

xml

jupyter

I just started using Jupyter with R, and I'm wondering if there's a good way to display HTML or LaTeX output.

Here's some example code that I wish worked:

library(xtable)
x <- runif(500, 1, 50)
y <- x + runif(500, -5, 5)
model <- lm(y~x)
print(xtable(model), type = 'html')

Instead of rendering the HTML, it just displays it as plaintext. Is there any way to change that behavior?

like image 658
Jeremy Avatar asked Sep 21 '15 20:09

Jeremy


3 Answers

A combination of repr (for setting options) and IRdisplay will work for HTML. Others may know about latex.

# Cell 1 ------------------------------------------------------------------

library(xtable)
library(IRdisplay)
library(repr)

data(tli)
tli.table <- xtable(tli[1:20, ])
digits(tli.table) <- matrix( 0:4, nrow = 20, ncol = ncol(tli)+1 )

options(repr.vector.quote=FALSE)

display_html(paste(capture.output(print(head(tli.table), type = 'html')), collapse="", sep=""))


# Cell 2 ------------------------------------------------------------------

display_html("<span style='color:red; float:right'>hello</span>")

# Cell 3 ------------------------------------------------------------------

display_markdown("[this](http://google.com)")

# Cell 4 ------------------------------------------------------------------

display_png(file="shovel-512.png")

# Cell 5 ------------------------------------------------------------------

display_html("<table style='width:20%;border:1px solid blue'><tr><td style='text-align:right'>cell 1</td></tr></table>")

enter image description here

like image 113
hrbrmstr Avatar answered Oct 10 '22 07:10

hrbrmstr


I found a simpler answer, for the initial, simple use case.

If you call xtable without wrapping it in a call to print, then it totally works. E.g.,

library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)
like image 4
Jeremy Avatar answered Oct 10 '22 08:10

Jeremy


In Jupyter, you can use Markdown. Just be sure to change the Jupyter cell from a code cell to a Markdown cell. Once you have done this you can simply place a double dollar sign ("$$") before and after the LaTex you have. Then run the cell.

The steps are as follows: 1. Create a Markdown cell. 2. $$ some LaTex $$ 3. Press play button within Jupyter.

like image 2
Student Avatar answered Oct 10 '22 06:10

Student