如何在 Jupyter (R) 中渲染 LaTeX / HTML?
How to render LaTeX / HTML in Jupyter (R)?
我刚开始在 R 中使用 Jupyter,我想知道是否有显示 HTML 或 LaTeX 输出的好方法。
下面是一些我希望有效的示例代码:
library(xtable)
x <- runif(500, 1, 50)
y <- x + runif(500, -5, 5)
model <- lm(y~x)
print(xtable(model), type = 'html')
而不是渲染 HTML,它只是将其显示为纯文本。有什么办法可以改变这种行为吗?
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>")
对于最初的简单用例,我找到了一个更简单的答案。
如果您调用 xtable 而不将其包装在打印调用中,那么它完全可以工作。例如,
library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)
在 Jupyter 中,您可以使用 Markdown。只要确保将 Jupyter 单元格从代码单元格更改为 Markdown 单元格即可。完成此操作后,您只需在您拥有的 LaTex 前后放置一个双美元符号 ("$$")。然后 运行 单元格。
步骤如下:
1. 创建一个 Markdown 单元格。
2. $$ 一些 LaTex $$
3. 在 Jupyter 中按下播放按钮。
在会话中定义以下函数会将 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)
没有repr_html.xtable
函数,因为返回的对象也是classdata.frame
,内核中的显示系统会丰富显示那个对象(=html table) 通过 repr::repr_html.data.frame
.
只是不要 print(...)
对象 :-)
Render/Embed html/Latex table 到 IR 内核 jupyter
R 中的某些包以 html 格式提供 tables,如 "knitr",因此如果您想将此 tables 放在笔记本:
library(knitr)
library(kableExtra)
library(IRdisplay) #the package that you need
#we create the table
dt <- mtcars[1:5, 1:6]
options(knitr.table.format = "html")
html_table= kable(dt) %>%
kable_styling("striped") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2))
#We put the table in our notebook
display_html(toString(html_table))
或者例如如果你有一个文件
display_latex(file = "your file path")
我刚开始在 R 中使用 Jupyter,我想知道是否有显示 HTML 或 LaTeX 输出的好方法。
下面是一些我希望有效的示例代码:
library(xtable)
x <- runif(500, 1, 50)
y <- x + runif(500, -5, 5)
model <- lm(y~x)
print(xtable(model), type = 'html')
而不是渲染 HTML,它只是将其显示为纯文本。有什么办法可以改变这种行为吗?
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>")
对于最初的简单用例,我找到了一个更简单的答案。
如果您调用 xtable 而不将其包装在打印调用中,那么它完全可以工作。例如,
library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)
在 Jupyter 中,您可以使用 Markdown。只要确保将 Jupyter 单元格从代码单元格更改为 Markdown 单元格即可。完成此操作后,您只需在您拥有的 LaTex 前后放置一个双美元符号 ("$$")。然后 运行 单元格。
步骤如下: 1. 创建一个 Markdown 单元格。 2. $$ 一些 LaTex $$ 3. 在 Jupyter 中按下播放按钮。
在会话中定义以下函数会将 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)
没有repr_html.xtable
函数,因为返回的对象也是classdata.frame
,内核中的显示系统会丰富显示那个对象(=html table) 通过 repr::repr_html.data.frame
.
只是不要 print(...)
对象 :-)
Render/Embed html/Latex table 到 IR 内核 jupyter
R 中的某些包以 html 格式提供 tables,如 "knitr",因此如果您想将此 tables 放在笔记本:
library(knitr)
library(kableExtra)
library(IRdisplay) #the package that you need
#we create the table
dt <- mtcars[1:5, 1:6]
options(knitr.table.format = "html")
html_table= kable(dt) %>%
kable_styling("striped") %>%
add_header_above(c(" " = 1, "Group 1" = 2, "Group 2" = 2, "Group 3" = 2))
#We put the table in our notebook
display_html(toString(html_table))
或者例如如果你有一个文件
display_latex(file = "your file path")