1

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 ?

Andrew Chisholm
  • 6,362
  • 2
  • 22
  • 41
Nathan Marotte
  • 771
  • 1
  • 5
  • 15
  • Use display from IPython instead of print - https://stackoverflow.com/a/56385466/10276092 – M.Viking May 14 '21 at 14:08
  • @M.Viking are you sure this works for R Jupyter Notebooks ? I tried `display(head(CO2))` and got `Error in display(head(CO2)): could not find function "display"`. Is it from a library ? – Nathan Marotte May 14 '21 at 14:14

1 Answers1

0

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

krassowski
  • 13,598
  • 4
  • 60
  • 92