Debian Codegraph @

example-notebooks/Display.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": false,
8 "jupyter": {
9 "outputs_hidden": false
10 }
11 },
12 "outputs": [],
13 "source": [
14 "library(IRdisplay)"
15 ]
16 },
17 {
18 "cell_type": "markdown",
19 "metadata": {},
20 "source": [
21 "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",
22 "\n",
23 "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):"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": 2,
29 "metadata": {
30 "collapsed": false,
31 "jupyter": {
32 "outputs_hidden": false
33 }
34 },
35 "outputs": [
36 {
37 "data": {
38 "text/html": [
39 "<strong>$jupyter.display_mimetypes</strong> = <style>\n",
40 ".list-inline {list-style: none; margin:0; padding: 0}\n",
41 ".list-inline>li {display: inline-block}\n",
42 ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
43 "</style>\n",
44 "<ol class=list-inline><li>'text/plain'</li><li>'text/html'</li><li>'text/markdown'</li><li>'text/latex'</li><li>'application/json'</li><li>'application/javascript'</li><li>'application/geo+json'</li><li>'application/vdom.v1+json'</li><li>'application/vnd.plotly.v1+json'</li><li>'application/vnd.vegalite.v2+json'</li><li>'application/vnd.vega.v4+json'</li><li>'application/pdf'</li><li>'image/png'</li><li>'image/jpeg'</li><li>'image/svg+xml'</li></ol>\n"
45 ],
46 "text/latex": [
47 "\\textbf{\\$jupyter.display\\_mimetypes} = \\begin{enumerate*}\n",
48 "\\item 'text/plain'\n",
49 "\\item 'text/html'\n",
50 "\\item 'text/markdown'\n",
51 "\\item 'text/latex'\n",
52 "\\item 'application/json'\n",
53 "\\item 'application/javascript'\n",
54 "\\item 'application/geo+json'\n",
55 "\\item 'application/vdom.v1+json'\n",
56 "\\item 'application/vnd.plotly.v1+json'\n",
57 "\\item 'application/vnd.vegalite.v2+json'\n",
58 "\\item 'application/vnd.vega.v4+json'\n",
59 "\\item 'application/pdf'\n",
60 "\\item 'image/png'\n",
61 "\\item 'image/jpeg'\n",
62 "\\item 'image/svg+xml'\n",
63 "\\end{enumerate*}\n"
64 ],
65 "text/markdown": [
66 "**$jupyter.display_mimetypes** = 1. 'text/plain'\n",
67 "2. 'text/html'\n",
68 "3. 'text/markdown'\n",
69 "4. 'text/latex'\n",
70 "5. 'application/json'\n",
71 "6. 'application/javascript'\n",
72 "7. 'application/geo+json'\n",
73 "8. 'application/vdom.v1+json'\n",
74 "9. 'application/vnd.plotly.v1+json'\n",
75 "10. 'application/vnd.vegalite.v2+json'\n",
76 "11. 'application/vnd.vega.v4+json'\n",
77 "12. 'application/pdf'\n",
78 "13. 'image/png'\n",
79 "14. 'image/jpeg'\n",
80 "15. 'image/svg+xml'\n",
81 "\n",
82 "\n"
83 ],
84 "text/plain": [
85 "$jupyter.display_mimetypes\n",
86 " [1] \"text/plain\" \"text/html\" \n",
87 " [3] \"text/markdown\" \"text/latex\" \n",
88 " [5] \"application/json\" \"application/javascript\" \n",
89 " [7] \"application/geo+json\" \"application/vdom.v1+json\" \n",
90 " [9] \"application/vnd.plotly.v1+json\" \"application/vnd.vegalite.v2+json\"\n",
91 "[11] \"application/vnd.vega.v4+json\" \"application/pdf\" \n",
92 "[13] \"image/png\" \"image/jpeg\" \n",
93 "[15] \"image/svg+xml\" \n"
94 ]
95 },
96 "metadata": {},
97 "output_type": "display_data"
98 }
99 ],
100 "source": [
101 "options('jupyter.display_mimetypes')"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": 3,
107 "metadata": {
108 "collapsed": false,
109 "jupyter": {
110 "outputs_hidden": false
111 },
112 "scrolled": true
113 },
114 "outputs": [
115 {
116 "data": {
117 "text/html": [
118 "\n",
119 "<table width=\"100%\" summary=\"page for display {IRdisplay}\"><tr><td>display {IRdisplay}</td><td style=\"text-align: right;\">R Documentation</td></tr></table>\n",
120 "\n",
121 "<h2>Create and use multiple available reprs</h2>\n",
122 "\n",
123 "<h3>Description</h3>\n",
124 "\n",
125 "<p>Both functions create a mimebundle for multiple reprs.\n",
126 "<code>display</code> proceeds to publish it using <code>publish_mimebundle</code>.\n",
127 "<code>prepare_mimebundle</code> returns it (see <em>Value</em> for details)\n",
128 "</p>\n",
129 "\n",
130 "\n",
131 "<h3>Usage</h3>\n",
132 "\n",
133 "<pre>\n",
134 "display(obj, ..., mimetypes = getOption(\"jupyter.display_mimetypes\"),\n",
135 " error_handler = stop)\n",
136 "\n",
137 "prepare_mimebundle(obj,\n",
138 " mimetypes = getOption(\"jupyter.display_mimetypes\"), metadata = NULL,\n",
139 " error_handler = stop)\n",
140 "</pre>\n",
141 "\n",
142 "\n",
143 "<h3>Arguments</h3>\n",
144 "\n",
145 "<table summary=\"R argblock\">\n",
146 "<tr valign=\"top\"><td><code>obj</code></td>\n",
147 "<td>\n",
148 "<p>The object to create representations for</p>\n",
149 "</td></tr>\n",
150 "<tr valign=\"top\"><td><code>mimetypes</code></td>\n",
151 "<td>\n",
152 "<p>Mimetypes to create reprs for. The defaults are defined by the option <code>jupyter.display_mimetypes</code>. (see: IRdisplay-options)</p>\n",
153 "</td></tr>\n",
154 "<tr valign=\"top\"><td><code>error_handler</code></td>\n",
155 "<td>\n",
156 "<p>Function used when errors in individual reprs occur</p>\n",
157 "</td></tr>\n",
158 "<tr valign=\"top\"><td><code>metadata, ...</code></td>\n",
159 "<td>\n",
160 "<p>Metadata to attach to the result (can be expanded by additional metadata)</p>\n",
161 "</td></tr>\n",
162 "</table>\n",
163 "\n",
164 "\n",
165 "<h3>Value</h3>\n",
166 "\n",
167 "<p><code>prepare_mimebundle</code> returns a list with items corresponding to the parameters of <code>publish_mimebundle</code> (<code>data</code> and <code>metadata</code>)\n",
168 "</p>\n",
169 "\n",
170 "\n",
171 "<h3>See Also</h3>\n",
172 "\n",
173 "<p><code>publish_mimebundle</code>\n",
174 "</p>\n",
175 "\n",
176 "\n",
177 "<h3>Examples</h3>\n",
178 "\n",
179 "<pre>\n",
180 "bundle &lt;- prepare_mimebundle(diag(3))\n",
181 "\n",
182 "## Not run: ## (Run inside of an IRkernel)\n",
183 "display(help(display))\n",
184 "## End(Not run)\n",
185 "\n",
186 "</pre>\n",
187 "\n",
188 "<hr /><div style=\"text-align: center;\">[Package <em>IRdisplay</em> version 0.7.0 ]</div>"
189 ],
190 "text/latex": [
191 "\\inputencoding{utf8}\n",
192 "\\HeaderA{display}{Create and use multiple available reprs}{display}\n",
193 "\\aliasA{prepare\\_mimebundle}{display}{prepare.Rul.mimebundle}\n",
194 "%\n",
195 "\\begin{Description}\\relax\n",
196 "Both functions create a mimebundle for multiple reprs.\n",
197 "\\code{display} proceeds to publish it using \\code{\\LinkA{publish\\_mimebundle}{publish.Rul.mimebundle}}.\n",
198 "\\code{prepare\\_mimebundle} returns it (see \\emph{Value} for details)\n",
199 "\\end{Description}\n",
200 "%\n",
201 "\\begin{Usage}\n",
202 "\\begin{verbatim}\n",
203 "display(obj, ..., mimetypes = getOption(\"jupyter.display_mimetypes\"),\n",
204 " error_handler = stop)\n",
205 "\n",
206 "prepare_mimebundle(obj,\n",
207 " mimetypes = getOption(\"jupyter.display_mimetypes\"), metadata = NULL,\n",
208 " error_handler = stop)\n",
209 "\\end{verbatim}\n",
210 "\\end{Usage}\n",
211 "%\n",
212 "\\begin{Arguments}\n",
213 "\\begin{ldescription}\n",
214 "\\item[\\code{obj}] The object to create representations for\n",
215 "\n",
216 "\\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",
217 "\n",
218 "\\item[\\code{error\\_handler}] Function used when errors in individual reprs occur\n",
219 "\n",
220 "\\item[\\code{metadata, ...}] Metadata to attach to the result (can be expanded by additional metadata)\n",
221 "\\end{ldescription}\n",
222 "\\end{Arguments}\n",
223 "%\n",
224 "\\begin{Value}\n",
225 "\\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",
226 "\\end{Value}\n",
227 "%\n",
228 "\\begin{SeeAlso}\\relax\n",
229 "\\code{\\LinkA{publish\\_mimebundle}{publish.Rul.mimebundle}}\n",
230 "\\end{SeeAlso}\n",
231 "%\n",
232 "\\begin{Examples}\n",
233 "\\begin{ExampleCode}\n",
234 "bundle <- prepare_mimebundle(diag(3))\n",
235 "\n",
236 "## Not run: ## (Run inside of an IRkernel)\n",
237 "display(help(display))\n",
238 "## End(Not run)\n",
239 "\n",
240 "\\end{ExampleCode}\n",
241 "\\end{Examples}"
242 ],
243 "text/plain": [
244 "display package:IRdisplay R Documentation\n",
245 "\n",
246 "_\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",
247 "\n",
248 "_\bD_\be_\bs_\bc_\br_\bi_\bp_\bt_\bi_\bo_\bn:\n",
249 "\n",
250 " Both functions create a mimebundle for multiple reprs. ‘display’\n",
251 " proceeds to publish it using ‘publish_mimebundle’.\n",
252 " ‘prepare_mimebundle’ returns it (see _Value_ for details)\n",
253 "\n",
254 "_\bU_\bs_\ba_\bg_\be:\n",
255 "\n",
256 " display(obj, ..., mimetypes = getOption(\"jupyter.display_mimetypes\"),\n",
257 " error_handler = stop)\n",
258 " \n",
259 " prepare_mimebundle(obj,\n",
260 " mimetypes = getOption(\"jupyter.display_mimetypes\"), metadata = NULL,\n",
261 " error_handler = stop)\n",
262 " \n",
263 "_\bA_\br_\bg_\bu_\bm_\be_\bn_\bt_\bs:\n",
264 "\n",
265 " obj: The object to create representations for\n",
266 "\n",
267 "mimetypes: Mimetypes to create reprs for. The defaults are defined by\n",
268 " the option ‘jupyter.display_mimetypes’. (see:\n",
269 " IRdisplay-options)\n",
270 "\n",
271 "error_handler: Function used when errors in individual reprs occur\n",
272 "\n",
273 "metadata, ...: Metadata to attach to the result (can be expanded by\n",
274 " additional metadata)\n",
275 "\n",
276 "_\bV_\ba_\bl_\bu_\be:\n",
277 "\n",
278 " ‘prepare_mimebundle’ returns a list with items corresponding to\n",
279 " the parameters of ‘publish_mimebundle’ (‘data’ and ‘metadata’)\n",
280 "\n",
281 "_\bS_\be_\be _\bA_\bl_\bs_\bo:\n",
282 "\n",
283 " ‘publish_mimebundle’\n",
284 "\n",
285 "_\bE_\bx_\ba_\bm_\bp_\bl_\be_\bs:\n",
286 "\n",
287 " bundle <- prepare_mimebundle(diag(3))\n",
288 " \n",
289 " ## Not run:\n",
290 " ## (Run inside of an IRkernel)\n",
291 " display(help(display))\n",
292 " ## End(Not run)\n",
293 " "
294 ]
295 },
296 "metadata": {},
297 "output_type": "display_data"
298 }
299 ],
300 "source": [
301 "display(help(display)) #without display(), help appears in a pager"
302 ]
303 },
304 {
305 "cell_type": "markdown",
306 "metadata": {},
307 "source": [
308 "To display specific formats, you can use the [`display_<image>`](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):"
309 ]
310 },
311 {
312 "cell_type": "code",
313 "execution_count": 4,
314 "metadata": {
315 "collapsed": false,
316 "jupyter": {
317 "outputs_hidden": false
318 }
319 },
320 "outputs": [
321 {
322 "data": {
323 "text/html": [
324 "<h1 style=\"background-color:#fe57a1\">HTML output</h1>"
325 ]
326 },
327 "metadata": {},
328 "output_type": "display_data"
329 }
330 ],
331 "source": [
332 "display_html('<h1 style=\"background-color:#fe57a1\">HTML output</h1>')"
333 ]
334 },
335 {
336 "cell_type": "markdown",
337 "metadata": {},
338 "source": [
339 "Plots are also displayed automatically by the IRkernel, but if you want to display e.&hairsp;g. a PNG from another source, this is how to do it:"
340 ]
341 },
342 {
343 "cell_type": "code",
344 "execution_count": 5,
345 "metadata": {
346 "collapsed": false,
347 "jupyter": {
348 "outputs_hidden": false
349 }
350 },
351 "outputs": [
352 {
353 "data": {
354 "image/png": "R0lGODdhAQABAIAAAPxqbAAAACwAAAAAAQABAAACAkQBADsK"
355 },
356 "metadata": {
357 "height": 64,
358 "width": 64
359 },
360 "output_type": "display_data"
361 }
362 ],
363 "source": [
364 "raw_png_data <- base64enc::base64decode('R0lGODdhAQABAIAAAPxqbAAAACwAAAAAAQABAAACAkQBADsK')\n",
365 "display_png(raw_png_data, width = 64, height = 64)\n",
366 "#display_png(file = 'a-file.png')"
367 ]
368 },
369 {
370 "cell_type": "markdown",
371 "metadata": {},
372 "source": [
373 "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`."
374 ]
375 },
376 {
377 "cell_type": "code",
378 "execution_count": 6,
379 "metadata": {
380 "collapsed": false,
381 "jupyter": {
382 "outputs_hidden": false
383 }
384 },
385 "outputs": [
386 {
387 "data": {
388 "text/html": [
389 "<strong>$jupyter.plot_mimetypes</strong> = <style>\n",
390 ".list-inline {list-style: none; margin:0; padding: 0}\n",
391 ".list-inline>li {display: inline-block}\n",
392 ".list-inline>li:not(:last-child)::after {content: \"\\00b7\"; padding: 0 .5ex}\n",
393 "</style>\n",
394 "<ol class=list-inline><li>'text/plain'</li><li>'image/png'</li></ol>\n"
395 ],
396 "text/latex": [
397 "\\textbf{\\$jupyter.plot\\_mimetypes} = \\begin{enumerate*}\n",
398 "\\item 'text/plain'\n",
399 "\\item 'image/png'\n",
400 "\\end{enumerate*}\n"
401 ],
402 "text/markdown": [
403 "**$jupyter.plot_mimetypes** = 1. 'text/plain'\n",
404 "2. 'image/png'\n",
405 "\n",
406 "\n"
407 ],
408 "text/plain": [
409 "$jupyter.plot_mimetypes\n",
410 "[1] \"text/plain\" \"image/png\" \n"
411 ]
412 },
413 "metadata": {},
414 "output_type": "display_data"
415 }
416 ],
417 "source": [
418 "options('jupyter.plot_mimetypes')"
419 ]
420 },
421 {
422 "cell_type": "code",
423 "execution_count": 7,
424 "metadata": {
425 "collapsed": false,
426 "jupyter": {
427 "outputs_hidden": false
428 }
429 },
430 "outputs": [
431 {
432 "data": {
433 "image/svg+xml": [
434 "<svg width=\"10\" height=\"10\"><circle r=\"5\" cx=\"5\" cy=\"5\"/></svg>"
435 ],
436 "text/plain": [
437 "A circle"
438 ]
439 },
440 "metadata": {},
441 "output_type": "display_data"
442 }
443 ],
444 "source": [
445 "publish_mimebundle(list(\n",
446 " 'image/svg+xml' = '<svg width=\"10\" height=\"10\"><circle r=\"5\" cx=\"5\" cy=\"5\"/></svg>',\n",
447 " 'text/plain' = 'A circle'))"
448 ]
449 }
450 ],
451 "metadata": {
452 "kernelspec": {
453 "display_name": "R",
454 "language": "R",
455 "name": "ir"
456 },
457 "language_info": {
458 "codemirror_mode": "r",
459 "file_extension": ".r",
460 "mimetype": "text/x-r-source",
461 "name": "R",
462 "pygments_lexer": "r",
463 "version": "4.0.3"
464 }
465 },
466 "nbformat": 4,
467 "nbformat_minor": 4
468 }