|
1 | 1 | # IJulia hooks for displaying plots with RCall
|
| 2 | +import IPythonDisplay: InlineDisplay |
2 | 3 |
|
3 |
| -if isdefined(Main, :IJulia) && Main.IJulia.inited |
4 |
| - import IPythonDisplay: InlineDisplay |
| 4 | +export rplot_set |
5 | 5 |
|
6 |
| - export rplot_set |
| 6 | +const rplot_active = Bool[false] |
| 7 | +const rplot_file = tempname() |
7 | 8 |
|
8 |
| - const rplot_active = Bool[false] |
9 |
| - const rplot_file = tempname() |
| 9 | +const rplot_opts = Any[MIME"image/png"(),(480,400),()] |
10 | 10 |
|
11 |
| - const rplot_opts = Any[MIME"image/png"(),(480,400),()] |
| 11 | +@doc """ |
| 12 | +Set options for R plotting with IJulia. |
12 | 13 |
|
13 |
| - @doc """ |
14 |
| - Set options for R plotting with IJulia. |
| 14 | +The first argument should be a MIME object: currently supported are |
| 15 | +* `MIME("image/png")` [default] |
| 16 | +* `MIME("image/svg+xml")` |
15 | 17 |
|
16 |
| - The first argument should be a MIME object: currently supported are |
17 |
| - * `MIME("image/png")` [default] |
18 |
| - * `MIME("image/svg+xml")` |
19 |
| -
|
20 |
| - The remaining arguments are passed to the appropriate R graphics |
21 |
| - device: see the relevant R help for details. |
22 |
| - """-> |
23 |
| - function rplot_set(m::MIME,args...;kwargs...) |
24 |
| - rplot_opts[1] = m |
25 |
| - rplot_opts[2] = args |
26 |
| - rplot_opts[3] = kwargs |
27 |
| - nothing |
28 |
| - end |
29 |
| - rplot_set(m::MIME"image/png") = rplot_set(m,480,400) |
30 |
| - rplot_set(m::MIME"image/svg+xml") = rplot_set(m,6,5) |
| 18 | +The remaining arguments are passed to the appropriate R graphics |
| 19 | +device: see the relevant R help for details. |
| 20 | +"""-> |
| 21 | +function rplot_set(m::MIME,args...;kwargs...) |
| 22 | + rplot_opts[1] = m |
| 23 | + rplot_opts[2] = args |
| 24 | + rplot_opts[3] = kwargs |
| 25 | + nothing |
| 26 | +end |
| 27 | +rplot_set(m::MIME"image/png") = rplot_set(m,480,400) |
| 28 | +rplot_set(m::MIME"image/svg+xml") = rplot_set(m,6,5) |
31 | 29 |
|
32 |
| - rplot_device(m::MIME"image/png") = :png |
33 |
| - rplot_device(m::MIME"image/svg+xml") = :svg |
| 30 | +rplot_device(m::MIME"image/png") = :png |
| 31 | +rplot_device(m::MIME"image/svg+xml") = :svg |
34 | 32 |
|
35 | 33 |
|
36 |
| - # open new png device |
37 |
| - function new_rplot() |
38 |
| - rcall(rplot_device(rplot_opts[1]),rplot_file,rplot_opts[2]...;rplot_opts[3]...) |
39 |
| - rplot_active[1] = true |
40 |
| - end |
| 34 | +# open new png device |
| 35 | +function new_rplot() |
| 36 | + rcall(rplot_device(rplot_opts[1]),rplot_file,rplot_opts[2]...;rplot_opts[3]...) |
| 37 | + rplot_active[1] = true |
| 38 | +end |
41 | 39 |
|
42 |
| - function displayfile(m::MIME"image/png", f) |
43 |
| - open(f) do f |
44 |
| - d = read(f,UInt8,filesize(rplot_file)) |
45 |
| - display(InlineDisplay(),m,d) |
46 |
| - end |
| 40 | +function displayfile(m::MIME"image/png", f) |
| 41 | + open(f) do f |
| 42 | + d = read(f,UInt8,filesize(rplot_file)) |
| 43 | + display(InlineDisplay(),m,d) |
47 | 44 | end
|
48 |
| - function displayfile(m::MIME"image/svg+xml", f) |
49 |
| - open(f) do f |
50 |
| - d = readall(f) |
51 |
| - display(InlineDisplay(),m,d) |
52 |
| - end |
| 45 | +end |
| 46 | +function displayfile(m::MIME"image/svg+xml", f) |
| 47 | + open(f) do f |
| 48 | + d = readall(f) |
| 49 | + display(InlineDisplay(),m,d) |
53 | 50 | end
|
| 51 | +end |
54 | 52 |
|
55 |
| - # close and display png device |
56 |
| - function disp_rplot() |
57 |
| - if rplot_active[1] |
58 |
| - rcall(symbol("dev.off")) |
59 |
| - rplot_active[1] = false |
60 |
| - if isfile(rplot_file) |
61 |
| - displayfile(rplot_opts[1],rplot_file) |
62 |
| - rm(rplot_file) |
63 |
| - end |
| 53 | +# close and display png device |
| 54 | +function disp_rplot() |
| 55 | + if rplot_active[1] |
| 56 | + rcall(symbol("dev.off")) |
| 57 | + rplot_active[1] = false |
| 58 | + if isfile(rplot_file) |
| 59 | + displayfile(rplot_opts[1],rplot_file) |
| 60 | + rm(rplot_file) |
64 | 61 | end
|
65 | 62 | end
|
| 63 | +end |
66 | 64 |
|
67 |
| - # cleanup png device on error |
68 |
| - function clean_rplot() |
69 |
| - if rplot_active[1] |
70 |
| - rcall(symbol("dev.off")) |
71 |
| - rplot_active[1] = false |
72 |
| - end |
73 |
| - isfile(rplot_file) && rm(rplot_file) |
| 65 | +# cleanup png device on error |
| 66 | +function clean_rplot() |
| 67 | + if rplot_active[1] |
| 68 | + rcall(symbol("dev.off")) |
| 69 | + rplot_active[1] = false |
74 | 70 | end
|
75 |
| - |
76 |
| - Main.IJulia.push_preexecute_hook(new_rplot) |
77 |
| - Main.IJulia.push_postexecute_hook(disp_rplot) |
78 |
| - Main.IJulia.push_posterror_hook(clean_rplot) |
| 71 | + isfile(rplot_file) && rm(rplot_file) |
79 | 72 | end
|
| 73 | + |
| 74 | +Main.IJulia.push_preexecute_hook(new_rplot) |
| 75 | +Main.IJulia.push_postexecute_hook(disp_rplot) |
| 76 | +Main.IJulia.push_posterror_hook(clean_rplot) |
0 commit comments