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

后端 未结 5 530
感情败类
感情败类 2020-12-11 04:26

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:

相关标签:
5条回答
  • 2020-12-11 04:43

    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.

    0 讨论(0)
  • 2020-12-11 04:44

    Render/Embed html/Latex table to IR Kernel jupyter

    Some packages in R give tables in html format like "knitr", so if you want to put this tables in the notebook:

    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")
    
    0 讨论(0)
  • 2020-12-11 04:47

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

    0 讨论(0)
  • 2020-12-11 04:54

    Defining the following function in the session will display objects returned by xtable as html generated by xtable:

    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.

    Just don't print(...) the object :-)

    0 讨论(0)
  • 2020-12-11 04:56

    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)
    
    0 讨论(0)
提交回复
热议问题