Julia Basics on Nextjournal

1.
Installing packages

J packages are installed normally via the interpreter with Pkg.add(). Preinstalled packages may be updated with Pkg.update().

Pkg.add("Crayons")






The cell above is not a standard Julia code cell, but a Julia Setup cell. The difference is that a setup cell will inherit and bequeath only filesystem state, just like a Bash cell. This is desirable for cells performing actions like installing packages, as it will discard the fragile process state, making it unlikely we will have to rerun the cell in the future. We can convert a standard language cell to a setup cell via the cell's menu.

Pkg.pin("Plots") # keeps Plots working
Pkg.update()






















































































































We can further protect a setup cell by Locking it after it's been run. This prevents editing and rerunning. By locking an article's setup cells (Bash and Language), we can ensure that the versions of programs and packages the article was developed on will be preserved for future reproducibility, even through a Remix.

2.
Data intake

Files downloaded into the filesystem can be accessed normally, as long as our inheritance chain connects back to the download cell. However, we can also upload files directly to the article via the insert menu.

cubic.csv
Download

This will store the file in a persistent, versioned database. To access the file in a code cell, simply start typing the file's name—an autocomplete dialog will pop up for you to finish inserting a file reference.

data = readcsv(cubic.csv)

We can also access files that were placed in /results/. These files are stored in the same database, but to reference these we start by typing the name of the cell that created the file, then again find the file in the autocomplete list.

echo 'Some text.
Some "other" text.' > /results/test.text
test.text
Download
str = open(readstring, create test file.test.text)

Note that in addition to the output on the right side of code cells, the article page will attempt to load and show the results of the last statement in the cell. Strings and simple variables will be printed, while more complicated structures (arrays, dictionaries) will appear as an expandable tree.

Like files, we can reference variables in other language cells without inheriting their process state. Just start typing the cell's name, then select the variable from the list.

print(read test file.str)



3.
Plotting

Julia supports the Plots.jl out of the box, with the Plotly and GR backends. We'll just plot the data we uploaded earlier. The default backend is Plotly.

Plots.scatter(read uploaded file.data,
  title="Cubic Function", xlab="x", ylab="f(x)",
  legend=:none
)

The GR backend provides little interactivity, but can be useful for plotting large amounts of data. With Plots.jl, all we have to do is tell it use the different backend.

Plots.gr() # switch backend

Plots.scatter(read uploaded file.data,
  title="Cubic Function", xlab="x", ylab="f(x)",
  legend=:none
)