Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating objects with IRKernel

Similar to how you can write _repr_html_ for a repr on an object in Python, I'd like to be able to decorate objects in R for inspection with the IRKernel (while not interfering with regular R usage of the library). How should I be writing R to make it nicer for Jupyter users?

like image 222
Kyle Kelley Avatar asked Jul 20 '26 10:07

Kyle Kelley


1 Answers

The main kind of classes in R are S3 classes, and these are what IRKernel's repr library knows about.

Methods on S3 classes are more like generic functions. repr declares a number of generic functions like repr_html. If you define a class called frob, you can provide a function called repr_html.frob which returns the HTML to represent your object. All the magic for method lookup is based on the name, as far as I know, so you can define the repr_html.frob function as normal - it doesn't matter what package it's in, so long as the user loads the package before the kernel tries to display your object.

The generic functions repr declares are:

  • repr_text
  • repr_html
  • repr_markdown
  • repr_latex
  • repr_json
  • repr_javascript
  • repr_pdf
  • repr_png
  • repr_jpg
  • repr_svg

As in Python, you can define as many or as few of these as makes sense. See the repr code for some examples.

like image 107
Thomas K Avatar answered Jul 22 '26 01:07

Thomas K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!