I've been prototyping some julia code in a Jupyter notebook, but it's outgrown the notebook format and I want to put it in a script in order to organise it properly. However, I can't work out how to get Plots to work in a script, and can't find any documentation about it.
Here's a minimal not-working example:
using Plots
gr()
display(plot([1,3,2]))
println("here")
This code takes 20-30 seconds to import Plots, then opens a window but immediately closes it again, prints "here", and exits. I can't use ctrl-C while the import process is happening.
So, I have three questions:
How do I prevent the plot window from closing as soon as it opens? What I want is for the script to block or (ideally) enter an event loop until the window is closed, and terminate after that.
Can the extremely long import time be reduced somehow?
Does any documentation exist for using Plots outside of a Jupyter environment?
If it makes a difference, I'm using julia 1.1.1 on a Mac.
The most natural way to achieve the workflow you're looking for in the first bullet is to use Juno or VS Code. Suppose in your working directory you have the following code in a file script.jl:
using Plots
gr()
display(plot([1,3,2]))
hello = "world"
println("here")
Now fire up Juno or VS Code and launch the integrated Julia REPL. From the Julia REPL, you can use include to source your script:
julia> include("script.jl")
Plot{Plots.GRBackend() n=1}
here
Now your script has been completely run and the plot is available for viewing in the plot pane (this is true for both Juno and VS Code). Now you can continue to include other files in the REPL or run other code interactively. Note that variables defined in script.jl are available for use:
julia> hello
"world"
EDIT:
If you run a script using the -i option (for interactive), the plot GUI will stay open and a REPL will launch after the script is done running:
$ julia -i script.jl
The various options to the Julia executable can be found here, or can be found by typing $ julia --help in a terminal.
The import time can be eliminated by compiling the Plots.jl package ahead of time and baking it into your julia executable using the PackageCompiler.jl package. Here is a link to the package website
https://julialang.github.io/PackageCompiler.jl/dev/
See the tutorial on how to do this, the first tutorial is about compiling a syntax highlighting package. That one replaces your default 'sysimage'. The second tutorial is about creating a loadable 'sysimage' where Plots.jl is compiled, and starting Julia with this 'sysimage' so it doesn't have to complie Plots.jl again.
However I coudln't get the second 'loadable sysimage' working so I used the first tutorials way even for Plots.jl.
After this change, using Plots and plot() are basically instant.
Related
I am used to starting all my Matlab scripts with clear all; close all; clc to ensure I am not looking at any old data or plots. I found Julia's clearconsole() to be equivalent to Matlab's clc, but don't have working solutions for the other two Matlab commands yet. I mostly work in the Juno IDE and run scripts with the Play ("Run All") button.
The Revise.jl package is supposed to clear the workspace now that workspace() is deprecated, but it doesn't work for this simple test case. If I define x once and then comment that line out, it will continue to print each time I run without error.
using Revise
clearconsole()
#x=1
println(x)
I know I can hit "Stop" then "Play" to reset the workspace. However, that still doesn't close old plots, and the time to first plot issue makes this option undesirable.
I found the "Forget All Plots" button in Juno's plot pane, but I would like to have that functionality as a line in my script instead. Currently, it takes me three clicks to run a script again after I edit it (four if I include "Stop").
"Forget All Plots"
Somewhere in the editor to put focus back on my current file.
"Run All"
I would ideally like to rerun in a fresh environment with one click or keystroke, but any tips on a better Juno workflow would be appreciated.
My question was answered on the Julia discourse website: link.
Juno.clearconsole() may be used like Matlab's clc.
Writing a script within a module will clear the variables upon each run like Matlab's clear all.
A new function may be added to Juno.jl in the future which will work like Matlab's close all.
I have a script with self-written functions (no plots). When I copy-paste that script into the R-Studio console, it takes ages to execute, but when I use source("Helperfunctions.R") it doesn't take more than a second.
Question: Where does the difference in speed come from?
I am aware of two differences between running code via the source() function vs. entering code at the R-Studio console:
From ?source:
Since expressions are not executed at the top level, auto-printing is not done.
The way I understand this: source() will not plot graphs (unless made specific with e.g. print(plot)), while the R Studio console codes will always plot graphs. I'm sure this will affect the speed of execution to a certain degree, but this seems irrelevant in my case, because there are barely any plot calls.
And:
(...) the complete file is parsed before any of it is run
I have been working with R for a while now, but I'm not sure whether this relevant for the speed-issue I'm having. Is it possible that completely parsing all code "before any of it is run" speeds up the execution of my helper functions script by a factor of a hundred?
Edit: I'm using R version 3.2.3.
The issue is not source() vs. console line code. Instead, it is an issue of how RStudio sends code from the source pane to the console.
When I copy the content of Helperfunctions.R and run it in RGui (instead of RStudio), the code is executed with nearly the same speed as when I use source("Helperfunctions.R") in RStudio.
Apparently, lines of code always (?) require more execution time in RStudio than in RGui. Even though you may usually not notice the time-difference when executing a couple of lines in the console, it seems to make a huge difference when, say, 3.000 lines of code are being executed in the R Studio console at once.
My understanding is that upon using source("Helperfunctions.R") in the RStudio source pane, the code is not actually sent to the RStudio console (which would have been slow), but is actually executed directly in the R language.
In Python, running the interpreter with the -i flag first executes the script, then drops back into the interpreter
python -i hello.py
Hello world
>>> print("Python ftw")
Python ftw
>>>
which allows me to type commands and reach the variables after execution.
With R, this seems to be of great difficulty. I have been searching online for some time, and am surprised to see there is not so many results with the keywords "R run file shell interpreter".
With R, you can use
$ R -f myfile.R which executes and then exits the interpreter
$ Rscript myfile.R which still does the same thing.
Even worse, it does not plot when run like this and just exits without showing any signs that something has been plotted.
So, to repeat my question:
How do I make R to drop into the R shell after running commands from a file, a.k.a. a script?
Concurrently, how can I make R really plot the plots and not close them off immediately?
I can do these with Python, MATLAB, Octave, Ruby and many others, and should be able to do with R too.
I will answer your two questions separately:
How do I drop into a shell after my script has executed?
The function "browser" called with no arguments will allow to to drop into a shell on the line that it's called. Appending this to your script should do the trick.
How do I save graphics when not run in interactive mode?
First, check that there isn't a pdf file being created in your working directory. Depending on how you're running R, I believe it may be named "Rplots.pdf". Personally, however, I prefer to explicitly save graphics to a particular file, as such:
pdf("temp.pdf")
plot(rnorm(100))
dev.off()
which will save the plot in a new file called temp.pdf (and will overwrite any existing file by that name, so watch out).
Functions analagous to "pdf" exist for other image formats if you would prefer that.
I'm using Textmate as my code editor, and I would like to be able to run Julia from it. I have no problems saving the .jl file and sending it to the Terminal (via the Julia bundle in Textmate), but I was wondering if it is possible to make the session interactive, so, for example, the variables are stored while the session is running (so, for instance, I could send the code to Julia line by line, or have something like Rdaemon).
I use TextMate a lot with Julia. With Julia 1.0, everything got a lot more convenient. These are basically the steps you need to do:
Make sure you put your code in a package.
Start Julia in your terminal, Then `using YourPackage; using Revise
Revise.jl makes life a lot easier. You can work in TextMate and change the code of your functions and that will automatically get reflected in your REPL session. No need to reload. So you keep all your variables.
Occasionally you have to restart because you changed the visibility of a function or a type.
I have a more detailed explanation of my workflow in Julia 1.0 here.
Is there an easy way to turn of all GUI elements in R and run it solely from the command line on OSX?
I'm trying to replicate the behavior of a remote linux terminal on my OSX machine. Thus plot() should just save a file and things like CRAN mirror selection should be text, not a Tk interface. I'm having trouble finding where to set this behavior.
I had this exact question and wanted a way to do it without changing my existing code. I usually run with graphics support but sometimes I'll run a script on the server for a larger dataset and then I just want the plots to be output somewhere automatically.
In Dirk's answer Ian Fellows gives the simple solution. On the command line in R type:
options(device=pdf)
And then any plots will be written to the current directly to an Rplots.pdf file.
If you want the files to not be plotted at all then use
options(device=NULL)
For the plots you can just direct the output to a file using the pdf() command (or png(), jpeg()...).
I don't own an OS X box, but did you try to unset the X11 environment variable DISPLAY:
DISPLAY="" R --vanilla
When I do that on Linux and query R for capabilties(), x11 comes up as FALSE as desired.
I don't run OSX but you could attempt to run R from the Terminal application, rather than the Mac OSX launcher, and see whether that runs as you need.
As Matti writes, you can send output to files using the following commands; but I don't know if that's really the substance of your question.
png("pngfile.png")
plot(foo)
title(main="bar")
dev.off()
So instead of the quartz graphical object, your output goes to the file.
Similarly, you can output what would normally appear in the terminal to a file.
sink("foo.file")