{
"cells": [
{
"cell_type": "raw",
"metadata": {
"tags": []
},
"source": [
"---\n",
"title: \"Notes: Julia installation, Jupyter Lab, and Quarto\"\n",
"subtitle: Advanced Statistical Computing\n",
"author: Joong-Ho Won\n",
"date: today\n",
"date-format: \"MMMM YYYY\"\n",
"institute: Seoul National University\n",
"execute:\n",
" echo: true\n",
"format:\n",
" html:\n",
" toc: false\n",
" code-fold: false \n",
"jupyter: julia \n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Installing Julia\n",
"\n",
"* Install the current stable release of Julia from \n",
"\n",
"* Follow the platform-specific instructions in \n",
"\n",
"* Make sure you run Julia REPL from the command line."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Julia REPL (Read-Eval-Print Loop)\n",
"\n",
"The `Julia` REPL, or `Julia` shell, has at least five modes.\n",
"\n",
"1. **Default mode** is the Julia prompt `julia>`. *Type backspace in other modes* to return to the default mode. \n",
"\n",
"2. **Help mode** `help?>`. Type `?` to enter help mode. `?search_term` does a fuzzy search for `search_term`. \n",
"\n",
"3. **Shell mode** `shell>`. Type `;` to enter shell mode. \n",
"\n",
"4. **Package mode** `(@v1.9) pkg>`. Type `]` to enter package mode for managing Julia packages (install, uninstall, update, ...).\n",
"\n",
"5. **Search mode** `(reverse-i-search)`. Press `ctrl+R` to enter search model. \n",
"\n",
"6. With `RCall.jl` package installed, we can enter the **R mode** by typing `$` (shift+4) at Julia REPL.\n",
"\n",
"Some survival commands in Julia REPL: \n",
"1. `quit()` or `Ctrl+D`: exit Julia.\n",
"\n",
"2. `Ctrl+C`: interrupt execution.\n",
"\n",
"3. `Ctrl+L`: clear screen.\n",
"\n",
"0. Append `;` (semi-colon) to suppress displaying output from a command. Same as Matlab.\n",
"\n",
"0. `include(\"filename.jl\")` to source a Julia code file."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Jupyter \n",
"\n",
"* 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. \n",
"\n",
"* [Jupyter](http://jupyter.org) is the current development that emcompasses multiple languages including **Ju**lia, **Pyt**hon, and **R**. \n",
"\n",
"* In this course, you are required to write your homework reports using [Jupyter Lab](https://jupyter.org/install.html).\n",
"\n",
"* You can use Julia in Jupyter notebook through the [IJulia.jl](https://github.com/JuliaLang/IJulia.jl) package."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Anaconda\n",
"\n",
"- [Anaconda](https://www.anaconda.com) 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](https://en.wikipedia.org/wiki/Anaconda_(Python_distribution))]\n",
"\n",
"- Go to https://www.anaconda.com/products/individual and clike \"Download\" to install the Anaconda.\n",
"\n",
"- From [Anaconda Navigator](https://docs.anaconda.com/free/navigator/index.html), user can launch Jupyter Lab.\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Jupyter Lab Usage\n",
"\n",
"* Useful to know some keyboard shortcuts. I frequently use\n",
" * `shift + return`: execute current cell. \n",
" * `b`: create a cell below current cell.\n",
" * `a`: create a cell above current cell. \n",
" * `y`: change cell to code. \n",
" * `m`: change cell to Markdown. \n",
" Check more shortcuts in menu `Help` -> `Keyboard Shortcuts`.\n",
"\n",
"* Notebook can be **converted to other formats** such as html, LaTeX, Markdown, Julia code, and many others, via menu `File` -> `Download as`. \n",
"\n",
"* **Mathematical formula** can can be typeset as LaTeX in Markdown cells. For example, inline math: $e^{i \\pi} + 1 = 0$ and displayed math\n",
"$$\n",
" e^x = \\sum_{i=0}^\\infty \\frac{1}{i!} x^i.\n",
"$$\n",
"For multiline displayed math:\n",
"\\begin{eqnarray*}\n",
" e^x &=& \\sum_{i=0}^\\infty \\frac{1}{i!} x^i \\\\\n",
" &\\approx& 1 + x + \\frac{x^2}{2}.\n",
"\\end{eqnarray*}\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### IJulia\n",
"\n",
"Install IJulia from the Julia REPL by pressing ] to enter pkg mode and entering:\n",
"```julia\n",
"# in Pkg mode\n",
"(@v1.9) pkg> add IJulia\n",
"```\n",
"\n",
"If you already have Jupyter installed on your machine, this process will also install a kernel specification that tells Jupyter how to launch Julia. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Julia package system"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"versioninfo()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Like R, the functionality of Julia can be extended by using packages.\n",
"\n",
"* Each Julia package is a Git repository. Each Julia package name ends with `.jl`. E.g., `Distributions.jl` package lives at . \n",
"Google search with `PackageName.jl` usually leads to the package on github.com. \n",
"\n",
"* 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/ \n",
"\n",
"* For example, the package called `Distributions.jl` is added with\n",
"```julia\n",
"# in Pkg mode\n",
"(@v1.9) pkg> add Distributions\n",
"```\n",
"and \"removed\" (although not completely deleted) with\n",
"```julia\n",
"# in Pkg mode\n",
"(@v1.9) pkg> rm Distributions\n",
"```\n",
"* The package manager provides a dependency solver that determines which packages are actually required to be installed.\n",
"\n",
"* **Non-registered** packages are added by cloning the relevant Git repository. E.g.,\n",
"```julia\n",
"# in Pkg mode\n",
"(@v1.9) pkg> add https://github.com/kose-y/ParProx.jl\n",
"```\n",
"\n",
"* A package needs only be added once, at which point it is downloaded into your local `.julia/packages` directory in your home directory. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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](https://bkamins.github.io/julialang/2020/05/10/julia-project-environments.html)). For example, this lecture note is a project whose environment has the following dependencies."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"using Pkg\n",
"Pkg.activate(pwd())\n",
"Pkg.instantiate()\n",
"Pkg.dependencies()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Directory of a specific package can be queried by `pathof()`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"using Distributions\n",
"\n",
"pathof(Distributions) # should look different in your machine"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* 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. \n",
"\n",
"* Periodically, one should run `update` in Pkg mode, which checks for, downloads and installs updated versions of all the packages you currently have installed.\n",
"\n",
"* `status` lists the status of all installed packages.\n",
"\n",
"* Using functions in package.\n",
"```julia\n",
"using Distributions\n",
"```\n",
"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\n",
"```julia\n",
"import Distributions\n",
"```\n",
"Now, the functions from the Distributions package are available only using \n",
"```julia\n",
"Distributions.\n",
"```\n",
"All functions, not only exported functions, are always available like this."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating your own environments\n",
"\n",
"* By default, packages are added to the default environment at `~/.julia/environments/v1.9`. \n",
"\n",
"* 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. \n",
"\n",
"* 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:\n",
"```julia\n",
"(@v1.9) pkg> activate MyProject\n",
"Activating new environment at `~/MyProject/Project.toml`\n",
"\n",
"(MyProject) pkg> st\n",
" Status `~/MyProject/Project.toml` (empty project)\n",
"```\n",
"\n",
"* 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`.\n",
"\n",
"* The above code (default mode in Julia REPL)\n",
"```julia\n",
"using Pkg\n",
"Pkg.activate(pwd())\n",
"Pkg.instantiate()\n",
"```\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quarto\n",
"\n",
"* [Quarto](https://quarto.org), developed by Posit (formerly RStudio), is an open-source scientific and technical publishing system.\n",
"\n",
"* Install Quarto by following the instructions at \n",
"\n",
"* This note is converted from the Jupyter notebook (`jupyter.ipynb`) to HTML by running\n",
"```bash\n",
" quarto render jupyter.ipynb\n",
"```\n",
"from the command line."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.9.3",
"language": "julia",
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.9.3"
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "311px",
"width": "252px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"threshold": 4,
"toc_cell": true,
"toc_position": {
"height": "262.5px",
"left": "0px",
"right": "800px",
"top": "140.5px",
"width": "204px"
},
"toc_section_display": "block",
"toc_window_display": true,
"widenNotebook": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}