I am using Gadfly in Pluto and I am trying to figure out if it is possible to have interactive versions of the plots in Pluto notebooks. If I just use the REPL, Gadfly produces very nice interactive plots that are opened in my web browser:
using Gadfly
plot([sin, cos], 0 , 4)
However, if I use Gadfly in Pluto, the plots that are included in the notebook are not interactive, they are static. This is a simple example of a Pluto notebook:
### A Pluto.jl notebook ###
# v0.12.20
using Markdown
using InteractiveUtils
# ╔═╡ 7bb74118-73d1-11eb-2bd5-c1ef89972288
using Gadfly
# ╔═╡ 9080ac2e-73d1-11eb-18d6-5f85903a7259
plot([sin, cos], 0 , 4)
# ╔═╡ Cell order:
# ╠═7bb74118-73d1-11eb-2bd5-c1ef89972288
# ╠═9080ac2e-73d1-11eb-18d6-5f85903a7259
How can I have interactive versions of the Gadfly plots in Pluto?
Any help is much appreciated!
Following the suggestion in https://github.com/fonsp/Pluto.jl/issues/546#issuecomment-705556778
You can define:
struct HTMLDocument
embedded
end
function Base.show(io::IO, mime::MIME"text/html", doc::HTMLDocument)
println(io, "<html>")
show(io, mime, doc.embedded)
println(io, "</html>")
end
And then do
plot([sin, cos], 0 , 4) |> HTMLDocument
To get the interactive plot in Pluto.
Best,
Related
Let's assume I have 2 source files, the first one named example1.r and the second one example2.r (given below).
example1.r
plot(1:10,1:10)
example2.r
qplot(1:10,1:10)
When I source example1.r, the graph is drawn. It does not, however, when I source example2.r. What is the solution here?
(qplot in example2.r is ggplot2's function)
Update:
.R files: source's option print.eval=TRUE will lead to printing behaviour of the evaluation result like in the interactive command line.
source("Script.R", print.eval=TRUE)
.Rnw files: knitr by default emulates the behaviour of the interactive command line wrt. printing. Note that knitr can be specified as Sweaving engine also for R package vignettes.
This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).
This is the famous FAQ 7.22: Why do lattice/trellis graphics not work?.
For grid graphics like ggplot2 or lattice, you need to print the graphics object in order to actually draw it.
Interactively on the command line this is done automatically. Everywhere else (inside files to be sourced, loops, functions, Sweave chunks) you need to print it explicitly.
print (qplot (1 : 10, 1 : 10))
Alternatively, you can redefine qplot to do the printing:
qplot <- function (x, y = NULL, z = NULL, ...) {
p <- ggplot2::qplot (x = x, y = y, z = z, ...)
print (p)
}
(this changes the axis labels to x and y).
I use this approach in vignettes where I want to write code exactly as a user in an interactive session would type it.
When running julia REPL from the command line and copy-pasting my script, the Gadfly plot command works as expected.
But, when running julia -i scriptname.jl plot no longer works and prints:
julia> DataFrame(CSV.File("file.csv"))
julia> plot(massdf, x=:date, y=:mass, group=:day, color=:day, Geom.line)
Plot(...)
julia>
Is there a setting needed to make it work?
You need to explicitly display the output of plot() in your script (which is done implicitly in a Read-Eval-Print-Loop but not when the whole script is executed, even with -i).
Use display(p::Plot), or draw(backend::Compose.Backend, p::Plot) :
df = DataFrame(CSV.File("file.csv"))
p = plot(df, x=:date, y=:mass, group=:day, color=:day, Geom.line)
display(p)
See also Gadfly Backends.
using Gadfly
xvalues = rand(1000);
yvalues = rand(1000);
Gadfly.plot(x=xvalues,y=xvalues,Geom.point,Geom.line)
I am trying make a plot using julia Gadfly package in the atom editor . it does display any plot in the plot panel
This should have been fixed by GiovineItalia/Gadfly.jl/pull/1373 (in Gadfly v1.2). What version of Gadfly do you have?:
julia> ]st Gadfly
If you have an older version, try doing popdisplay(), and redoing the plot.
Let's assume I have 2 source files, the first one named example1.r and the second one example2.r (given below).
example1.r
plot(1:10,1:10)
example2.r
qplot(1:10,1:10)
When I source example1.r, the graph is drawn. It does not, however, when I source example2.r. What is the solution here?
(qplot in example2.r is ggplot2's function)
Update:
.R files: source's option print.eval=TRUE will lead to printing behaviour of the evaluation result like in the interactive command line.
source("Script.R", print.eval=TRUE)
.Rnw files: knitr by default emulates the behaviour of the interactive command line wrt. printing. Note that knitr can be specified as Sweaving engine also for R package vignettes.
This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).
This is the famous FAQ 7.22: Why do lattice/trellis graphics not work?.
For grid graphics like ggplot2 or lattice, you need to print the graphics object in order to actually draw it.
Interactively on the command line this is done automatically. Everywhere else (inside files to be sourced, loops, functions, Sweave chunks) you need to print it explicitly.
print (qplot (1 : 10, 1 : 10))
Alternatively, you can redefine qplot to do the printing:
qplot <- function (x, y = NULL, z = NULL, ...) {
p <- ggplot2::qplot (x = x, y = y, z = z, ...)
print (p)
}
(this changes the axis labels to x and y).
I use this approach in vignettes where I want to write code exactly as a user in an interactive session would type it.
Let's assume I have 2 source files, the first one named example1.r and the second one example2.r (given below).
example1.r
plot(1:10,1:10)
example2.r
qplot(1:10,1:10)
When I source example1.r, the graph is drawn. It does not, however, when I source example2.r. What is the solution here?
(qplot in example2.r is ggplot2's function)
Update:
.R files: source's option print.eval=TRUE will lead to printing behaviour of the evaluation result like in the interactive command line.
source("Script.R", print.eval=TRUE)
.Rnw files: knitr by default emulates the behaviour of the interactive command line wrt. printing. Note that knitr can be specified as Sweaving engine also for R package vignettes.
This is my original answer. But note that this workaround is IMHO completely obsolete now (and it always was good for a small lazy niche only).
This is the famous FAQ 7.22: Why do lattice/trellis graphics not work?.
For grid graphics like ggplot2 or lattice, you need to print the graphics object in order to actually draw it.
Interactively on the command line this is done automatically. Everywhere else (inside files to be sourced, loops, functions, Sweave chunks) you need to print it explicitly.
print (qplot (1 : 10, 1 : 10))
Alternatively, you can redefine qplot to do the printing:
qplot <- function (x, y = NULL, z = NULL, ...) {
p <- ggplot2::qplot (x = x, y = y, z = z, ...)
print (p)
}
(this changes the axis labels to x and y).
I use this approach in vignettes where I want to write code exactly as a user in an interactive session would type it.