Getting Started with Elixir Data Exploration
This document demonstrates how to use Elixir and Livebook for interactive data exploration. We'll learn basic data processing, visualization, and analysis techniques.
What you'll learn:
- Processing collection data with the Enum module
- Creating interactive tables with Kino
- Data visualization with VegaLite
Setup
First, let's install the necessary dependencies:
Mix.install([ {:kino, "~> 0.12"}, {:vega_lite, "~> 0.1"}, {:kino_vega_lite, "~> 0.1"} ])
:ok
Creating Sample Data
Let's create a dataset containing programming language popularity:
# Define programming language data languages = [ %{name:"Elixir", year:2011, type:"Functional", popularity:85}, %{name:"Rust", year:2010, type:"Systems", popularity:92}, %{name:"Go", year:2009, type:"Systems", popularity:88}, %{name:"TypeScript", year:2012, type:"Scripting", popularity:95}, %{name:"Kotlin", year:2011, type:"JVM", popularity:78}, %{name:"Swift", year:2014, type:"Mobile", popularity:82} ]
[
%{name: "Elixir", popularity: 85, type: "Functional", year: 2011},
%{name: "Rust", popularity: 92, type: "Systems", year: 2010},
%{name: "Go", popularity: 88, type: "Systems", year: 2009},
%{name: "TypeScript", popularity: 95, type: "Scripting", year: 2012},
%{name: "Kotlin", popularity: 78, type: "JVM", year: 2011},
%{name: "Swift", popularity: 82, type: "Mobile", year: 2014}
]Displaying Interactive Tables with Kino
Kino can render data as beautiful interactive tables:
Kino.DataTable.new(languages)| Name | Year | Type | Popularity |
|---|---|---|---|
| Elixir | 2011 | Functional | 85 |
| Rust | 2010 | Systems | 92 |
| Go | 2009 | Systems | 88 |
| TypeScript | 2012 | Scripting | 95 |
| Kotlin | 2011 | JVM | 78 |
| Swift | 2014 | Mobile | 82 |
Data Processing and Analysis
Using the Enum module for data filtering and transformation:
# Filter languages released after 2010 with popularity > 80 languages |> Enum.filter(fn lang -> lang.year >= 2010and lang.popularity > 80end) |> Enum.map(fn lang -> "#{lang.name} (#{lang.year})"end)
["Elixir (2011)", "Rust (2010)", "TypeScript (2012)", "Swift (2014)"]
# Calculate average popularity avg_popularity = languages |> Enum.map(& &1.popularity) |> Enum.sum() |> Kernel./(length(languages)) |> Float.round(1) "Average popularity: #{avg_popularity}"
"Average popularity: 86.7"
Data Visualization
Creating charts with VegaLite:
alias VegaLite, as: Vl Vl.new(width:500, height:300, title:"Programming Language Popularity") |> Vl.data_from_values(languages) |> Vl.mark(:bar) |> Vl.encode_field(:x, "name", type::nominal, title:"Language") |> Vl.encode_field(:y, "popularity", type::quantitative, title:"Popularity") |> Vl.encode_field(:color, "type", type::nominal)
Summary
In this Livebook document, we learned:
- How to create and process Elixir data structures
- Using the
Enummodule for data filtering and transformation - Using
Kinoto display interactive tables - Using
VegaLiteto create data visualizations
Next steps: Try modifying the code, adding more data, or creating different types of charts!