简体   繁体   English

如何在 Jupyter (R) 中渲染 LaTeX / HTML?

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

I just started using Jupyter with R, and I'm wondering if there's a good way to display HTML or LaTeX output.我刚开始在 R 中使用 Jupyter,我想知道是否有一种显示 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.它不呈现 HTML,而是将其显示为纯文本。 Is there any way to change that behavior?有什么办法可以改变这种行为吗?

A combination of repr (for setting options) and IRdisplay will work for HTML. repr (用于设置选项)和IRdisplay适用于 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>")

在此处输入图片说明

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.如果您调用 xtable 而不将其包装在打印调用中,那么它完全可以工作。 Eg,例如,

library(xtable)
data(cars)
model <- lm(speed ~ ., data = cars)
xtable(model)

In Jupyter, you can use Markdown.在 Jupyter 中,您可以使用 Markdown。 Just be sure to change the Jupyter cell from a code cell to a Markdown cell.只需确保将 Jupyter 单元格从代码单元格更改为 Markdown 单元格。 Once you have done this you can simply place a double dollar sign ("$$") before and after the LaTex you have.完成此操作后,您只需在您拥有的 LaTex 前后放置一个双美元符号(“$$”)。 Then run the cell.然后运行单元格。

The steps are as follows: 1. Create a Markdown cell.步骤如下: 1. 创建一个 Markdown 单元格。 2. $$ some LaTex $$ 3. Press play button within Jupyter. 2. $$ 一些 LaTex $$ 3. 在 Jupyter 中按下播放按钮。

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 table)。

Just don't print(...) the object :-)只是不要print(...)对象:-)

Render/Embed html/Latex table to IR Kernel jupyter将 html/Latex 表渲染/嵌入到 IR 内核 jupyter

Some packages in R give tables in html format like "knitr", so if you want to put this tables in the notebook: R 中的一些包以 html 格式提供表格,如“knitr”,所以如果你想把这些表格放在笔记本中:

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))

Or for example if you have a file或者例如,如果您有一个文件

display_latex(file = "your file path")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM