-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…, due to JuliaLang/julia#8501. Should now be working on 0.4.
- Loading branch information
1 parent
112f50a
commit 72bcc09
Showing
2 changed files
with
66 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters