Skip to content

Commit

Permalink
Move conditional loading of IJulia to outside include. This fixes #45
Browse files Browse the repository at this point in the history
…, due to JuliaLang/julia#8501. Should now be working on 0.4.
  • Loading branch information
simonbyrne committed Apr 14, 2015
1 parent 112f50a commit 72bcc09
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 67 deletions.
117 changes: 57 additions & 60 deletions src/IJulia.jl
Original file line number Diff line number Diff line change
@@ -1,79 +1,76 @@
# IJulia hooks for displaying plots with RCall
import IPythonDisplay: InlineDisplay

if isdefined(Main, :IJulia) && Main.IJulia.inited
import IPythonDisplay: InlineDisplay
export rplot_set

export rplot_set
const rplot_active = Bool[false]
const rplot_file = tempname()

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

const rplot_opts = Any[MIME"image/png"(),(480,400),()]
@doc """
Set options for R plotting with IJulia.
@doc """
Set options for R plotting with IJulia.
The first argument should be a MIME object: currently supported are
* `MIME("image/png")` [default]
* `MIME("image/svg+xml")`
The first argument should be a MIME object: currently supported are
* `MIME("image/png")` [default]
* `MIME("image/svg+xml")`
The remaining arguments are passed to the appropriate R graphics
device: see the relevant R help for details.
"""->
function rplot_set(m::MIME,args...;kwargs...)
rplot_opts[1] = m
rplot_opts[2] = args
rplot_opts[3] = kwargs
nothing
end
rplot_set(m::MIME"image/png") = rplot_set(m,480,400)
rplot_set(m::MIME"image/svg+xml") = rplot_set(m,6,5)
The remaining arguments are passed to the appropriate R graphics
device: see the relevant R help for details.
"""->
function rplot_set(m::MIME,args...;kwargs...)
rplot_opts[1] = m
rplot_opts[2] = args
rplot_opts[3] = kwargs
nothing
end
rplot_set(m::MIME"image/png") = rplot_set(m,480,400)
rplot_set(m::MIME"image/svg+xml") = rplot_set(m,6,5)

rplot_device(m::MIME"image/png") = :png
rplot_device(m::MIME"image/svg+xml") = :svg
rplot_device(m::MIME"image/png") = :png
rplot_device(m::MIME"image/svg+xml") = :svg


# open new png device
function new_rplot()
rcall(rplot_device(rplot_opts[1]),rplot_file,rplot_opts[2]...;rplot_opts[3]...)
rplot_active[1] = true
end
# open new png device
function new_rplot()
rcall(rplot_device(rplot_opts[1]),rplot_file,rplot_opts[2]...;rplot_opts[3]...)
rplot_active[1] = true
end

function displayfile(m::MIME"image/png", f)
open(f) do f
d = read(f,UInt8,filesize(rplot_file))
display(InlineDisplay(),m,d)
end
function displayfile(m::MIME"image/png", f)
open(f) do f
d = read(f,UInt8,filesize(rplot_file))
display(InlineDisplay(),m,d)
end
function displayfile(m::MIME"image/svg+xml", f)
open(f) do f
d = readall(f)
display(InlineDisplay(),m,d)
end
end
function displayfile(m::MIME"image/svg+xml", f)
open(f) do f
d = readall(f)
display(InlineDisplay(),m,d)
end
end

# close and display png device
function disp_rplot()
if rplot_active[1]
rcall(symbol("dev.off"))
rplot_active[1] = false
if isfile(rplot_file)
displayfile(rplot_opts[1],rplot_file)
rm(rplot_file)
end
# close and display png device
function disp_rplot()
if rplot_active[1]
rcall(symbol("dev.off"))
rplot_active[1] = false
if isfile(rplot_file)
displayfile(rplot_opts[1],rplot_file)
rm(rplot_file)
end
end
end

# cleanup png device on error
function clean_rplot()
if rplot_active[1]
rcall(symbol("dev.off"))
rplot_active[1] = false
end
isfile(rplot_file) && rm(rplot_file)
# cleanup png device on error
function clean_rplot()
if rplot_active[1]
rcall(symbol("dev.off"))
rplot_active[1] = false
end

Main.IJulia.push_preexecute_hook(new_rplot)
Main.IJulia.push_postexecute_hook(disp_rplot)
Main.IJulia.push_posterror_hook(clean_rplot)
isfile(rplot_file) && rm(rplot_file)
end

Main.IJulia.push_preexecute_hook(new_rplot)
Main.IJulia.push_postexecute_hook(disp_rplot)
Main.IJulia.push_posterror_hook(clean_rplot)
16 changes: 9 additions & 7 deletions src/RCall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ function __init__()
global const unboundValue = sexp(unsafe_load(cglobal((:R_UnboundValue,libR),Ptr{Void})))
end

include("types.jl") # define the various types of SEXPREC
include("sexp.jl")
include("iface.jl")
include("show.jl")
include("functions.jl")
include("library.jl")
include("IJulia.jl")
include("types.jl") # define the various types of SEXPREC
include("sexp.jl")
include("iface.jl")
include("show.jl")
include("functions.jl")
include("library.jl")

# only if using IJulia
isdefined(Main, :IJulia) && Main.IJulia.inited && include("IJulia.jl")

end # module

0 comments on commit 72bcc09

Please sign in to comment.