Skip to content

Commit 72bcc09

Browse files
committed
Move conditional loading of IJulia to outside include. This fixes #45, due to JuliaLang/julia#8501. Should now be working on 0.4.
1 parent 112f50a commit 72bcc09

File tree

2 files changed

+66
-67
lines changed

2 files changed

+66
-67
lines changed

src/IJulia.jl

+57-60
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,76 @@
11
# IJulia hooks for displaying plots with RCall
2+
import IPythonDisplay: InlineDisplay
23

3-
if isdefined(Main, :IJulia) && Main.IJulia.inited
4-
import IPythonDisplay: InlineDisplay
4+
export rplot_set
55

6-
export rplot_set
6+
const rplot_active = Bool[false]
7+
const rplot_file = tempname()
78

8-
const rplot_active = Bool[false]
9-
const rplot_file = tempname()
9+
const rplot_opts = Any[MIME"image/png"(),(480,400),()]
1010

11-
const rplot_opts = Any[MIME"image/png"(),(480,400),()]
11+
@doc """
12+
Set options for R plotting with IJulia.
1213
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")`
1517
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)
3129

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
3432

3533

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
4139

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)
4744
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)
5350
end
51+
end
5452

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)
6461
end
6562
end
63+
end
6664

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
7470
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)
7972
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)

src/RCall.jl

+9-7
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ function __init__()
6666
global const unboundValue = sexp(unsafe_load(cglobal((:R_UnboundValue,libR),Ptr{Void})))
6767
end
6868

69-
include("types.jl") # define the various types of SEXPREC
70-
include("sexp.jl")
71-
include("iface.jl")
72-
include("show.jl")
73-
include("functions.jl")
74-
include("library.jl")
75-
include("IJulia.jl")
69+
include("types.jl") # define the various types of SEXPREC
70+
include("sexp.jl")
71+
include("iface.jl")
72+
include("show.jl")
73+
include("functions.jl")
74+
include("library.jl")
75+
76+
# only if using IJulia
77+
isdefined(Main, :IJulia) && Main.IJulia.inited && include("IJulia.jl")
7678

7779
end # module

0 commit comments

Comments
 (0)