{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "library(IRdisplay)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IRkernel already displays things automatically, but the [`IRdisplay` package](http://irkernel.github.io/docs/IRdisplay/0.4.4/) allows to programmatically access this machinery.\n", "\n", "You can display all standard formats of an object via [`display`](http://irkernel.github.io/docs/IRdisplay/0.4.4/display.html). This uses [`repr_*`](http://irkernel.github.io/docs/repr/0.8/repr-generics.html) and the [`jupyter.display_mimetypes` option](http://irkernel.github.io/docs/IRdisplay/0.4.4/IRdisplay-options.html):" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/html": [ "$jupyter.display_mimetypes = \n", "
  1. 'text/plain'
  2. 'text/html'
  3. 'text/markdown'
  4. 'text/latex'
  5. 'application/json'
  6. 'application/javascript'
  7. 'application/geo+json'
  8. 'application/vdom.v1+json'
  9. 'application/vnd.plotly.v1+json'
  10. 'application/vnd.vegalite.v2+json'
  11. 'application/vnd.vega.v4+json'
  12. 'application/pdf'
  13. 'image/png'
  14. 'image/jpeg'
  15. 'image/svg+xml'
\n" ], "text/latex": [ "\\textbf{\\$jupyter.display\\_mimetypes} = \\begin{enumerate*}\n", "\\item 'text/plain'\n", "\\item 'text/html'\n", "\\item 'text/markdown'\n", "\\item 'text/latex'\n", "\\item 'application/json'\n", "\\item 'application/javascript'\n", "\\item 'application/geo+json'\n", "\\item 'application/vdom.v1+json'\n", "\\item 'application/vnd.plotly.v1+json'\n", "\\item 'application/vnd.vegalite.v2+json'\n", "\\item 'application/vnd.vega.v4+json'\n", "\\item 'application/pdf'\n", "\\item 'image/png'\n", "\\item 'image/jpeg'\n", "\\item 'image/svg+xml'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "**$jupyter.display_mimetypes** = 1. 'text/plain'\n", "2. 'text/html'\n", "3. 'text/markdown'\n", "4. 'text/latex'\n", "5. 'application/json'\n", "6. 'application/javascript'\n", "7. 'application/geo+json'\n", "8. 'application/vdom.v1+json'\n", "9. 'application/vnd.plotly.v1+json'\n", "10. 'application/vnd.vegalite.v2+json'\n", "11. 'application/vnd.vega.v4+json'\n", "12. 'application/pdf'\n", "13. 'image/png'\n", "14. 'image/jpeg'\n", "15. 'image/svg+xml'\n", "\n", "\n" ], "text/plain": [ "$jupyter.display_mimetypes\n", " [1] \"text/plain\" \"text/html\" \n", " [3] \"text/markdown\" \"text/latex\" \n", " [5] \"application/json\" \"application/javascript\" \n", " [7] \"application/geo+json\" \"application/vdom.v1+json\" \n", " [9] \"application/vnd.plotly.v1+json\" \"application/vnd.vegalite.v2+json\"\n", "[11] \"application/vnd.vega.v4+json\" \"application/pdf\" \n", "[13] \"image/png\" \"image/jpeg\" \n", "[15] \"image/svg+xml\" \n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "options('jupyter.display_mimetypes')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false }, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "
display {IRdisplay}R Documentation
\n", "\n", "

Create and use multiple available reprs

\n", "\n", "

Description

\n", "\n", "

Both functions create a mimebundle for multiple reprs.\n", "display proceeds to publish it using publish_mimebundle.\n", "prepare_mimebundle returns it (see Value for details)\n", "

\n", "\n", "\n", "

Usage

\n", "\n", "
\n",
       "display(obj, ..., mimetypes = getOption(\"jupyter.display_mimetypes\"),\n",
       "  error_handler = stop)\n",
       "\n",
       "prepare_mimebundle(obj,\n",
       "  mimetypes = getOption(\"jupyter.display_mimetypes\"), metadata = NULL,\n",
       "  error_handler = stop)\n",
       "
\n", "\n", "\n", "

Arguments

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
obj\n", "

The object to create representations for

\n", "
mimetypes\n", "

Mimetypes to create reprs for. The defaults are defined by the option jupyter.display_mimetypes. (see: IRdisplay-options)

\n", "
error_handler\n", "

Function used when errors in individual reprs occur

\n", "
metadata, ...\n", "

Metadata to attach to the result (can be expanded by additional metadata)

\n", "
\n", "\n", "\n", "

Value

\n", "\n", "

prepare_mimebundle returns a list with items corresponding to the parameters of publish_mimebundle (data and metadata)\n", "

\n", "\n", "\n", "

See Also

\n", "\n", "

publish_mimebundle\n", "

\n", "\n", "\n", "

Examples

\n", "\n", "
\n",
       "bundle <- prepare_mimebundle(diag(3))\n",
       "\n",
       "## Not run: ## (Run inside of an IRkernel)\n",
       "display(help(display))\n",
       "## End(Not run)\n",
       "\n",
       "
\n", "\n", "
[Package IRdisplay version 0.7.0 ]
" ], "text/latex": [ "\\inputencoding{utf8}\n", "\\HeaderA{display}{Create and use multiple available reprs}{display}\n", "\\aliasA{prepare\\_mimebundle}{display}{prepare.Rul.mimebundle}\n", "%\n", "\\begin{Description}\\relax\n", "Both functions create a mimebundle for multiple reprs.\n", "\\code{display} proceeds to publish it using \\code{\\LinkA{publish\\_mimebundle}{publish.Rul.mimebundle}}.\n", "\\code{prepare\\_mimebundle} returns it (see \\emph{Value} for details)\n", "\\end{Description}\n", "%\n", "\\begin{Usage}\n", "\\begin{verbatim}\n", "display(obj, ..., mimetypes = getOption(\"jupyter.display_mimetypes\"),\n", " error_handler = stop)\n", "\n", "prepare_mimebundle(obj,\n", " mimetypes = getOption(\"jupyter.display_mimetypes\"), metadata = NULL,\n", " error_handler = stop)\n", "\\end{verbatim}\n", "\\end{Usage}\n", "%\n", "\\begin{Arguments}\n", "\\begin{ldescription}\n", "\\item[\\code{obj}] The object to create representations for\n", "\n", "\\item[\\code{mimetypes}] Mimetypes to create reprs for. The defaults are defined by the option \\code{jupyter.display\\_mimetypes}. (see: \\LinkA{IRdisplay-options}{IRdisplay.Rdash.options})\n", "\n", "\\item[\\code{error\\_handler}] Function used when errors in individual reprs occur\n", "\n", "\\item[\\code{metadata, ...}] Metadata to attach to the result (can be expanded by additional metadata)\n", "\\end{ldescription}\n", "\\end{Arguments}\n", "%\n", "\\begin{Value}\n", "\\code{prepare\\_mimebundle} returns a list with items corresponding to the parameters of \\code{\\LinkA{publish\\_mimebundle}{publish.Rul.mimebundle}} (\\code{data} and \\code{metadata})\n", "\\end{Value}\n", "%\n", "\\begin{SeeAlso}\\relax\n", "\\code{\\LinkA{publish\\_mimebundle}{publish.Rul.mimebundle}}\n", "\\end{SeeAlso}\n", "%\n", "\\begin{Examples}\n", "\\begin{ExampleCode}\n", "bundle <- prepare_mimebundle(diag(3))\n", "\n", "## Not run: ## (Run inside of an IRkernel)\n", "display(help(display))\n", "## End(Not run)\n", "\n", "\\end{ExampleCode}\n", "\\end{Examples}" ], "text/plain": [ "display package:IRdisplay R Documentation\n", "\n", "_\bC_\br_\be_\ba_\bt_\be _\ba_\bn_\bd _\bu_\bs_\be _\bm_\bu_\bl_\bt_\bi_\bp_\bl_\be _\ba_\bv_\ba_\bi_\bl_\ba_\bb_\bl_\be _\br_\be_\bp_\br_\bs\n", "\n", "_\bD_\be_\bs_\bc_\br_\bi_\bp_\bt_\bi_\bo_\bn:\n", "\n", " Both functions create a mimebundle for multiple reprs. ‘display’\n", " proceeds to publish it using ‘publish_mimebundle’.\n", " ‘prepare_mimebundle’ returns it (see _Value_ for details)\n", "\n", "_\bU_\bs_\ba_\bg_\be:\n", "\n", " display(obj, ..., mimetypes = getOption(\"jupyter.display_mimetypes\"),\n", " error_handler = stop)\n", " \n", " prepare_mimebundle(obj,\n", " mimetypes = getOption(\"jupyter.display_mimetypes\"), metadata = NULL,\n", " error_handler = stop)\n", " \n", "_\bA_\br_\bg_\bu_\bm_\be_\bn_\bt_\bs:\n", "\n", " obj: The object to create representations for\n", "\n", "mimetypes: Mimetypes to create reprs for. The defaults are defined by\n", " the option ‘jupyter.display_mimetypes’. (see:\n", " IRdisplay-options)\n", "\n", "error_handler: Function used when errors in individual reprs occur\n", "\n", "metadata, ...: Metadata to attach to the result (can be expanded by\n", " additional metadata)\n", "\n", "_\bV_\ba_\bl_\bu_\be:\n", "\n", " ‘prepare_mimebundle’ returns a list with items corresponding to\n", " the parameters of ‘publish_mimebundle’ (‘data’ and ‘metadata’)\n", "\n", "_\bS_\be_\be _\bA_\bl_\bs_\bo:\n", "\n", " ‘publish_mimebundle’\n", "\n", "_\bE_\bx_\ba_\bm_\bp_\bl_\be_\bs:\n", "\n", " bundle <- prepare_mimebundle(diag(3))\n", " \n", " ## Not run:\n", " ## (Run inside of an IRkernel)\n", " display(help(display))\n", " ## End(Not run)\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display(help(display)) #without display(), help appears in a pager" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To display specific formats, you can use the [`display_`](http://irkernel.github.io/docs/IRdisplay/0.4.4/display_-less-than-image-greater-than.html) and [`display_text`](http://irkernel.github.io/docs/IRdisplay/0.4.4/display_-less-than-text-greater-than.html) functions (their only difference is that images support convenience `width` and `height` arguments):" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/html": [ "

HTML output

" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "display_html('

HTML output

')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plots are also displayed automatically by the IRkernel, but if you want to display e. g. a PNG from another source, this is how to do it:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "image/png": "R0lGODdhAQABAIAAAPxqbAAAACwAAAAAAQABAAACAkQBADsK" }, "metadata": { "height": 64, "width": 64 }, "output_type": "display_data" } ], "source": [ "raw_png_data <- base64enc::base64decode('R0lGODdhAQABAIAAAPxqbAAAACwAAAAAAQABAAACAkQBADsK')\n", "display_png(raw_png_data, width = 64, height = 64)\n", "#display_png(file = 'a-file.png')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Displaying multiple formats (implicitly or explicitly) allows to choose the frontend to choose the best representation. E.g. when converting to PDF, you’ll want to make sure that `jupyter.plot_mimetypes` contains `image/svg+xml` and/or `application/pdf`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "text/html": [ "$jupyter.plot_mimetypes = \n", "
  1. 'text/plain'
  2. 'image/png'
\n" ], "text/latex": [ "\\textbf{\\$jupyter.plot\\_mimetypes} = \\begin{enumerate*}\n", "\\item 'text/plain'\n", "\\item 'image/png'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "**$jupyter.plot_mimetypes** = 1. 'text/plain'\n", "2. 'image/png'\n", "\n", "\n" ], "text/plain": [ "$jupyter.plot_mimetypes\n", "[1] \"text/plain\" \"image/png\" \n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "options('jupyter.plot_mimetypes')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "A circle" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "publish_mimebundle(list(\n", " 'image/svg+xml' = '',\n", " 'text/plain' = 'A circle'))" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.0.3" } }, "nbformat": 4, "nbformat_minor": 4 }