I just started using Jupyter with R, and I'm wondering if there's a good way to display HTML or LaTeX output.
我刚开始使用Jupyter和R,我想知道是否有一种很好的方式来显示HTML或LaTeX输出。
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?
它只是将其显示为纯文本,而不是呈现HTML。有没有办法改变这种行为?
4 个解决方案
#1
12
A combination of repr
(for setting options) and IRdisplay
will work for HTML. Others may know about latex.
repr(用于设置选项)和IRdisplay的组合将适用于HTML。其他人可能知道乳胶。
# 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>")
#2
4
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.,
如果你调用xtable而不将它包装在打印调用中,那么它完全有效。例如。,
library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)
#3
2
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.
在Jupyter中,您可以使用Markdown。只需确保将Jupyter单元格从代码单元格更改为Markdown单元格。完成此操作后,您只需在LaTex之前和之后放置一个双美元符号(“$$”)即可。然后运行单元格。
The steps are as follows: 1. Create a Markdown cell. 2. $$ some LaTex $$ 3. Press play button within Jupyter.
步骤如下:1。创建Markdown单元格。 2. $$一些LaTex $$ 3.按Jupyter中的播放按钮。
#4
0
Defining the following function in the session will display objects returned by xtable as html generated by xtable:
在会话中定义以下函数将显示xtable返回的对象作为xtable生成的html:
repr_html.xtable <- function(obj, ...){
paste(capture.output(print(obj, type = 'html')), collapse="", sep="")
}
library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)
Without the repr_html.xtable
function, because the returned object is also of class data.frame
, the display system in the kernel will rich display that object (=html table) via repr::repr_html.data.frame
.
如果没有repr_html.xtable函数,因为返回的对象也是类data.frame,内核中的显示系统将通过repr :: repr_html.data.frame丰富显示该对象(= html表)。
Just don't print(...)
the object :-)
只是不要打印(...)对象:-)
#1
12
A combination of repr
(for setting options) and IRdisplay
will work for HTML. Others may know about latex.
repr(用于设置选项)和IRdisplay的组合将适用于HTML。其他人可能知道乳胶。
# 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>")
#2
4
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.,
如果你调用xtable而不将它包装在打印调用中,那么它完全有效。例如。,
library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)
#3
2
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.
在Jupyter中,您可以使用Markdown。只需确保将Jupyter单元格从代码单元格更改为Markdown单元格。完成此操作后,您只需在LaTex之前和之后放置一个双美元符号(“$$”)即可。然后运行单元格。
The steps are as follows: 1. Create a Markdown cell. 2. $$ some LaTex $$ 3. Press play button within Jupyter.
步骤如下:1。创建Markdown单元格。 2. $$一些LaTex $$ 3.按Jupyter中的播放按钮。
#4
0
Defining the following function in the session will display objects returned by xtable as html generated by xtable:
在会话中定义以下函数将显示xtable返回的对象作为xtable生成的html:
repr_html.xtable <- function(obj, ...){
paste(capture.output(print(obj, type = 'html')), collapse="", sep="")
}
library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)
Without the repr_html.xtable
function, because the returned object is also of class data.frame
, the display system in the kernel will rich display that object (=html table) via repr::repr_html.data.frame
.
如果没有repr_html.xtable函数,因为返回的对象也是类data.frame,内核中的显示系统将通过repr :: repr_html.data.frame丰富显示该对象(= html表)。
Just don't print(...)
the object :-)
只是不要打印(...)对象:-)