Notes: Julia installation, Jupyter Lab, and Quarto

Advanced Statistical Computing

Author
Affiliation

Joong-Ho Won

Seoul National University

Published

September 2023

Installing Julia

Julia REPL (Read-Eval-Print Loop)

The Julia REPL, or Julia shell, has at least five modes.

  1. Default mode is the Julia prompt julia>. Type backspace in other modes to return to the default mode.

  2. Help mode help?>. Type ? to enter help mode. ?search_term does a fuzzy search for search_term.

  3. Shell mode shell>. Type ; to enter shell mode.

  4. Package mode (@v1.9) pkg>. Type ] to enter package mode for managing Julia packages (install, uninstall, update, …).

  5. Search mode (reverse-i-search). Press ctrl+R to enter search model.

  6. With RCall.jl package installed, we can enter the R mode by typing $ (shift+4) at Julia REPL.

Some survival commands in Julia REPL:
1. quit() or Ctrl+D: exit Julia.

  1. Ctrl+C: interrupt execution.

  2. Ctrl+L: clear screen.

  3. Append ; (semi-colon) to suppress displaying output from a command. Same as Matlab.

  4. include("filename.jl") to source a Julia code file.

Jupyter

  • IPython notebook (precursor of Jupyter notebook) is a powerful tool for authoring dynamic document in Python, which combines code, formatted text, math, and multimedia in a single document.

  • Jupyter is the current development that emcompasses multiple languages including Julia, Python, and R.

  • In this course, you are required to write your homework reports using Jupyter Lab.

  • You can use Julia in Jupyter notebook through the IJulia.jl package.

Anaconda

  • Anaconda is a distribution of the Python and R programming languages for scientific computing, that aims to simplify package management and deployment. The distribution includes data-science packages suitable for Windows, Linux, and macOS. [Wikipedia]

  • Go to https://www.anaconda.com/products/individual and clike “Download” to install the Anaconda.

  • From Anaconda Navigator, user can launch Jupyter Lab.

Jupyter Lab Usage

  • Useful to know some keyboard shortcuts. I frequently use

    • shift + return: execute current cell.
    • b: create a cell below current cell.
    • a: create a cell above current cell.
    • y: change cell to code.
    • m: change cell to Markdown.
      Check more shortcuts in menu Help -> Keyboard Shortcuts.
  • Notebook can be converted to other formats such as html, LaTeX, Markdown, Julia code, and many others, via menu File -> Download as.

  • Mathematical formula can can be typeset as LaTeX in Markdown cells. For example, inline math: \(e^{i \pi} + 1 = 0\) and displayed math \[ e^x = \sum_{i=0}^\infty \frac{1}{i!} x^i. \] For multiline displayed math: \[\begin{eqnarray*} e^x &=& \sum_{i=0}^\infty \frac{1}{i!} x^i \\ &\approx& 1 + x + \frac{x^2}{2}. \end{eqnarray*}\]

IJulia

Install IJulia from the Julia REPL by pressing ] to enter pkg mode and entering:

# in Pkg mode
(@v1.9) pkg> add IJulia

If you already have Jupyter installed on your machine, this process will also install a kernel specification that tells Jupyter how to launch Julia.

Julia package system

versioninfo()
Julia Version 1.9.3
Commit bed2cd540a1 (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (x86_64-apple-darwin22.4.0)
  CPU: 8 × Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, skylake)
  Threads: 2 on 8 virtual cores
  • Like R, the functionality of Julia can be extended by using packages.

  • Each Julia package is a Git repository. Each Julia package name ends with .jl. E.g., Distributions.jl package lives at https://github.com/JuliaStats/Distributions.jl.
    Google search with PackageName.jl usually leads to the package on github.com.

  • The package ecosystem is rapidly maturing; a complete list of registered packages (which are required to have a certain level of testing and documentation) is at https://julialang.org/packages/

  • For example, the package called Distributions.jl is added with

# in Pkg mode
(@v1.9) pkg> add Distributions

and “removed” (although not completely deleted) with

# in Pkg mode
(@v1.9) pkg> rm Distributions
  • The package manager provides a dependency solver that determines which packages are actually required to be installed.

  • Non-registered packages are added by cloning the relevant Git repository. E.g.,

# in Pkg mode
(@v1.9) pkg> add https://github.com/kose-y/ParProx.jl
  • A package needs only be added once, at which point it is downloaded into your local .julia/packages directory in your home directory.

Package dependencies are very important in reproducing results. In Julia they can be managed at each project, with `Project.toml’ and ‘Manifest.toml’ files (see here). For example, this lecture note is a project whose environment has the following dependencies.

using Pkg
Pkg.activate(pwd())
Pkg.instantiate()
Pkg.dependencies()
  Activating project at `~/Dropbox/class/M1399.000200/2023/M1300_000200-2023fall/lectures/02-juliaintro`
Dict{Base.UUID, Pkg.API.PackageInfo} with 216 entries:
  UUID("49dc2e85-a5d0-5ad3… => PackageInfo("Calculus", v"0.5.1", "f641eb0a4f00c…
  UUID("6c6a2e73-6563-6170… => PackageInfo("Scratch", v"1.2.0", "30449ee1223762…
  UUID("efcefdf7-47ab-520b… => PackageInfo("PCRE2_jll", v"10.42.0+0", nothing, …
  UUID("3da002f7-5984-5a60… => PackageInfo("ColorTypes", v"0.11.4", "eb7f0f8307…
  UUID("104b5d7c-a370-577a… => PackageInfo("WebSockets", v"1.6.0", "4162e95e05e…
  UUID("aacddb02-875f-59d6… => PackageInfo("JpegTurbo_jll", v"2.1.91+0", "6f267…
  UUID("dfaa095f-4041-5dcd… => PackageInfo("x265_jll", v"3.5.0+0", "ee567a171cc…
  UUID("a63ad114-7e13-5084… => PackageInfo("Mmap", nothing, nothing, false, fal…
  UUID("91a5bcdd-55d7-5caf… => PackageInfo("Plots", v"1.39.0", "ccee59c6e48e6f2…
  UUID("76f85450-5226-5b5a… => PackageInfo("LibGit2", nothing, nothing, false, …
  UUID("f638f0a6-7fb0-5443… => PackageInfo("libfdk_aac_jll", v"2.0.2+0", "daacc…
  UUID("f7e6163d-2fa5-5f23… => PackageInfo("Kaleido_jll", v"0.2.1+0", "43032da5…
  UUID("2a0f44e3-6c83-55bd… => PackageInfo("Base64", nothing, nothing, false, f…
  UUID("f0f68f2c-4968-5e81… => PackageInfo("PlotlyJS", v"0.18.10", "7452869933c…
  UUID("05823500-19ac-5b8b… => PackageInfo("OpenLibm_jll", v"0.8.1+0", nothing,…
  UUID("4b2f31a3-9ecc-558c… => PackageInfo("Libmount_jll", v"2.35.0+0", "9c3053…
  UUID("d2c73de3-f751-5644… => PackageInfo("GR_jll", v"0.72.9+1", "1596bab77f4f…
  UUID("34004b35-14d8-5ef3… => PackageInfo("HypergeometricFunctions", v"0.3.23"…
  UUID("1082639a-0dae-5f34… => PackageInfo("Xorg_libXext_jll", v"1.3.4+4", "b7c…
  UUID("88015f11-f218-50d7… => PackageInfo("LERC_jll", v"3.0.0+1", "bf36f528eec…
  UUID("b77e0a4c-d291-57a0… => PackageInfo("InteractiveUtils", nothing, nothing…
  UUID("0f8b85d8-7281-11e9… => PackageInfo("JSON3", v"1.13.2", "95220473901735a…
  UUID("4c63d2b9-4356-54db… => PackageInfo("StatsFuns", v"1.3.0", "f625d686d5a8…
  UUID("21216c6a-2e73-6563… => PackageInfo("Preferences", v"1.4.0", "7eb1686b4f…
  UUID("62fd8b95-f654-4bbd… => PackageInfo("TensorCore", v"0.1.1", "1feb45f88d1…
  ⋮                         => ⋮
  • Directory of a specific package can be queried by pathof():
using Distributions

pathof(Distributions)  # should look different in your machine
"/Users/jhwon/.julia/packages/Distributions/Ufrz2/src/Distributions.jl"
  • If you start having problems with packages that seem to be unsolvable, you may try just deleting your .julia directory and reinstalling all your packages.

  • Periodically, one should run update in Pkg mode, which checks for, downloads and installs updated versions of all the packages you currently have installed.

  • status lists the status of all installed packages.

  • Using functions in package.

using Distributions

This pulls all of the exported functions in the module into your local namespace, as you can check using the whos() command. An alternative is

import Distributions

Now, the functions from the Distributions package are available only using

Distributions.<FUNNAME>

All functions, not only exported functions, are always available like this.

Creating your own environments

  • By default, packages are added to the default environment at ~/.julia/environments/v1.9.

  • It is however easy to create other, independent, projects. This approach has the benefit of allowing you to check in a Project.toml, and even a Manifest.toml if you wish, into version control (e.g. git) alongside your code.

  • In order to create a new project, create a directory for it and then activate that directory to make it the “active project”, which package operations manipulate:

(@v1.9) pkg> activate MyProject
Activating new environment at `~/MyProject/Project.toml`

(MyProject) pkg> st
    Status `~/MyProject/Project.toml` (empty project)
  • Note that the REPL prompt changes when the new project is activated. Until a package is added, there are no files in this environment and the directory to the environment might not even be created. Added packages and dependencies are stored in ~/MyProject/Project.toml and ~/MyProject/Manifest.toml.

  • The above code (default mode in Julia REPL)

using Pkg
Pkg.activate(pwd())
Pkg.instantiate()

activates the Project.toml in the current working directory and sets a new environment; instantiate installs and precompiles any missing packages for the environment to be ready.

Quarto

  • Quarto, developed by Posit (formerly RStudio), is an open-source scientific and technical publishing system.

  • Install Quarto by following the instructions at https://quarto.org/docs/get-started/

  • This note is converted from the Jupyter notebook (jupyter.ipynb) to HTML by running

    quarto render jupyter.ipynb

from the command line.