diff --git a/src/IJulia.jl b/src/IJulia.jl index a84e9561..aacc4eaf 100644 --- a/src/IJulia.jl +++ b/src/IJulia.jl @@ -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) diff --git a/src/RCall.jl b/src/RCall.jl index c4564f70..1f22f403 100644 --- a/src/RCall.jl +++ b/src/RCall.jl @@ -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