rjupyter-notebookjupyter-irkernel

Pretty print of R Jupyter Notebook inside a function


When calling head(...) in a cell in jupyter notebook, we get a nice table with the data

enter image description here

However if I want to do the same in a function, nothing is shown, so I have to use print(head(...)) but the result is less pretty as you can see

enter image description here

Is there a way to get the same type of output as the first image but in a function ?


Solution

  • Jupyter display functions for IRkernel are provided by the IRdisplay package, which gets installed together with IRkernel (by default). Just use:

    IRdisplay::display(CO2)
    

    Or load the library into the namespace first:

    library(IRdisplay)
    data("CO2")
    a <- function() {
        display(head(CO2))
    }
    a()
    

    Which works as intended:

    enter image description here