<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>SSPHub</title>
<link>https://ssphub.netlify.app/blog.html</link>
<atom:link href="https://ssphub.netlify.app/blog.xml" rel="self" type="application/rss+xml"/>
<description>Blog du réseau des data scientists de la statistique publique</description>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Mon, 23 Oct 2023 00:00:00 GMT</lastBuildDate>
<item>
  <title>Guide d’utilisation des données du recensement de la population au format Parquet</title>
  <link>https://ssphub.netlify.app/post/parquetRP/</link>
  <description><![CDATA[ 





<p>Ce guide présente quelques exemples d’utilisation des données du recensement de la population diffusées au format <code>Parquet</code>. Il s’agit d’une version HTML enrichissant le guide publié sur le site <a href="https://www.insee.fr/fr/statistiques/7637890">insee.fr</a> pour les langages <code>Python</code> <i class="fa-brands fa-python" aria-label="python"></i> et <i class="fa-brands fa-r-project" aria-label="r-project"></i> avec des exemples interactifs pouvant être construits par le biais de <code>Quarto Markdown</code> et <code>Observable</code>.</p>
<p>L’ensemble des codes utilisés pour produire cette note est disponible sur le dépôt <a href="https://github.com/InseeFrLab/exemples-recensement-parquet"><code>Github</code> <i class="fa-brands fa-github" aria-label="github"></i> InseeFrLab/exemples-recensement-parquet</a> au format <a href="https://quarto.org/"><code>Quarto Markdown</code></a>.</p>
<p>Pour plus d’informations sur le format <code>Parquet</code>, dans un contexte de statistique publique, se référer à <span class="citation" data-cites="dondon-lamarche-2023">Dondon et Lamarche (2023)</span>. Pour un exemple sur la différence entre format <code>CSV</code> et <code>Parquet</code> illustré sur les données du recensement de la population, voir <span class="citation" data-cites="mauviere-2022">Mauvière (2022)</span>.</p>
<p>Ce guide propose d’utiliser <a href="https://duckdb.org/"><code>DuckDB</code></a> à travers plusieurs langages pour effectuer des traitements sur les fichiers détails du recensement. Par rapport à d’autres approches, <a href="https://duckdb.org/"><code>DuckDB</code></a> a été choisi pour son efficacité ainsi que pour son universalité<sup>1</sup>.</p>
<details>
<summary>
Afficher le dictionnaire des variables
</summary>
<p>Il est possible de rechercher dans la documentation des noms de variables ou des informations dans les descriptions. Lorsqu’une variable présente de nombreuses modalités, seules la première est la dernière sont retournées. Un exemple ci-dessous montre comment utiliser une requête SQL pour récupérer l’ensemble des valeurs d’une telle variable.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb1" data-startfrom="68" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 67;"><span id="cb1-68">viewof unique_description_filter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">search</span>(</span>
<span id="cb1-69">  documentation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {</span>
<span id="cb1-70">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">placeholder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Trouver une information dans la documentation"</span></span>
<span id="cb1-71">  }</span>
<span id="cb1-72">  )</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb2" data-startfrom="77" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 76;"><span id="cb2-77">table_filtre_description <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb2-78">  unique_description_filter<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb2-79">  {</span>
<span id="cb2-80">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">header</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {</span>
<span id="cb2-81">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">description_variable</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Description de la variable"</span></span>
<span id="cb2-82">    }    </span>
<span id="cb2-83">  }</span>
<span id="cb2-84">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb3" data-startfrom="89" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 88;"><span id="cb3-89">documentation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`SELECT * FROM documentation_indiv`</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-3" data-nodetype="declaration">

</div>
</div>
</div>
</details>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Ce <em>post</em> accompagne la mise à disposition des données du recensement de la population au format <code>Parquet</code> sur le site <a href="https://www.insee.fr/fr/statistiques/7706119?sommaire=7637890">insee.fr</a>.</p>
<p>Il vise à fournir une expérience plus ergonomique et des exemples supplémentaires liés au langage <code>Javascript</code> au guide PDF disponible sur la page du site <a href="https://www.insee.fr/fr/statistiques/7706119?sommaire=7637890">insee.fr</a>.</p>
<p>⚠️ Cette page s’appuie sur l’exploitation de données qui sont préalablement téléchargées de manière automatique par le navigateur <em>web</em>. Celles-ci représentent une volumétrie autour de 1Go, ce qui peut nécessiter un certain temps en fonction de la bande passante. Une fois téléchargées, l’exploitation de ces données nécessite néanmoins beaucoup moins de ressources grâce à l’optimisation permise par <code>DuckDB</code>.</p>
</div>
</div>
<section id="initialisation" class="level1">
<h1>Initialisation</h1>
<p>Les données sont disponibles sur le site <code>data.gouv</code> aux adresses suivantes :</p>
<ul>
<li><a href="https://www.insee.fr/fr/statistiques/7706119?sommaire=7637890">Fichier détail individuel</a> ;</li>
<li><a href="https://www.insee.fr/fr/statistiques/7705908?sommaire=7637890">Fichier détail logement</a>.</li>
</ul>
<p>Dans la suite de ce guide, il sera fait l’hypothèse pour <code>Python</code> et <code>R</code> que les données sont récupérées depuis les URL stables, enregistrées dans un même dossier et qu’il existe une variable <em>ad hoc</em> enregistrant ce chemin dans <code>Python</code> ou <code>R</code>. En ce qui concerne <code>Observable</code>, il est proposé d’utiliser directement l’URL de mise à disposition des fichiers.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>Les URL stables de mise à dispositon des données seront directement renseignées dans la création de la base de données.</p>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Mise en garde</span>Emplacement des données pour les exemples <code>Observable</code>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>En ce qui concerne <code>Observable</code>, il est proposé d’utiliser directement l’URL de mise à disposition des fichiers car l’extension <code>httpfs</code> est nativement intégrée à la librairie <code>DuckDB</code>, <em>a contrario</em> des clients <code>Python</code> et <code>R</code>.</p>
<p>Ces jeux de données sont automatiquement téléchargés par le navigateur et mis en <em>cache</em> pour accélérer des utilisations ultérieures de la page. Le premier chargement de tous les tableaux et graphiques sur celle-ci peut ainsi prendre un peu plus de temps.</p>
</div>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">path_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C:/MesDocuments/dossierpersonnel'</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remplacer par l'emplacement où sont stockées </span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># les données</span></span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">path_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C:/MesDocuments/dossierpersonnel'</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remplacer par l'emplacement où sont stockées </span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># les données</span></span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">path_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C:/MesDocuments/dossierpersonnel'</span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remplacer par l'emplacement où sont stockées </span></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># les données</span></span></code></pre></div></div>
</div>
</div>
</div>
<p>Il est proposé, pour initialiser la connexion entre les données <code>Parquet</code> et le langage client (<code>Javascript</code>, <code>R</code> ou <code>Python</code>) d’utiliser des vues. Ceci permet de faire référence de manière répétée à la même source de données par le biais d’un alias (<code>table_logement</code> ou <code>table_individu</code>).</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-2-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-3" aria-controls="tabset-2-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-2-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-4" aria-controls="tabset-2-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<details>
<summary>
Dérouler pour découvrir le code <code>Observable</code> d’initialisation de la base de données avec <code>DuckDB</code>
</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb7-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb7-2">url_table_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.data.gouv.fr/fr/datasets/r/f314175a-6d33-4ee4-b5eb-2cb6c29df2c2"</span></span>
<span id="cb7-3">url_table_individu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.data.gouv.fr/fr/datasets/r/c8e1b241-75fe-43e9-a266-830fc30ec61d"</span></span>
<span id="cb7-4">url_doc_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.data.gouv.fr/fr/datasets/r/c274705f-98db-4d9b-9674-578e04f03198"</span></span>
<span id="cb7-5">url_doc_individu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.data.gouv.fr/fr/datasets/r/1c6c6ab2-b766-41a4-90f0-043173d5e9d1"</span></span>
<span id="cb7-6"></span>
<span id="cb7-7">renommage_documentation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`SELECT </span></span>
<span id="cb7-8"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  COD_VAR AS nom_variable, </span></span>
<span id="cb7-9"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  LIB_VAR AS description_variable, </span></span>
<span id="cb7-10"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  TYPE_VAR AS type_variable, </span></span>
<span id="cb7-11"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  COD_MOD AS code_modalite, </span></span>
<span id="cb7-12"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  LIB_MOD AS description_modalite,</span></span>
<span id="cb7-13"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  LONG_VAR as longueur_variable`</span></span>
<span id="cb7-14"></span>
<span id="cb7-15">db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb7-16">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> configuredClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> DuckDBClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">of</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-17"></span>
<span id="cb7-18">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb7-19"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW table_individu </span></span>
<span id="cb7-20"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS SELECT *</span></span>
<span id="cb7-21"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM read_parquet('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_table_individu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">')`</span></span>
<span id="cb7-22">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-23"></span>
<span id="cb7-24">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb7-25"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW table_logement </span></span>
<span id="cb7-26"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS SELECT *</span></span>
<span id="cb7-27"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM read_parquet('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_table_logement<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">')`</span></span>
<span id="cb7-28">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-29"></span>
<span id="cb7-30">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb7-31"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW documentation_logement</span></span>
<span id="cb7-32"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>renommage_documentation<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> FROM read_csv_auto('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_doc_logement<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">', header=true)`</span></span>
<span id="cb7-33">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-34">  </span>
<span id="cb7-35">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb7-36"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW documentation_indiv</span></span>
<span id="cb7-37"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>renommage_documentation<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> FROM read_csv_auto('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_doc_individu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">', header=true)`</span></span>
<span id="cb7-38">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb7-39"></span>
<span id="cb7-40">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> configuredClient</span>
<span id="cb7-41">}</span>
<span id="cb7-42"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb8" data-startfrom="248" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 247;"><span id="cb8-248">url_table_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://static.data.gouv.fr/resources/recensement-de-la-population-fichiers-detail-logements-ordinaires-en-2020-1/20231023-123618/fd-logemt-2020.parquet"</span></span>
<span id="cb8-249">url_table_individu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://static.data.gouv.fr/resources/recensement-de-la-population-fichiers-detail-individus-localises-au-canton-ou-ville-2020-1/20231023-122841/fd-indcvi-2020.parquet"</span></span>
<span id="cb8-250">url_doc_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.data.gouv.fr/fr/datasets/r/c274705f-98db-4d9b-9674-578e04f03198"</span></span>
<span id="cb8-251">url_doc_individu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.data.gouv.fr/fr/datasets/r/1c6c6ab2-b766-41a4-90f0-043173d5e9d1"</span></span>
<span id="cb8-252"></span>
<span id="cb8-253">renommage_documentation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`SELECT </span></span>
<span id="cb8-254"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  COD_VAR AS nom_variable, </span></span>
<span id="cb8-255"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  LIB_VAR AS description_variable, </span></span>
<span id="cb8-256"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  TYPE_VAR AS type_variable, </span></span>
<span id="cb8-257"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  COD_MOD AS code_modalite, </span></span>
<span id="cb8-258"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  LIB_MOD AS description_modalite,</span></span>
<span id="cb8-259"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  LONG_VAR as longueur_variable`</span></span>
<span id="cb8-260"></span>
<span id="cb8-261">db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb8-262">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> configuredClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> DuckDBClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">of</span>()<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-263"></span>
<span id="cb8-264">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb8-265"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW table_individu </span></span>
<span id="cb8-266"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS SELECT *</span></span>
<span id="cb8-267"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM read_parquet('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_table_individu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">')`</span></span>
<span id="cb8-268">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-269"></span>
<span id="cb8-270">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb8-271"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW table_logement </span></span>
<span id="cb8-272"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS SELECT *</span></span>
<span id="cb8-273"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM read_parquet('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_table_logement<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">')`</span></span>
<span id="cb8-274">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-275">  </span>
<span id="cb8-276">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb8-277"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW documentation_indiv</span></span>
<span id="cb8-278"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>renommage_documentation<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> FROM read_csv_auto('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_doc_individu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">', header=true)`</span></span>
<span id="cb8-279">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-280"></span>
<span id="cb8-281">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb8-282"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW documentation_logement</span></span>
<span id="cb8-283"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    AS </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>renommage_documentation<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;"> FROM read_csv_auto('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>url_doc_logement<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">', header=true)`</span></span>
<span id="cb8-284">  ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb8-285"></span>
<span id="cb8-286">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> configuredClient</span>
<span id="cb8-287">}</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-3" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-4" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-5" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-4-6" data-nodetype="declaration">

</div>
</div>
</div>
</div>
</details>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> duckdb</span>
<span id="cb9-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> os.path.isdir(path_data):</span>
<span id="cb9-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path_data n'est pas un répertoire valide."</span>)</span>
<span id="cb9-6"></span>
<span id="cb9-7">duckdb.sql(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'''</span></span>
<span id="cb9-8"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  CREATE OR REPLACE VIEW table_individu</span></span>
<span id="cb9-9"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  AS SELECT * FROM read_parquet("</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>path_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/FD_INDCVI_2020.parquet")</span></span>
<span id="cb9-10"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'''</span></span>
<span id="cb9-11">)</span>
<span id="cb9-12"></span>
<span id="cb9-13">duckdb.sql(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'''</span></span>
<span id="cb9-14"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  CREATE OR REPLACE VIEW table_logement</span></span>
<span id="cb9-15"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  AS SELECT * FROM read_parquet("</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>path_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/FD_LOGEMT_2020.parquet")</span></span>
<span id="cb9-16"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'''</span></span>
<span id="cb9-17">)</span>
<span id="cb9-18"></span>
<span id="cb9-19">duckdb.sql(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'''</span></span>
<span id="cb9-20"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  CREATE OR REPLACE VIEW documentation_indiv</span></span>
<span id="cb9-21"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  AS SELECT COD_VAR AS nom_variable,</span></span>
<span id="cb9-22"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     LIB_VAR AS description_variable,</span></span>
<span id="cb9-23"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     TYPE_VAR AS type_variable,</span></span>
<span id="cb9-24"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     COD_MOD AS code_modalite,</span></span>
<span id="cb9-25"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     LIB_MOD AS description_modalite,</span></span>
<span id="cb9-26"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     LONG_VAR as longueur_variable</span></span>
<span id="cb9-27"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  FROM read_csv_auto("</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>path_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/dictionnaire_variables_indcvi_2020.csv", header=true)</span></span>
<span id="cb9-28"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'''</span></span>
<span id="cb9-29">)</span>
<span id="cb9-30"></span>
<span id="cb9-31">duckdb.sql(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'''</span></span>
<span id="cb9-32"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  CREATE OR REPLACE VIEW documentation_logement</span></span>
<span id="cb9-33"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  AS SELECT COD_VAR AS nom_variable,</span></span>
<span id="cb9-34"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     LIB_VAR AS description_variable,</span></span>
<span id="cb9-35"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     TYPE_VAR AS type_variable,</span></span>
<span id="cb9-36"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     COD_MOD AS code_modalite,</span></span>
<span id="cb9-37"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     LIB_MOD AS description_modalite,</span></span>
<span id="cb9-38"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">     LONG_VAR as longueur_variable</span></span>
<span id="cb9-39"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  FROM read_csv_auto("</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>path_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">/dictionnaire_variables_logemt_2020.csv", header=true)</span></span>
<span id="cb9-40"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'''</span></span>
<span id="cb9-41">)</span></code></pre></div></div>
</div>
<div id="tabset-2-3" class="tab-pane" aria-labelledby="tabset-2-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(duckdb)</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glue)</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pour créer une base de données en mémoire</span></span>
<span id="cb10-5">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbConnect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">duckdb</span>())</span>
<span id="cb10-6"></span>
<span id="cb10-7">path_data_sql <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DBI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SQL</span>(path_data)</span>
<span id="cb10-8"></span>
<span id="cb10-9"></span>
<span id="cb10-10">renommage_documentation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  DBI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SQL</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb10-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT"</span>,</span>
<span id="cb10-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COD_VAR AS nom_variable,"</span>,</span>
<span id="cb10-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LIB_VAR AS description_variable,"</span>,</span>
<span id="cb10-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TYPE_VAR AS type_variable,"</span>,</span>
<span id="cb10-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COD_MOD AS code_modalite,"</span>,</span>
<span id="cb10-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LIB_MOD AS description_modalite,"</span>,</span>
<span id="cb10-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LONG_VAR as longueur_variable"</span></span>
<span id="cb10-18">))</span>
<span id="cb10-19"></span>
<span id="cb10-20"></span>
<span id="cb10-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb10-22">  con,</span>
<span id="cb10-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(  </span>
<span id="cb10-24">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW table_individu AS '</span>,</span>
<span id="cb10-25">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SELECT * FROM read_parquet("{path_data_sql}/FD_INDCVI_2020.parquet")'</span>,</span>
<span id="cb10-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb10-27">  )</span>
<span id="cb10-28">)</span>
<span id="cb10-29"></span>
<span id="cb10-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb10-31">  con,</span>
<span id="cb10-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(  </span>
<span id="cb10-33">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW table_logement AS '</span>,</span>
<span id="cb10-34">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SELECT * FROM read_parquet("{path_data_sql}/FD_LOGEMT_2020.parquet")'</span>,</span>
<span id="cb10-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb10-36">  )</span>
<span id="cb10-37">)</span>
<span id="cb10-38"></span>
<span id="cb10-39"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb10-40">  con,</span>
<span id="cb10-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(</span>
<span id="cb10-42">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW documentation_indiv AS '</span>,</span>
<span id="cb10-43">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{renommage_documentation} FROM '</span>,</span>
<span id="cb10-44">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'read_csv_auto("{path_data_sql}/dictionnaire_variables_indcvi_2020.csv", header=true)'</span>,</span>
<span id="cb10-45">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb10-46">  )</span>
<span id="cb10-47">)</span>
<span id="cb10-48"></span>
<span id="cb10-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb10-50">  con,</span>
<span id="cb10-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(</span>
<span id="cb10-52">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW documentation_logement AS '</span>,</span>
<span id="cb10-53">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{renommage_documentation} FROM '</span>,</span>
<span id="cb10-54">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'read_csv_auto("{path_data_sql}/dictionnaire_variables_logemt_2020.csv", header=true)'</span>,</span>
<span id="cb10-55">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb10-56">  )</span>
<span id="cb10-57">)</span></code></pre></div></div>
</div>
<div id="tabset-2-4" class="tab-pane" aria-labelledby="tabset-2-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(duckdb)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb11-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glue)</span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pour créer une base de données en mémoire</span></span>
<span id="cb11-7">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbConnect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">duckdb</span>())</span>
<span id="cb11-8"></span>
<span id="cb11-9">path_data_sql <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> DBI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SQL</span>(path_data)</span>
<span id="cb11-10"></span>
<span id="cb11-11">renommage_documentation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  DBI<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">SQL</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb11-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT"</span>,</span>
<span id="cb11-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COD_VAR AS nom_variable,"</span>,</span>
<span id="cb11-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LIB_VAR AS description_variable,"</span>,</span>
<span id="cb11-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TYPE_VAR AS type_variable,"</span>,</span>
<span id="cb11-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COD_MOD AS code_modalite,"</span>,</span>
<span id="cb11-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LIB_MOD AS description_modalite,"</span>,</span>
<span id="cb11-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LONG_VAR as longueur_variable"</span></span>
<span id="cb11-19">))</span>
<span id="cb11-20"></span>
<span id="cb11-21"></span>
<span id="cb11-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb11-23">  con,</span>
<span id="cb11-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(  </span>
<span id="cb11-25">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW table_individu AS '</span>,</span>
<span id="cb11-26">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SELECT * FROM read_parquet("{path_data_sql}/FD_INDCVI_2020.parquet")'</span>,</span>
<span id="cb11-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb11-28">  )</span>
<span id="cb11-29">)</span>
<span id="cb11-30"></span>
<span id="cb11-31">table_individu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_individu"</span>)</span>
<span id="cb11-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># La requête pourrait aussi s'écrire directement dans l'appel à tbl :</span></span>
<span id="cb11-33">table_individu_direct <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'read_parquet("{path_data}/FD_INDCVI_2020.parquet")'</span>))</span>
<span id="cb11-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(table_individu_direct)</span>
<span id="cb11-35"></span>
<span id="cb11-36"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb11-37">  con,</span>
<span id="cb11-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(  </span>
<span id="cb11-39">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW table_logement AS '</span>,</span>
<span id="cb11-40">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SELECT * FROM read_parquet("{path_data_sql}/FD_LOGEMT_2020.parquet")'</span>,</span>
<span id="cb11-41">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb11-42">  )</span>
<span id="cb11-43">)</span>
<span id="cb11-44">table_logement <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"table_logement"</span>)</span>
<span id="cb11-45"></span>
<span id="cb11-46"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb11-47">  con,</span>
<span id="cb11-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(</span>
<span id="cb11-49">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW documentation_indiv AS '</span>,</span>
<span id="cb11-50">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{renommage_documentation} FROM '</span>,</span>
<span id="cb11-51">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'read_csv_auto("{path_data_sql}/dictionnaire_variables_indcvi_2020.csv", header=true)'</span>,</span>
<span id="cb11-52">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb11-53">  )</span>
<span id="cb11-54">)</span>
<span id="cb11-55">documentation_indiv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"documentation_indiv"</span>)</span>
<span id="cb11-56"></span>
<span id="cb11-57"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb11-58">  con,</span>
<span id="cb11-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(</span>
<span id="cb11-60">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW documentation_logement AS '</span>,</span>
<span id="cb11-61">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'{renommage_documentation} FROM '</span>,</span>
<span id="cb11-62">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'read_csv_auto("{path_data_sql}/dictionnaire_variables_logemt_2020.csv", header=true)'</span>,</span>
<span id="cb11-63">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb11-64">  )</span>
<span id="cb11-65">)</span>
<span id="cb11-66">documentation_logement <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"documentation_logement"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<p>Pour rapidement avoir une idée des informations présentes dans ces données, le code ci-dessous peut être utilisé :</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-3-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-1" aria-controls="tabset-3-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-3-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-2" aria-controls="tabset-3-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-3-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-3" aria-controls="tabset-3-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-3-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-3-4" aria-controls="tabset-3-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-3-1" class="tab-pane active" aria-labelledby="tabset-3-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb12-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb12-2">schema_table_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb12-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement"</span></span>
<span id="cb12-4">  )</span>
<span id="cb12-5">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(documentation_logement)</span>
<span id="cb12-6"></span>
<span id="cb12-7">schema_table_individu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb12-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv"</span></span>
<span id="cb12-9">  )</span>
<span id="cb12-10">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(schema_table_individu)</span>
<span id="cb12-11"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-3-2" class="tab-pane" aria-labelledby="tabset-3-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1">schema_table_individu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duckdb.sql(</span>
<span id="cb13-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv"</span></span>
<span id="cb13-3">  ).to_df()</span>
<span id="cb13-4">display(schema_table_individu.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb13-5"></span>
<span id="cb13-6">schema_table_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duckdb.sql(</span>
<span id="cb13-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement"</span></span>
<span id="cb13-8">  ).to_df()</span>
<span id="cb13-9">display(schema_table_logement.head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div></div>
</div>
<div id="tabset-3-3" class="tab-pane" aria-labelledby="tabset-3-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">schema_table_individu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb14-2">  con,</span>
<span id="cb14-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv"</span></span>
<span id="cb14-4">)</span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(schema_table_individu))</span>
<span id="cb14-6"></span>
<span id="cb14-7">schema_table_logement <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb14-8">  con,</span>
<span id="cb14-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement"</span></span>
<span id="cb14-10">)</span>
<span id="cb14-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(schema_table_logement))</span></code></pre></div></div>
</div>
<div id="tabset-3-4" class="tab-pane" aria-labelledby="tabset-3-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"></span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(documentation_indiv))</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(documentation_logement))</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb16" data-startfrom="260" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 259;"><span id="cb16-260">schema_table_individu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb16-261">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv"</span></span>
<span id="cb16-262">  )</span>
<span id="cb16-263">schema_table_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb16-264">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement"</span></span>
<span id="cb16-265">  )</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-5-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-5-2" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<p>Voici le dictionnaire des variables de la table logement:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb17" data-startfrom="266" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 265;"><span id="cb17-266">viewof search_logement <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">search</span>(schema_table_logement<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {</span>
<span id="cb17-267">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">placeholder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rechercher une information dans la table logement"</span></span>
<span id="cb17-268">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-6" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb18" data-startfrom="273" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 272;"><span id="cb18-273">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(search_logement)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-7" data-nodetype="expression">

</div>
</div>
</div>
<p>Et voici celui de la table individus:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb19" data-startfrom="280" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 279;"><span id="cb19-280">viewof search_individus <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">search</span>(schema_table_individu<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {</span>
<span id="cb19-281">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">placeholder</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rechercher une information dans la table individus"</span></span>
<span id="cb19-282">})</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-8" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb20" data-startfrom="316" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 315;"><span id="cb20-316">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(search_individus)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-9" data-nodetype="expression">

</div>
</div>
</div>
<p>Pour découvrir les informations présentes dans la base, il est possible d’utiliser les fonctions pré-implémentées de <code>DuckDB</code> pour la <a href="https://duckdb.org/docs/sql/functions/char.html">manipulation de données textuelles</a>. Par exemple, pour extraire toutes les modalités des variables dont la description contient le terme <em>“catégorie”</em> :</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-4-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-1" aria-controls="tabset-4-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-4-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-2" aria-controls="tabset-4-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-4-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-3" aria-controls="tabset-4-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-4-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-4-4" aria-controls="tabset-4-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-4-1" class="tab-pane active" aria-labelledby="tabset-4-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb21-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb21-2">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement WHERE CONTAINS(description_variable, 'Catégorie')"</span></span>
<span id="cb21-3">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb21-4">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(query)</span>
<span id="cb21-5">)</span>
<span id="cb21-6"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-4-2" class="tab-pane" aria-labelledby="tabset-4-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement WHERE CONTAINS(description_variable, 'Catégorie')"</span></span>
<span id="cb22-2">duckdb.sql(query)</span></code></pre></div></div>
</div>
<div id="tabset-4-3" class="tab-pane" aria-labelledby="tabset-4-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement WHERE CONTAINS(description_variable, 'Catégorie')"</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span></code></pre></div></div>
</div>
<div id="tabset-4-4" class="tab-pane" aria-labelledby="tabset-4-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">documentation_logement <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(description_variable, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"catégorie"</span>))</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb25" data-startfrom="351" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 350;"><span id="cb25-351">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_logement WHERE CONTAINS(description_variable, 'Catégorie')"</span></span>
<span id="cb25-352">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb25-353">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(query)</span>
<span id="cb25-354">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-10-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-10-2" data-nodetype="expression">

</div>
</div>
</div>
</div>
<p>Cette approche peut permettre de récupérer les modalités d’une variable. Dans cette base de données, les valeurs <code>Z</code> sont à part. Il est possible d’avoir du détail sur celles-ci avec la requête suivante :</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-5-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-1" aria-controls="tabset-5-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-5-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-2" aria-controls="tabset-5-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-5-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-3" aria-controls="tabset-5-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-5-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-5-4" aria-controls="tabset-5-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-5-1" class="tab-pane active" aria-labelledby="tabset-5-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb26-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb26-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb26-3">  db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb26-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv WHERE CONTAINS(code_modalite, 'Z')"</span></span>
<span id="cb26-5">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb26-6">  {</span>
<span id="cb26-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">columns</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb26-8">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nom_variable"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code_modalite"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb26-9">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description_modalite"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description_variable"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb26-10">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type_variable"</span></span>
<span id="cb26-11">      ]</span>
<span id="cb26-12">  }</span>
<span id="cb26-13">)</span>
<span id="cb26-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-5-2" class="tab-pane" aria-labelledby="tabset-5-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1">duckdb.sql(</span>
<span id="cb27-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv WHERE CONTAINS(code_modalite, 'Z')"</span></span>
<span id="cb27-3">)</span></code></pre></div></div>
</div>
<div id="tabset-5-3" class="tab-pane" aria-labelledby="tabset-5-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv WHERE CONTAINS(code_modalite, 'Z')"</span></span>
<span id="cb28-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span></code></pre></div></div>
</div>
<div id="tabset-5-4" class="tab-pane" aria-labelledby="tabset-5-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">documentation_indiv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb29-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(code_modalite, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z"</span>))</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb30" data-startfrom="397" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 396;"><span id="cb30-397">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb30-398">  db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb30-399">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM documentation_indiv WHERE CONTAINS(code_modalite, 'Z')"</span></span>
<span id="cb30-400">  )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb30-401">  {</span>
<span id="cb30-402">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">columns</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb30-403">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nom_variable"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"code_modalite"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb30-404">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description_modalite"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description_variable"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb30-405">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type_variable"</span></span>
<span id="cb30-406">      ]</span>
<span id="cb30-407">  }</span>
<span id="cb30-408">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-11" data-nodetype="expression">

</div>
</div>
</div>
</section>
<section id="lecture-et-affichage-de-quelques-valeurs" class="level1">
<h1>Lecture et affichage de quelques valeurs</h1>
<p>Pour visualiser un nombre limité de valeurs, par exemple 5, deux approches sont possibles :</p>
<ul>
<li>Sélectionner un échantillon restreint sur les premières lignes du <code>Parquet</code>, par exemple les 5 premières lignes ;</li>
<li>Sélectionner un échantillon aléatoire.</li>
</ul>
<p>Pour les premières lignes, la commande à utiliser est <code>LIMIT</code>.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-6-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-1" aria-controls="tabset-6-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-6-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-2" aria-controls="tabset-6-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-6-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-3" aria-controls="tabset-6-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-6-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-6-4" aria-controls="tabset-6-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-6-1" class="tab-pane active" aria-labelledby="tabset-6-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb31-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb31-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb31-3">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement LIMIT 5"</span>)</span>
<span id="cb31-4">)</span>
<span id="cb31-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-6-2" class="tab-pane" aria-labelledby="tabset-6-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb32-1">duckdb.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement LIMIT 5"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-6-3" class="tab-pane" aria-labelledby="tabset-6-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb33-2">  con,</span>
<span id="cb33-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement LIMIT 5"</span></span>
<span id="cb33-4">)</span></code></pre></div></div>
</div>
<div id="tabset-6-4" class="tab-pane" aria-labelledby="tabset-6-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1">table_logement <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb35" data-startfrom="406" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 405;"><span id="cb35-406">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb35-407">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement LIMIT 5"</span>)</span>
<span id="cb35-408">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-12" data-nodetype="expression">

</div>
</div>
</div>
<p>Pour un échantillon aléatoire, la commande à utiliser est <code>USING SAMPLE</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb36" data-startfrom="435" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 434;"><span id="cb36-435">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb36-436">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement USIN SAMPLE 5"</span>)</span>
<span id="cb36-437">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-13" data-nodetype="expression">

</div>
</div>
</div>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-7-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-1" aria-controls="tabset-7-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-7-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-2" aria-controls="tabset-7-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-7-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-3" aria-controls="tabset-7-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-7-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-7-4" aria-controls="tabset-7-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-7-1" class="tab-pane active" aria-labelledby="tabset-7-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb37-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb37-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb37-3">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement USING SAMPLE 5"</span>)</span>
<span id="cb37-4">)</span>
<span id="cb37-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-7-2" class="tab-pane" aria-labelledby="tabset-7-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1">duckdb.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement USING SAMPLE 5"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-7-3" class="tab-pane" aria-labelledby="tabset-7-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb39-2">  con,</span>
<span id="cb39-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement USING SAMPLE 5"</span></span>
<span id="cb39-4">)</span></code></pre></div></div>
</div>
<div id="tabset-7-4" class="tab-pane" aria-labelledby="tabset-7-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sql</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement USING SAMPLE 5"</span>))</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb41" data-startfrom="470" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 469;"><span id="cb41-470">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb41-471">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement USING SAMPLE 5"</span>)</span>
<span id="cb41-472">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-14" data-nodetype="expression">

</div>
</div>
</div>
</section>
<section id="sélectionner-des-observations-ou-des-variables" class="level1">
<h1>Sélectionner des observations ou des variables</h1>
<section id="requêtes-sur-les-colonnes-select" class="level2">
<h2 class="anchored" data-anchor-id="requêtes-sur-les-colonnes-select">Requêtes sur les colonnes (<code>SELECT</code>)</h2>
<p>La liste des colonnes à extraire du fichier peut être renseignée avec la clause <code>SELECT</code>. Celles-ci peuvent être renommées en appliquant au passage la clause <code>AS</code>.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-8-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-1" aria-controls="tabset-8-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-8-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-2" aria-controls="tabset-8-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-8-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-3" aria-controls="tabset-8-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-8-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-8-4" aria-controls="tabset-8-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-8-1" class="tab-pane active" aria-labelledby="tabset-8-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb42-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb42-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb42-3">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, AGED, VOIT FROM table_individu LIMIT 10"</span>)</span>
<span id="cb42-4">)</span>
<span id="cb42-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-8-2" class="tab-pane" aria-labelledby="tabset-8-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb43-1">duckdb.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, AGED, VOIT FROM table_individu LIMIT 10"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-8-3" class="tab-pane" aria-labelledby="tabset-8-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb44-2">  con,</span>
<span id="cb44-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, AGED, VOIT FROM table_individu LIMIT 10"</span></span>
<span id="cb44-4">)</span></code></pre></div></div>
</div>
<div id="tabset-8-4" class="tab-pane" aria-labelledby="tabset-8-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1">table_individu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb45-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">poids =</span> IPONDI, AGED, VOIT) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb45-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb46" data-startfrom="502" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 501;"><span id="cb46-502">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb46-503">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, AGED, VOIT FROM table_individu LIMIT 10"</span>)</span>
<span id="cb46-504">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-15" data-nodetype="expression">

</div>
</div>
</div>
<p><code>DuckDB</code> propose également des fonctionnalités pour extraire des colonnes à travers des <a href="https://fr.wikipedia.org/wiki/Expression_r%C3%A9guli%C3%A8re">expressions régulières</a>. De nombreux exemples peuvent être trouvés sur <a href="https://duckdb.org/2023/08/23/even-friendlier-sql.html">cette page</a>.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-9-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-9-1" aria-controls="tabset-9-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-9-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-9-2" aria-controls="tabset-9-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-9-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-9-3" aria-controls="tabset-9-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-9-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-9-4" aria-controls="tabset-9-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-9-1" class="tab-pane active" aria-labelledby="tabset-9-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb47-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb47-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb47-3">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, COLUMNS('.*AGE.*') FROM table_individu LIMIT 10"</span>)</span>
<span id="cb47-4">)</span>
<span id="cb47-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-9-2" class="tab-pane" aria-labelledby="tabset-9-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb48-1">duckdb.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, COLUMNS('.*AGE.*') FROM table_individu LIMIT 10"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-9-3" class="tab-pane" aria-labelledby="tabset-9-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb49-2">  con,</span>
<span id="cb49-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, COLUMNS('.*AGE.*') FROM table_individu LIMIT 10"</span></span>
<span id="cb49-4">)</span></code></pre></div></div>
</div>
<div id="tabset-9-4" class="tab-pane" aria-labelledby="tabset-9-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1">table_individu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb50-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">poids =</span> IPONDI, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGE"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb50-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb51" data-startfrom="540" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 539;"><span id="cb51-540">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb51-541">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT IPONDI AS poids, COLUMNS('.*AGE.*') FROM table_individu LIMIT 10"</span>)</span>
<span id="cb51-542">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-16" data-nodetype="expression">

</div>
</div>
</div>
</section>
<section id="requêtes-sur-les-lignes-where" class="level2">
<h2 class="anchored" data-anchor-id="requêtes-sur-les-lignes-where">Requêtes sur les lignes (<code>WHERE</code>)</h2>
<p>Pour extraire un sous-échantillon des données complètes, la clause <code>WHERE</code> permet d’appliquer des filtres à partir de conditions logiques. Par exemple, il est possible de ne conserver, du fichier national, que les données de l’Aude (11), de la Haute-Garonne (31) et de l’Hérault (34).</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-10-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-10-1" aria-controls="tabset-10-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-10-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-10-2" aria-controls="tabset-10-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-10-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-10-3" aria-controls="tabset-10-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-10-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-10-4" aria-controls="tabset-10-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-10-1" class="tab-pane active" aria-labelledby="tabset-10-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb52-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb52-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb52-3">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_individu WHERE DEPT IN ('11', '31', '34') LIMIT 10"</span>)</span>
<span id="cb52-4">)</span>
<span id="cb52-5"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-10-2" class="tab-pane" aria-labelledby="tabset-10-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb53-1">duckdb.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_individu WHERE DEPT IN ('11', '31', '34')"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-10-3" class="tab-pane" aria-labelledby="tabset-10-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb54-2">  con,</span>
<span id="cb54-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_individu WHERE DEPT IN ('11', '31', '34')"</span></span>
<span id="cb54-4">)</span></code></pre></div></div>
</div>
<div id="tabset-10-4" class="tab-pane" aria-labelledby="tabset-10-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1">table_individu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb55-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(DEPT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"11"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb55-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb56" data-startfrom="573" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 572;"><span id="cb56-573">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb56-574">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_individu WHERE DEPT IN ('11', '31', '34') LIMIT 10"</span>)</span>
<span id="cb56-575">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-17" data-nodetype="expression">

</div>
</div>
</div>
<p>Il est également possible de formater cette liste telle qu’attendue par SQL à partir d’une liste <code>Python</code> ou d’un vecteur <code>R</code> plus classique. Pour cela, le code suivant peut servir de modèle :</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-11-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-11-1" aria-controls="tabset-11-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-11-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-11-2" aria-controls="tabset-11-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-11-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-11-3" aria-controls="tabset-11-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-11-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-11-4" aria-controls="tabset-11-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-11-1" class="tab-pane active" aria-labelledby="tabset-11-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb57-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb57-2">liste_regions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"11"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34"</span>]</span>
<span id="cb57-3">liste_regions_sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> liste_regions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(item <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>item<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'`</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)</span>
<span id="cb57-4">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb57-5">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`SELECT * FROM table_individu WHERE DEPT IN (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>liste_regions_sql<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">) LIMIT 10`</span>)</span>
<span id="cb57-6">)</span>
<span id="cb57-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-11-2" class="tab-pane" aria-labelledby="tabset-11-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb58-1">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duckdb.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>()</span>
<span id="cb58-2"></span>
<span id="cb58-3">con.execute(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'''</span></span>
<span id="cb58-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  CREATE OR REPLACE VIEW table_individu</span></span>
<span id="cb58-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  AS SELECT * FROM read_parquet("FD_INDCVI_2020.parquet")</span></span>
<span id="cb58-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'''</span></span>
<span id="cb58-7">)</span>
<span id="cb58-8"></span>
<span id="cb58-9">liste_regions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"11"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34"</span>]</span>
<span id="cb58-10"></span>
<span id="cb58-11">dep_slots <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>.join([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"?"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> liste_regions])</span>
<span id="cb58-12">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_individu WHERE DEPT IN (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(dep_slots)</span>
<span id="cb58-13">liste_regions_sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>.join([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dep<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> dep <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> liste_regions])</span>
<span id="cb58-14">con.execute(query, liste_regions).fetchdf()</span></code></pre></div></div>
</div>
<div id="tabset-11-3" class="tab-pane" aria-labelledby="tabset-11-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1">liste_regions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"11"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34"</span>)</span>
<span id="cb59-2">liste_regions_sql <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql_collapse</span>(</span>
<span id="cb59-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(</span>
<span id="cb59-4">    liste_regions, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(dep) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'{`dep`}'"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con)</span>
<span id="cb59-5">    ),</span>
<span id="cb59-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span></span>
<span id="cb59-7">)</span>
<span id="cb59-8">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(</span>
<span id="cb59-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_individu WHERE DEPT IN ({liste_regions_sql})"</span>,</span>
<span id="cb59-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb59-11">)</span>
<span id="cb59-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span></code></pre></div></div>
</div>
<div id="tabset-11-4" class="tab-pane" aria-labelledby="tabset-11-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1">liste_regions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"11"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34"</span>)</span>
<span id="cb60-2">table_individu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb60-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(DEPT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> liste_regions)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb61" data-startfrom="615" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 614;"><span id="cb61-615">liste_regions <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"11"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"31"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"34"</span>]</span>
<span id="cb61-616">liste_regions_sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> liste_regions<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(item <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>item<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'`</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">join</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)</span>
<span id="cb61-617">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb61-618">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`SELECT * FROM table_individu WHERE DEPT IN (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>liste_regions_sql<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">) LIMIT 10`</span>)</span>
<span id="cb61-619">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-18-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-18-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-18-3" data-nodetype="expression">

</div>
</div>
</div>
</div>
<p>Pour en savoir plus sur les <em>prepared statements</em> avec DuckDB en Python, et plus généralement pour avoir des exemples d’utilisations différentes, c’est <a href="https://duckdb.org/docs/api/python/dbapi.html">ici</a> que ça se passe.</p>
<p><br></p>
<p>Les filtres sur les observations peuvent être faits à partir de critères sur plusieurs colonnes. Par exemple, pour ne conserver que les observations de la ville de Nice où la date d’emménagement est postérieure à 2020, la requête suivante peut être utilisée :</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-12-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-12-1" aria-controls="tabset-12-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-12-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-12-2" aria-controls="tabset-12-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-12-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-12-3" aria-controls="tabset-12-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-12-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-12-4" aria-controls="tabset-12-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-12-1" class="tab-pane active" aria-labelledby="tabset-12-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb62" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb62-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb62-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb62-3">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb62-4">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement WHERE COMMUNE = '06088' and AEMM &gt; 2020"</span></span>
<span id="cb62-5">    )</span>
<span id="cb62-6">)</span>
<span id="cb62-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-12-2" class="tab-pane" aria-labelledby="tabset-12-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb63" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb63-1">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement WHERE COMMUNE = '06088' and AEMM &gt; 2020"</span></span>
<span id="cb63-2">duckdb.sql(query)</span></code></pre></div></div>
</div>
<div id="tabset-12-3" class="tab-pane" aria-labelledby="tabset-12-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb64" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(</span>
<span id="cb64-2">  con,</span>
<span id="cb64-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement WHERE COMMUNE = '06088' and AEMM &gt; 2020"</span></span>
<span id="cb64-4">)</span></code></pre></div></div>
</div>
<div id="tabset-12-4" class="tab-pane" aria-labelledby="tabset-12-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb65" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1">table_logement <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb65-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(COMMUNE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"06088"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb65-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(AEMM <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2020</span>)</span>
<span id="cb65-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Peut aussi s'écrire en une fois :</span></span>
<span id="cb65-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># table_logement %&gt;% filter(COMMUNE == "06088", AEMM &gt; 2020)</span></span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb66" data-startfrom="669" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 668;"><span id="cb66-669">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb66-670">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb66-671">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM table_logement WHERE COMMUNE = '06088' and AEMM &gt; 2020"</span></span>
<span id="cb66-672">    )</span>
<span id="cb66-673">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-19" data-nodetype="expression">

</div>
</div>
</div>
</section>
</section>
<section id="statistiques-agrégées" class="level1">
<h1>Statistiques agrégées</h1>
<p>Le langage <code>SQL</code> permet d’exécuter de manière très efficace des requêtes complexes afin de construire, à partir de données fines, des statistiques agrégées.</p>
<p>Cette partie illustre d’abord ceci avec deux exemples de statistiques agrégées renvoyant une unique statistique :</p>
<ul>
<li>Extraire la liste des codes arrondissements de Paris, Lyon, Marseille où au moins une personne a été recensée ;</li>
<li>Reproduire l’exemple de <span class="citation" data-cites="mauviere-2022">Mauvière (2022)</span> permettant de calculer le nombre d’habitants de Toulouse qui ont changé de logement en un an ;</li>
</ul>
<p>Ensuite, des statistiques plus fines sont construites par le biais d’agrégations par groupe :</p>
<ul>
<li>Calculer le nombre de personnes recensées par cohorte pour les départements de l’Aude (11), de la Haute-Garonne (31) et de l’Hérault (34) ;</li>
<li>Calculer le nombre de centenaires recensés par département.</li>
</ul>
<p>La fonction <code>DISTINCT</code> appliquée à la variable <code>ARM</code> permet d’extraire la liste des codes arrondissements présents dans la base de données.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-13-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-13-1" aria-controls="tabset-13-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-13-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-13-2" aria-controls="tabset-13-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-13-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-13-3" aria-controls="tabset-13-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-13-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-13-4" aria-controls="tabset-13-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-13-1" class="tab-pane active" aria-labelledby="tabset-13-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb67" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb67-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb67-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb67-3">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb67-4">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT DISTINCT(ARM) FROM table_logement WHERE NOT CONTAINS(ARM, 'ZZZZZ') ORDER BY ARM"</span></span>
<span id="cb67-5">    )</span>
<span id="cb67-6">)</span>
<span id="cb67-7"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-13-2" class="tab-pane" aria-labelledby="tabset-13-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb68" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb68-1">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT DISTINCT(ARM) "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+\</span></span>
<span id="cb68-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FROM table_logement "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+\</span></span>
<span id="cb68-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WHERE NOT CONTAINS(ARM, 'ZZZZZ') "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+\</span></span>
<span id="cb68-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ORDER BY ARM"</span></span>
<span id="cb68-5">duckdb.sql(query)</span></code></pre></div></div>
</div>
<div id="tabset-13-3" class="tab-pane" aria-labelledby="tabset-13-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb69" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb69-1">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(</span>
<span id="cb69-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT DISTINCT(ARM) "</span>,</span>
<span id="cb69-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FROM table_logement "</span>,</span>
<span id="cb69-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WHERE NOT CONTAINS(ARM, 'ZZZZZ') "</span>,</span>
<span id="cb69-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ORDER BY ARM"</span>,</span>
<span id="cb69-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb69-7">)</span>
<span id="cb69-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span></code></pre></div></div>
</div>
<div id="tabset-13-4" class="tab-pane" aria-labelledby="tabset-13-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb70" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb70-1">table_logement <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb70-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(ARM, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ZZZZZ"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">negate =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb70-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ARM =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">distinct</span>(ARM)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb70-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(ARM)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb71" data-startfrom="703" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 702;"><span id="cb71-703">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb71-704">    db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb71-705">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT DISTINCT(ARM) FROM table_logement WHERE NOT CONTAINS(ARM, 'ZZZZZ') ORDER BY ARM"</span></span>
<span id="cb71-706">    )</span>
<span id="cb71-707">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-20" data-nodetype="expression">

</div>
</div>
</div>
<p>Il est possible d’extraire des statistiques beaucoup plus raffinées par le biais d’une requête SQL plus complexe. Par exemple pour calculer le nombre d’habitants de Toulouse qui ont changé de logement en un an:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-14-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-14-1" aria-controls="tabset-14-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-14-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-14-2" aria-controls="tabset-14-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-14-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-14-3" aria-controls="tabset-14-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-14-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-14-4" aria-controls="tabset-14-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-14-1" class="tab-pane active" aria-labelledby="tabset-14-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb72" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb72-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb72-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb72-3">  db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb72-4">    <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb72-5"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT CAST(</span></span>
<span id="cb72-6"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      SUM(IPONDL*CAST(INPER AS INT)) AS INT</span></span>
<span id="cb72-7"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    ) AS habitants_toulouse_demenagement</span></span>
<span id="cb72-8"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM table_logement</span></span>
<span id="cb72-9"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    WHERE COMMUNE == '31555' AND IRANM NOT IN ('1', 'Z') AND INPER != 'Y'</span></span>
<span id="cb72-10"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    `</span></span>
<span id="cb72-11">  )</span>
<span id="cb72-12">)</span>
<span id="cb72-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-14-2" class="tab-pane" aria-labelledby="tabset-14-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb73" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb73-1">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb73-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb73-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT CAST(</span></span>
<span id="cb73-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  SUM(IPONDL*CAST(INPER AS INT)) AS INT</span></span>
<span id="cb73-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">) AS habitants_toulouse_demenagement</span></span>
<span id="cb73-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM table_logement</span></span>
<span id="cb73-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">WHERE COMMUNE == '31555' AND IRANM NOT IN ('1', 'Z') AND INPER != 'Y'</span></span>
<span id="cb73-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb73-9">duckdb.sql(query).df()</span></code></pre></div></div>
</div>
<div id="tabset-14-3" class="tab-pane" aria-labelledby="tabset-14-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb74" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb74-1">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb74-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT CAST(SUM(IPONDL*CAST(INPER AS INT)) AS INT) "</span>,</span>
<span id="cb74-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AS habitants_toulouse_demenagement"</span>,</span>
<span id="cb74-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FROM table_logement"</span>,</span>
<span id="cb74-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WHERE COMMUNE == '31555' AND IRANM NOT IN ('1', 'Z') AND INPER != 'Y'"</span>,</span>
<span id="cb74-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>)</span>
<span id="cb74-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span></code></pre></div></div>
</div>
<div id="tabset-14-4" class="tab-pane" aria-labelledby="tabset-14-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb75" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb75-1">table_logement <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb75-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(COMMUNE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'31555'</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>IRANM <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Z'</span>), INPER <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb75-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">INPER =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(INPER)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb75-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">habitants_toulouse_demenagement =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(IPONDL <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> INPER)))</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb76" data-startfrom="758" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 757;"><span id="cb76-758">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb76-759">  db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb76-760">    <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb76-761"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT CAST(</span></span>
<span id="cb76-762"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">      SUM(IPONDL*CAST(INPER AS INT)) AS INT</span></span>
<span id="cb76-763"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    ) AS habitants_toulouse_demenagement</span></span>
<span id="cb76-764"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM table_logement</span></span>
<span id="cb76-765"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    WHERE COMMUNE == '31555' AND IRANM NOT IN ('1', 'Z') AND INPER != 'Y'</span></span>
<span id="cb76-766"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    `</span></span>
<span id="cb76-767">  )</span>
<span id="cb76-768">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-21" data-nodetype="expression">

</div>
</div>
</div>
<p>Pour représenter la pyramide des âges recensés dans ces trois départements, il est possible de procéder en deux étapes</p>
<ul>
<li>Effectuer une agrégation par le biais de <code>DuckDB</code> et transformer ces résultats sous forme de <em>dataframe</em></li>
<li>Utiliser ce <em>dataframe</em> avec un <em>package</em> d’analyse graphique pour représenter la pyramide des âges.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Pour illustrer le parallélisme possible entre les codes <code>R</code> et <code>Python</code>, l’exemple de représentation graphique ci-dessus s’appuie sur le <em>package</em> <code>plotnine</code> - dont la syntaxe reproduit celle du <em>package</em> <code>R</code> <code>ggplot2</code>, plutôt que sur <code>matplotlib</code> ou <code>seaborn</code>.</p>
</div>
</div>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-15-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-15-1" aria-controls="tabset-15-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-15-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-15-2" aria-controls="tabset-15-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-15-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-15-3" aria-controls="tabset-15-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-15-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-15-4" aria-controls="tabset-15-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-15-1" class="tab-pane active" aria-labelledby="tabset-15-1-tab">
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Mise en garde</span>Version de <code>Plot</code> embarquée dans <code>Quarto</code>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Pour les versions de <code>Quarto</code> antérieures à la v1.4, la version embarquée par défaut de la librairie <code>Plot</code> ne propose pas d’interactivité par le biais des <a href="https://observablehq.com/plot/marks/tip"><em>tooltips</em></a>.</p>
<p>Pour utiliser une version compatible, par exemple la <code>0.6.11</code>, faire:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb77" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb77-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb77-2">Plot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">require</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6.11/dist/plot.umd.min.js"</span>)</span>
<span id="cb77-3"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
</div>
</div>
<details>
<summary>
Code pour structurer les données pour la représentation graphique.
</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb78" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb78-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb78-2">pyramide_ages <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb78-3"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb78-4"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT</span></span>
<span id="cb78-5"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  CAST(SUM(IPONDI) AS INT) AS individus,</span></span>
<span id="cb78-6"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  CAST(AGED AS INT) AS AGED,</span></span>
<span id="cb78-7"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  DEPT AS departement</span></span>
<span id="cb78-8"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM table_individu</span></span>
<span id="cb78-9"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  WHERE DEPT IN ('11', '31', '34')</span></span>
<span id="cb78-10"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY AGED, DEPT ORDER BY DEPT, AGED</span></span>
<span id="cb78-11"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb78-12">)</span>
<span id="cb78-13">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(pyramide_ages)</span>
<span id="cb78-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</details>
<details>
<summary>
Code pour produire la représentation graphique.
</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb79" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb79-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb79-2">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb79-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">percent</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb79-6">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">barY</span>(</span>
<span id="cb79-7">        pyramide_ages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-8">        {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGED"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">individus</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'departement'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"departement"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tip</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}</span>
<span id="cb79-9">        )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-10">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleX</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-11">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tickY</span>(</span>
<span id="cb79-12">        pyramide_ages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-13">        {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGED"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">individus</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'departement'</span>}</span>
<span id="cb79-14">        )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-15">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleY</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-16">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axisY</span>({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population (en milliers)"</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb79-17">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axisX</span>({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ticks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ticks</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Âge"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>})</span>
<span id="cb79-18">  ]</span>
<span id="cb79-19">})</span>
<span id="cb79-20"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</details>
</div>
<div id="tabset-15-2" class="tab-pane" aria-labelledby="tabset-15-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb80" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb80-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb80-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> plotnine <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span></span>
<span id="cb80-3"></span>
<span id="cb80-4">pyramide_ages <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duckdb.sql(</span>
<span id="cb80-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb80-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT</span></span>
<span id="cb80-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  SUM(IPONDI) AS individus,</span></span>
<span id="cb80-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  CAST(AGED AS int) AS AGED,</span></span>
<span id="cb80-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  DEPT AS departement</span></span>
<span id="cb80-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM table_individu</span></span>
<span id="cb80-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  WHERE DEPT IN ('11', '31', '34')</span></span>
<span id="cb80-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY AGED, DEPT ORDER BY DEPT, AGED</span></span>
<span id="cb80-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb80-14">).to_df()</span>
<span id="cb80-15"></span>
<span id="cb80-16">(</span>
<span id="cb80-17">    ggplot(pyramide_ages, aes(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGED"</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"individus"</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb80-18">    geom_bar(</span>
<span id="cb80-19">      aes(fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"departement"</span>),</span>
<span id="cb80-20">      stat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, show_legend<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb80-21">    ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb80-22">    geom_vline(xintercept <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>, linetype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb80-23">    facet_wrap(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'departement'</span>, scales <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, nrow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb80-24">    theme_minimal() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb80-25">    labs(y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Individus recensés"</span>, x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Âge"</span>)</span>
<span id="cb80-26">)</span></code></pre></div></div>
</div>
<div id="tabset-15-3" class="tab-pane" aria-labelledby="tabset-15-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb81" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb81-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(labeling)</span>
<span id="cb81-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb81-3"></span>
<span id="cb81-4">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb81-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT SUM(IPONDI) AS individus, AGED, DEPT AS departement"</span>,</span>
<span id="cb81-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FROM table_individu"</span>,</span>
<span id="cb81-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WHERE DEPT IN ('11', '31', '34')"</span>,</span>
<span id="cb81-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GROUP BY AGED, DEPT"</span>,</span>
<span id="cb81-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ORDER BY DEPT, AGED"</span>,</span>
<span id="cb81-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span></span>
<span id="cb81-11">)</span>
<span id="cb81-12">pyramide_ages <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span>
<span id="cb81-13"></span>
<span id="cb81-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pyramide_ages, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> AGED, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> individus)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb81-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> departement), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb81-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb81-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>departement, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb81-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb81-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Individus recensés"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Âge"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-15-4" class="tab-pane" aria-labelledby="tabset-15-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb82" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb82-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(labeling)</span>
<span id="cb82-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb82-3"></span>
<span id="cb82-4">pyramide_ages <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> table_individu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb82-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(DEPT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'11'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'31'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'34'</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb82-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(AGED, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">departement =</span> DEPT) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb82-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">individus =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(IPONDI), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb82-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(departement, AGED) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb82-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span>
<span id="cb82-10"></span>
<span id="cb82-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pyramide_ages, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> AGED, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> individus)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb82-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> departement), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb82-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb82-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>departement, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_y"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb82-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb82-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Individus recensés"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Âge"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb83" data-startfrom="776" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 775;"><span id="cb83-776">pyramide_ages <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb83-777"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb83-778"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT</span></span>
<span id="cb83-779"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  CAST(SUM(IPONDI) AS INT) AS individus,</span></span>
<span id="cb83-780"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  CAST(AGED AS INT) AS AGED,</span></span>
<span id="cb83-781"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  DEPT AS departement</span></span>
<span id="cb83-782"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM table_individu</span></span>
<span id="cb83-783"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  WHERE DEPT IN ('11', '31', '34')</span></span>
<span id="cb83-784"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY AGED, DEPT ORDER BY DEPT, AGED</span></span>
<span id="cb83-785"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb83-786">)</span>
<span id="cb83-787">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(pyramide_ages)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-22-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-22-2" data-nodetype="expression">

</div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb84" data-startfrom="825" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 824;"><span id="cb84-825">Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>({</span>
<span id="cb84-826">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">null</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-827">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">percent</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-828">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">marks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> [</span>
<span id="cb84-829">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">barY</span>(</span>
<span id="cb84-830">        pyramide_ages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-831">        {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGED"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">individus</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'departement'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fill</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"departement"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tip</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span>}</span>
<span id="cb84-832">        )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-833">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleX</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">stroke</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-834">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tickY</span>(</span>
<span id="cb84-835">        pyramide_ages<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-836">        {<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGED"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">=&gt;</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">individus</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'departement'</span>}</span>
<span id="cb84-837">        )<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-838">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ruleY</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-839">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axisY</span>({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population (en milliers)"</span>})<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb84-840">    Plot<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">axisX</span>({<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ticks</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> d3<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ticks</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Âge"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">fontSize</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>})</span>
<span id="cb84-841">  ]</span>
<span id="cb84-842">})</span></code></pre></div></div>
<div id="fig-ojs-cell-23" class="cell-output cell-output-display quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-ojs-cell-23-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div id="ojs-cell-23" data-nodetype="expression">

</div>
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-ojs-cell-23-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Un exemple de représentation graphique produite à partir du recensement de la population
</figcaption>
</figure>
</div>
</div>
<p>Si on s’intéresse plus spécifiquement au nombre de centenaires recensés par département et qu’on désire classer ces derniers par ordre décroissant.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-16-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-16-1" aria-controls="tabset-16-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-16-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-16-2" aria-controls="tabset-16-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-16-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-16-3" aria-controls="tabset-16-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-16-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-16-4" aria-controls="tabset-16-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-16-1" class="tab-pane active" aria-labelledby="tabset-16-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb85" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb85-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb85-2">db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb85-3"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb85-4"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT</span></span>
<span id="cb85-5"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  SUM(IPONDI) AS individus_recenses,</span></span>
<span id="cb85-6"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  DEPT</span></span>
<span id="cb85-7"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM table_individu</span></span>
<span id="cb85-8"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  WHERE AGED &gt;= 100</span></span>
<span id="cb85-9"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY DEPT</span></span>
<span id="cb85-10"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ORDER BY individus_recenses DESC</span></span>
<span id="cb85-11"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb85-12">)</span>
<span id="cb85-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-16-2" class="tab-pane" aria-labelledby="tabset-16-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb86" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb86-1">duckdb.sql(</span>
<span id="cb86-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb86-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT</span></span>
<span id="cb86-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  SUM(IPONDI) AS individus_recenses,</span></span>
<span id="cb86-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  DEPT</span></span>
<span id="cb86-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM table_individu</span></span>
<span id="cb86-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  WHERE AGED &gt;= 100</span></span>
<span id="cb86-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY DEPT</span></span>
<span id="cb86-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ORDER BY individus_recenses DESC</span></span>
<span id="cb86-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb86-11">)</span></code></pre></div></div>
</div>
<div id="tabset-16-3" class="tab-pane" aria-labelledby="tabset-16-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb87" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb87-1">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb87-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT SUM(IPONDI) AS individus_recenses, DEPT"</span>,</span>
<span id="cb87-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FROM table_individu"</span>,</span>
<span id="cb87-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WHERE AGED &gt;= 100"</span>,</span>
<span id="cb87-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GROUP BY DEPT"</span>,</span>
<span id="cb87-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ORDER BY individus_recenses DESC"</span>,</span>
<span id="cb87-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span></span>
<span id="cb87-8">)</span>
<span id="cb87-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span></code></pre></div></div>
</div>
<div id="tabset-16-4" class="tab-pane" aria-labelledby="tabset-16-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb88" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb88-1">table_individu <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb88-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(AGED <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb88-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(DEPT) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb88-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">individus_recenses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(IPONDI)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb88-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(individus_recenses))</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb89" data-startfrom="923" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 922;"><span id="cb89-923">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb89-924">  db<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb89-925">  <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb89-926"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  SELECT</span></span>
<span id="cb89-927"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CAST(SUM(IPONDI) AS INT) AS individus_recenses,</span></span>
<span id="cb89-928"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    DEPT</span></span>
<span id="cb89-929"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  FROM table_individu</span></span>
<span id="cb89-930"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    WHERE AGED &gt;= 100</span></span>
<span id="cb89-931"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  GROUP BY DEPT</span></span>
<span id="cb89-932"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  ORDER BY individus_recenses DESC</span></span>
<span id="cb89-933"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">  `</span></span>
<span id="cb89-934">  )</span>
<span id="cb89-935">)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div id="ojs-cell-24" data-nodetype="expression">

</div>
</div>
</div>
</section>
<section id="associer-à-dautres-sources-de-données" class="level1">
<h1>Associer à d’autres sources de données</h1>
<p>Le <a href="https://www.insee.fr/fr/information/6800675"><em>code officiel géographique</em> (COG)</a> est utile pour illuster l’ajout d’information annexe. Le code commune va être utilisé pour associer les deux bases de données. Cette variable porte des noms différents dans les deux bases, ce qui n’est pas un problème.</p>
<p>Il est proposé, ci-dessous, de télécharger les données de manière reproductible, via une fonction adaptée (ici à travers le <em>package</em> <code>requests</code> pour <code>Python</code> ou via <code>download.file</code> en <code>R</code>). Bien que <code>DuckDB</code> permette l’import direct depuis un <em>url</em>, ceci implique l’installation en amont de l’<a href="https://duckdb.org/docs/extensions/httpfs.html">extension <code>httpfs</code></a>.</p>
<p>L’association de sources de données passe généralement par un <code>JOIN</code>. Pour illustrer cette clause, il est possible d’associer les agrégats de la table logement à un niveau communal avec celles du COG grâce au code commune.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-17-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-17-1" aria-controls="tabset-17-1" aria-selected="true" href=""><code>Observable</code> via <code>Quarto</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-17-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-17-2" aria-controls="tabset-17-2" aria-selected="false" href=""><code>Python</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-17-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-17-3" aria-controls="tabset-17-3" aria-selected="false" href=""><code>R</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-17-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-17-4" aria-controls="tabset-17-4" aria-selected="false" href=""><code>R (dbplyr)</code></a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-17-1" class="tab-pane active" aria-labelledby="tabset-17-1-tab">
<p>La lecture directe depuis <code>Observable</code> du fichier par le biais du protocole <code>https</code> vers le site de l’Insee ne fonctionnant pas, il est recommandé de télécharger en amont le fichier par l’intermédiaire de <code>Python</code> ou de <code>R</code>.</p>
<details>
<summary>
Code <code>Python</code> pour récupérer le code officiel géographique
</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb90" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb90-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> requests</span>
<span id="cb90-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb90-3"></span>
<span id="cb90-4">url_cog <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.insee.fr/fr/statistiques/fichier/6800675/v_commune_2023.csv"</span></span>
<span id="cb90-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> os.path.exists(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cog.csv"</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>:</span>
<span id="cb90-6">  response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> requests.get(url_cog)</span>
<span id="cb90-7">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cog.csv"</span>, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wb"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb90-8">      <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>.write(response.content)</span></code></pre></div></div>
</details>
<details>
<summary>
Code <code>R</code> pour récupérer le code officiel géographique
</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb91" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb91-1">url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.insee.fr/fr/statistiques/fichier/6800675/v_commune_2023.csv"</span></span>
<span id="cb91-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(url, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cog.csv"</span>)</span></code></pre></div></div>
</details>
<p>Après avoir récupérées les données, ce code peut permettre de créer une base de données intégrant le code officiel géographique:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb92" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb92-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb92-2">db2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb92-3">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> configuredClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb92-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb92-5">    <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb92-6"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW cog2023 AS</span></span>
<span id="cb92-7"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT * FROM read_csv_auto('cog.csv', header = true)</span></span>
<span id="cb92-8"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    `</span></span>
<span id="cb92-9">    )</span>
<span id="cb92-10"></span>
<span id="cb92-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> configuredClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb92-12"></span>
<span id="cb92-13">}</span>
<span id="cb92-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb93" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb93-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{ojs}</span></span>
<span id="cb93-2">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb93-3">  db2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb93-4">    <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb93-5"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT cog2023.NCCENR, CAST(SUM(table_logement.IPONDL) AS INT) AS recenses</span></span>
<span id="cb93-6"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM table_logement</span></span>
<span id="cb93-7"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    LEFT OUTER JOIN cog2023 ON table_logement.COMMUNE = cog2023.COM</span></span>
<span id="cb93-8"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    GROUP BY cog2023.NCCENR</span></span>
<span id="cb93-9"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    ORDER BY recenses;</span></span>
<span id="cb93-10"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    `</span></span>
<span id="cb93-11">  )  </span>
<span id="cb93-12">)</span>
<span id="cb93-13"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</div>
<div id="tabset-17-2" class="tab-pane" aria-labelledby="tabset-17-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb94" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb94-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> requests</span>
<span id="cb94-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb94-3"></span>
<span id="cb94-4">url_cog <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.insee.fr/fr/statistiques/fichier/6800675/v_commune_2023.csv"</span></span>
<span id="cb94-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> os.path.exists(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cog.csv"</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>:</span>
<span id="cb94-6">  response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> requests.get(url_cog)</span>
<span id="cb94-7">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cog.csv"</span>, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wb"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb94-8">      <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>.write(response.content)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb95" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb95-1">duckdb.sql(</span>
<span id="cb95-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CREATE OR REPLACE VIEW cog2023 AS '</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+\</span></span>
<span id="cb95-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SELECT * FROM read_csv_auto("cog.csv", header=true)'</span></span>
<span id="cb95-4">)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb96" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb96-1">duckdb.sql(</span>
<span id="cb96-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb96-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT cog2023.NCCENR, CAST(SUM(table_logement.IPONDL) AS INT) AS recenses</span></span>
<span id="cb96-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM table_logement</span></span>
<span id="cb96-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">LEFT OUTER JOIN cog2023 ON table_logement.COMMUNE = cog2023.COM</span></span>
<span id="cb96-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">GROUP BY cog2023.NCCENR</span></span>
<span id="cb96-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ORDER BY recenses;</span></span>
<span id="cb96-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb96-9">)</span></code></pre></div></div>
</div>
<div id="tabset-17-3" class="tab-pane" aria-labelledby="tabset-17-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb97" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb97-1">url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.insee.fr/fr/statistiques/fichier/6800675/v_commune_2023.csv"</span></span>
<span id="cb97-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(url, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cog.csv"</span>)</span>
<span id="cb97-3"></span>
<span id="cb97-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbExecute</span>(</span>
<span id="cb97-5">  con,</span>
<span id="cb97-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue_sql</span>(</span>
<span id="cb97-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CREATE OR REPLACE VIEW cog2023 AS "</span>,</span>
<span id="cb97-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT * FROM read_csv_auto("</span>cog.csv<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", header=true)"</span>,</span>
<span id="cb97-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.con=</span>con</span>
<span id="cb97-10">  )</span>
<span id="cb97-11">)</span>
<span id="cb97-12"></span>
<span id="cb97-13">query <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb97-14">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT cog2023.NCCENR, CAST(SUM(table_logement.IPONDL) AS INT) AS recenses"</span>,</span>
<span id="cb97-15">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FROM table_logement"</span>,</span>
<span id="cb97-16">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LEFT OUTER JOIN cog2023 ON table_logement.COMMUNE = cog2023.COM"</span>,</span>
<span id="cb97-17">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GROUP BY cog2023.NCCENR ORDER BY recenses"</span>,</span>
<span id="cb97-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span></span>
<span id="cb97-19">)</span>
<span id="cb97-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, query)</span></code></pre></div></div>
</div>
<div id="tabset-17-4" class="tab-pane" aria-labelledby="tabset-17-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb98" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb98-1">url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.insee.fr/fr/statistiques/fichier/6800675/v_commune_2023.csv"</span></span>
<span id="cb98-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(url, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cog.csv"</span>)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb99" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb99-1">cog <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl</span>(con, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'read_csv_auto("{path_data}/cog.csv", header = true)'</span>))</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb100" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb100-1">table_logement <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb100-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(cog, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COMMUNE"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COM"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb100-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(NCCENR) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb100-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">recenses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(IPONDL)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb100-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(recenses)</span></code></pre></div></div>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code hidden" id="cb101" data-startfrom="944" data-source-offset="-0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 943;"><span id="cb101-944">proxy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://corsproxy.io/?"</span></span>
<span id="cb101-945">url_cog <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://www.insee.fr/fr/statistiques/fichier/6800675/v_commune_2023.csv"</span></span>
<span id="cb101-946">db2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb101-947">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">const</span> configuredClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> db <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb101-948">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">await</span> configuredClient<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb101-949">    <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb101-950"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    CREATE OR REPLACE VIEW cog2023 AS</span></span>
<span id="cb101-951"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT * FROM read_csv_auto('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">${</span>proxy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> url_cog<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">', header = true)</span></span>
<span id="cb101-952"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    `</span></span>
<span id="cb101-953">    )</span>
<span id="cb101-954"></span>
<span id="cb101-955">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> configuredClient <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb101-956"></span>
<span id="cb101-957">}</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-25-1" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-25-2" data-nodetype="declaration">

</div>
</div>
</div>
<div class="cell-output cell-output-display">
<div>
<div id="ojs-cell-25-3" data-nodetype="declaration">

</div>
</div>
</div>
</div>
<p>Les données du code officiel géographique présentent l’aspect suivant :</p>
<div class="cell">
<details class="code-fold">
<summary>Requête Observable pour explorer les premières lignes</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb102" data-startfrom="956" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript" style="counter-reset: source-line 955;"><span id="cb102-956">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb102-957">  db2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`SELECT * FROM cog2023 LIMIT 10`</span>)</span>
<span id="cb102-958">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="ojs-cell-26" data-nodetype="expression">

</div>
</div>
</div>
<p>Voici par exemple le résultat d’une fusion avec une agrégation produite à la volée sur la table logement :</p>
<div class="cell">
<details class="code-fold">
<summary>Requête pour explorer les premières lignes</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb103" data-startfrom="1" data-source-offset="0" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb103-1">Inputs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(</span>
<span id="cb103-2">  db2<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">query</span>(</span>
<span id="cb103-3">    <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span>
<span id="cb103-4"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT cog2023.NCCENR, CAST(SUM(table_logement.IPONDL) AS INT) AS recenses</span></span>
<span id="cb103-5"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM table_logement</span></span>
<span id="cb103-6"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    LEFT OUTER JOIN cog2023 ON table_logement.COMMUNE = cog2023.COM</span></span>
<span id="cb103-7"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    GROUP BY cog2023.NCCENR</span></span>
<span id="cb103-8"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    ORDER BY recenses;</span></span>
<span id="cb103-9"><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">    `</span></span>
<span id="cb103-10">  )  </span>
<span id="cb103-11">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="ojs-cell-27" data-nodetype="expression">

</div>
</div>
</div>
<section id="références" class="level2">




</section>
</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">Références</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-dondon-lamarche-2023" class="csl-entry">
Dondon, Alexis, et Pierre Lamarche. 2023. <span>«&nbsp;Quels formats pour quelles données?&nbsp;»</span> <em>Courrier des statistiques</em>, nᵒ 9.
</div>
<div id="ref-mauviere-2022" class="csl-entry">
Mauvière, Éric. 2022. <span>«&nbsp;Parquet devrait remplacer le format CSV&nbsp;»</span>. Post de blog [Consulté le 12 octobre 2023]. <a href="https://www.icem7.fr/cartographie/parquet-devrait-remplacer-le-format-csv/">https://www.icem7.fr/cartographie/parquet-devrait-remplacer-le-format-csv/</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Notes de bas de page</h2>

<ol>
<li id="fn1"><p>Des propositions d’enrichissements de cette documentation à partir d’implémentations alternatives, par exemple s’appuyant sur <code>Arrow</code> et <code>dbplyr</code> ou sur <code>Polars</code> sont bienvenues sur le <a href="https://github.com/InseeFrLab/exemples-recensement-parquet">Github InseeFrLab/exemples-recensement-parquet</a>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Python</category>
  <category>R</category>
  <category>Parquet</category>
  <guid>https://ssphub.netlify.app/post/parquetRP/</guid>
  <pubDate>Mon, 23 Oct 2023 00:00:00 GMT</pubDate>
  <media:content url="https://ssphub.netlify.app/post/parquetRP/ducks.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Onyxia: l’infrastructure cloud mère des dragons</title>
  <link>https://ssphub.netlify.app/post/onyxia/</link>
  <description><![CDATA[ 





<p><code>Onyxia</code> est un logiciel <em>open source</em> développé par l’Insee (<a href="https://github.com/InseeFrLab/onyxia-web">disponible sur <code>Github</code> <i class="fa-brands fa-github"></i></a>) permettant de fournir un environnement de traitement de données à l’état de l’art. Principalement conçu pour permettre le travail interactif des data scientists, l’expérience fournie avec <code>Onyxia</code> favorise également la reproductibilité des travaux et leur mise en production.</p>
<p>Le logiciel <code>Onyxia</code> est installé par des organisations souhaitant créer un <em>datalab</em>, c’est-à-dire une plateforme interactive de traitement de données. Ces organisations ont toutes le point commun de vouloir construire une plateforme qui embrasse les technologies <em>cloud</em> que sont la conteneurisation et le stockage objet tout en mettant à disposition celles-ci dans un environnement <em>user-friendly</em> où l’interconnexion entre ces différentes briques est gérée de manière cohérente. Les technologies <em>cloud native</em> sont devenues indispensables dans l’écosystème de la donnée, du fait d’une meilleure gestion des ressources de traitement ou de la capacité à créer un environnement parfaitement reproductible pour une mise en production accélérée.</p>
<p>Ce <em>post</em> de blog a pour objectif de présenter la raison d’être d’<code>Onyxia</code>, sa génèse et les solutions qu’apporte cette infrastructure à des irritants classiques des projets novateurs de data science.</p>
<section id="contexte" class="level2">
<h2 class="anchored" data-anchor-id="contexte">Contexte</h2>
<p>L’écosystème de la data science est en mouvement accéléré depuis 10 ans et le rôle du <em>data scientist</em> dans les organisations valorisant de la donnée évolue continuellement <span class="citation" data-cites="davenport2022data">(Davenport et Patil 2022)</span>. Les data scientists modernes sont amenés à utiliser de plus en plus de langages et doivent être capables de maîtriser plusieurs architectures informatiques. La frontière est ainsi moins nette que par le passé entre statisticiens et informaticiens. De plus, les innovations récentes dans le monde du développement logiciel, notamment l’adoption massive de l’approche <code>DevOps</code> - approche qui consiste à automatiser la production de livrables dès la conception du prototype - a également fait évoluer les pratiques des data scientists.</p>
<p>Ce besoin de ressources informatiques croissantes, de flexibilité dans le prototypage de solutions informatiques et l’évolution des pratiques consistant à mettre à disposition en continu des livrables ont eu des implications importantes sur les architectures informatiques dominantes dans l’écosystème de la donnée. Pour répondre au besoin croissant de puissance de traitement, les serveurs partagés, organisés sous forme de <em>clusters</em>, se sont développés dans de nombreuses organisations. Après avoir connue son heure de gloire au début des années 2010, l’infrastructure <a href="https://openclassrooms.com/fr/courses/4467481-creez-votre-data-lake/4509426-decouvrez-le-systeme-de-fichiers-distribue-hdfs"><code>HDFS</code> (<em>Hadoop Distributed File System</em>)</a>, qui reposait sur des <em>clusters</em> où les données et la puissance de traitement étaient distribuées et collocalisées, a laissé place à des infrastructures plus <em>scalables</em>, basées sur l’approche de la conteneurisation.</p>
</section>
<section id="de-hdfs-à-la-conteneurisation" class="level2">
<h2 class="anchored" data-anchor-id="de-hdfs-à-la-conteneurisation">De HDFS à la conteneurisation</h2>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Cette partie plus technique développe des éléments pour comprendre le succès récent des infrastructures conteneurisées.</p>
<p>Elle pourra intéresser le lecteur curieux sur les fondements des infrastructures <em>cloud</em> modernes mais n’est pas nécessaire à la compréhension générale de l’article.</p>
</div>
</div>
<p>La conteneurisation, qui repose sur l’idée que les serveurs de stockage de la donnée peuvent être dissociés de ceux effectuant les traitements, sert de fondement aux principales plateformes <em>cloud</em> actuelles fournissant des services à la demande.</p>
<p>Ce nouveau paradigme part de deux constats. Le premier est que les échanges de données entre les noeuds d’un serveur sont aujourd’hui peu coûteux. Avec des flux réseaux suffisants et une technologie performante, il est donc possible d’échanger à un coût modéré de gros volumes de données au sein d’une infrastructure. Le deuxième constat est que la maintenance d’une infrastructure conteneurisée, faite pour être très malléable, est plus légère que celle d’une infrastructure basée sur des machines virtuelles ou sur les infrastructrures calibrées pour l’analytique <em>big data</em> comme <code>HDFS</code> reposant sur la collocalisation des données et des traitements<sup>1</sup>.</p>
<p>Les données étant stockées sur des serveurs différents de ceux exécutant les traitements, l’accès à celles-ci se fait à travers des API qui permettent de traiter le système de stockage distant comme un système de fichiers classique. <code>Onyxia</code> a adopté une implémentation <em>open source</em> du système de stockage <a href="https://fr.wikipedia.org/wiki/Amazon_S3"><code>S3</code></a> appelée <a href="https://min.io/"><code>MinIO</code></a>.</p>
<p>En ce qui concerne le traitement des données, le fait d’utiliser un système de conteneurs, c’est-à-dire une configuration logicielle portable minimaliste prête à l’emploi (par opposition aux machines virtuelles qui impliquent un système d’exploitation complet), offre une grande liberté sur le choix des logiciels de traitement. De nombreuses technologies <em>open source</em> devenues standards dans le monde de la <em>data science</em> (<code>Jupyter</code>, <code>RStudio</code>, <code>ElasticSearch</code>…) existent déjà sous cette forme et peuvent ainsi être adoptées dans une telle infrastructure pour fournir des services prêts-à-l’emploi pour les data scientists. La mise en musique de toutes ces petites boites auto-suffisantes, notamment l’optimisation des ressources concurrentes sur un serveur, est permise par la technologie d’orchestration <a href="https://kubernetes.io/fr/"><code>Kubernetes</code></a>.</p>
<p><br></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/onyxia/onyxia_logic.svg" class="img-fluid figure-img"></p>
<figcaption><em>Centralisation des ressources par <code>Onyxia</code></em></figcaption>
</figure>
</div>
<p><br></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Plus de détails pour comprendre le changement de paradigme vers la conteuneurisation 👇
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Les infrastructures <em>big data</em> reposent sur le principe du <em>cluster</em> (grappe) informatique. Des serveurs sont connectés entre eux, ce qui forme de manière imagée une grappe. Cette interconnexion de plusieurs serveurs entre eux peut se faire au niveau :</p>
<ul>
<li>du <strong>stockage</strong> : les données volumineuses ne sont pas stockées sur un seul serveur mais au contraire réparties ;</li>
<li>du <strong>traitement</strong> : les calculs sont effectués par blocs sur plusieurs serveurs et le résultat de ceux-ci est ensuite transmis à un serveur maître.</li>
</ul>
<p>Le système <em>Hadoop Distributed File System</em> a été pensé pour tirer parti de l’algorithme de traitement parallélisé <a href="https://fr.wikipedia.org/wiki/MapReduce"><code>MapReduce</code></a> proposé en 2004 par <code>Google</code>. Les fichiers volumineux sont fractionnés et répartis sur plusieurs serveurs.</p>
<p><img src="https://i0.wp.com/datascientest.com/wp-content/uploads/2021/04/illu_schema_mapreduce-04.png?w=1024&amp;ssl=1" class="img-fluid"></p>
<p><em>Fonctionnement d’une architecture <code>MapReduce</code> (source: <a href="https://datascientest.com/mapreduce">Datascientest</a>)</em></p>
<p>La spécificité de l’architecture <code>HDFS</code> est que non seulement le stockage est distribué mais également aussi la puissance de traitement associée. On parle à ce propos de <strong>collocalisation</strong> : les traitements ont lieu sur les mêmes serveurs que ceux où sont stockés les données. Cela permet de réduire les mouvements de données (<em>shuffle</em> dans l’image ci-dessus) qui sont coûteux du point de vue de la performance. Cette collocalisation a permis au système <code>HDFS</code> de devenir, au début de la décennie 2010, le paradigme dominant. En tirant parti de la parallélisation permise par des langages très efficaces comme <code>Spark</code> tout en limitant les échanges réseaux pouvant faire perdre en performance, cette architecture a attiré au-delà de l’écosystème du <em>big data</em>.</p>
<p>Le système HDFS présente néanmoins certaines limites qui expliquent sa perte de succès avec l’émergence d’un nouveau paradigme plus flexible.</p>
<p>En premier lieu, ce système nécessite beaucoup de ressources du fait de son <em>design</em>. Comme les traitements sont lourds et partagés pour des usages concurrents, les noeuds constituant le <em>cluster</em> peuvent subir des arrêts à cause de surcharge des ressources. Pour tenir compte de la nature instable de cette infrastructure <em>big data</em>, les fichiers sont dupliqués. Ainsi, lors d’une erreur sur le serveur générant un arrêt du nœud (par exemple à cause de traitements trop gourmands), les traitements sur l’ensemble des données sont sécurisés évitant également la perte partielle ou totale de ces dernières.</p>
<p>L’implication est que les données, déjà volumineuses, sont dupliquées plusieurs fois impliquant des architectures assez monumentales. Si la duplication de la donnée n’est pas en soi choquante afin d’éviter la perte de données, cela a un effet pervers dans un système de collocalisation. A chaque ajout de noeuds pour le stockage de données, il est également nécessaire d’ajouter des ressources pour les traiter. Il est donc compliqué de décorréler l’ajout de ressources de stockage et de traitement. Cette absence de flexibilité est pénalisante dans un monde où les données sont mises à jour fréquemment et où les technologies de traitement, donc les besoins associés, évoluent rapidement. Les infrastructures <code>HDFS</code> sont donc lourdes à changer, que ce soit pour ajouter des ressources ou faire évoluer les distributions logicielles présentes dessus.</p>
<p>Le deuxième facteur qui a favorisé le changement de paradigme est l’amélioration des échanges réseaux. Il n’est plus aussi coûteux que par le passé de transférer des volumes importants de données au sein d’une infrastructure. Cela facilite la décorrélation entre environnement de stockage et de traitement.</p>
<p>Cette séparation des environnements de stockage et de traitement permet alors d’adopter pour chacun les technologies les plus performantes. Dans le domaine du stockage, celle qui a rencontré le plus de succès est le système de stockage <a href="https://aws.amazon.com/fr/s3/"><code>S3</code></a> développé par Amazon. L’implémentation <em>open source</em> du système S3 est <a href="https://min.io/"><code>MinIO</code></a>, utilisée par <code>Onyxia</code>.</p>
<p>Dans le domaine du traitement, la technologie la plus performante dépend de la nature de la tâche réalisée. Selon qu’on désire effectuer de la recherche textuelle, des visualisations de données ou de l’analyse d’image, on ne va pas vouloir utiliser la même technologie. Pour mettre à disposition des logiciels sur un serveur, il existe principalement deux approches concurrentes.</p>
<p>La première repose sur le principe des machines virtuelles. Cette approche n’est pas nouvelle et de nombreuses organisations ont proposé ou proposent encore ce type d’infrastructures pour des serveurs collectifs de traitement. Cette approche est néanmoins lourde : elle nécessite un système d’exploitation complet dont il faudra ensuite adapter la configuration lors de l’installation de chaque logiciel. Plusieurs logiciels coexistent donc dans ce système d’exploitation même si un seul, par exemple, <code>Python</code>, est utilisé. Les machines virtuelles sont des infrastructures assez polluantes puisque pour faire fonctionner un système d’exploitation dans son ensemble, il est nécessaire de mobiliser des ressources plus importantes que celles seulement nécessaires aux traitements. De plus, la configuration d’un système d’exploitation, et notamment, la gestion de la dépendance de multiples logiciels à des configurations systèmes qui peuvent ne pas correspondre, n’est pas triviale. Il est donc lourd de faire évoluer une infrastructure reposant sur des machines virtuelles. L’absence de flexibilité d’une infrastructure reposant sur le principe des machines virtuelles est pénalisante dans un écosystème mouvant comme celui de la <em>data science</em>, où une partie importante du travail de prototypage consiste à tester plusieurs technologies pour déterminer celle s’intégrant le mieux dans un processus de traitement de données.</p>
<p>Le système de la conteneurisation a justement été pensé pour cela : plutôt qu’installer de nombreuses librairies au niveau du système, pour une fraction d’utilisateurs limitée à chacune, il est plus intéressant de créer des environnements complets qui vont exister de manière conjointe. Chaque <em>framework</em> va être construit comme un conteneur autosuffisant avec un système d’exploitation minime et un nombre minimal de couches de configurations supplémentaires. Un <em>framework</em> est livré sous la forme d’une image <a href="https://fr.wikipedia.org/wiki/Docker_(logiciel)"><code>Docker</code></a>, une technologie qui permet d’empaqueter un logiciel et ses dépendances sous la forme de boites minimalistes et les mettre à disposition facilement pour une réutilisation. Il existe par exemple des images <code>Docker</code> pour pouvoir utiliser <code>RStudio</code>, <code>Jupyter</code>, <code>VSCode</code> avec des configurations minimales afin d’exécuter du <code>Python</code> ou du <code>R</code>. A partir de celles-ci, l’utilisateur qui désire des configurations supplémentaires peut ajouter les couches qui lui sont utiles.</p>
<p>Mais les images <code>Docker</code> ne se réduisent pas à la mise à disposition d’environnements de développement. Une partie des technologies les plus appréciées de l’écosystème de la data science sont également livrées sous forme d’images <code>Docker</code>. Par exemple, le moteur de recherche <code>ElasticSearch</code>, très utilisé pour la recherche textuelle, peut être empaqueté dans une image <code>Docker</code>. Le logiciel <code>Onyxia</code> propose dès lors, dans un catalogue vivant, un certain nombre de logiciels très utiles pour les <em>data scientists</em> ayant fait l’objet d’un tel empaquetage. Les nombreuses images <code>Docker</code> servant à créer des services pour les <em>data scientists</em> sont disponibles en <em>open source</em> sur <a href="https://github.com/InseeFrLab/images-datascience"><code>Github</code></a>.</p>
<p>Pour organiser sur un serveur la coexistence de multiples utilisateurs de services gourmands en ressource, la solution <a href="https://kubernetes.io/fr/"><code>Kubernetes</code></a> fait aujourd’hui office de référence. Entre sa création en 2014 et aujourd’hui, cette solution d’orchestration, c’est-à-dire de gestion d’une infrastructure, est devenue incontournable. Outre son allocation dynamique des ressources, elle permet de transformer facilement le livrable d’une chaine de traitement en application disponible en continu. Ceci est particulièrement adapté dans un contexte de diversification des livrables fournis par les <em>data scientists</em> (API, application web, modèle…) et d’adoption d’une démarche <code>DevOps</code> voire <code>MLOps</code>.</p>
</div>
</div>
</div>
</section>
<section id="la-solution-onyxia" class="level2">
<h2 class="anchored" data-anchor-id="la-solution-onyxia">La solution Onyxia</h2>
<section id="dun-cloud-de-ladministration-à-un-logiciel-ouvert" class="level3">
<h3 class="anchored" data-anchor-id="dun-cloud-de-ladministration-à-un-logiciel-ouvert">D’un <em>cloud</em> de l’administration à un logiciel ouvert</h3>
<p>Pour permettre aux <em>data scientists</em> des administrations françaises de bénéficier de technologies <em>cloud</em> sans être dépendant d’un fournisseur de service privé, l’équipe innovation de l’Insee a eu l’idée de créer un <em>datalab</em> basé sur la philosophie de la conteneurisation en mobilisant exclusivement des composants open-source.</p>
<p>Ce <em>datalab</em>, né à l’Insee en 2018, a été ouvert à l’administration publique sous la forme d’une instance https://www.sspcloud.fr/ à condition d’utiliser des données ouvertes. En plus des agents déjà en poste dans l’administration, cette infrastructure sert depuis deux ans à former les élèves de l’ENSAE et de l’ENSAI dans le cadre de leur formation en <em>data science</em>.</p>
<p>Début 2023, ce sont plus de 3000 agents et étudiants qui sont inscrits sur cette infrastructure avec, en moyenne, 300 utilisateurs hebdomadaires. L’infrastructure de traitement propose 10 TB de RAM, 1100 CPU disponibles et 34 GPU. La capacité de stockage associée est de 150 TB.</p>
<p>Pour les utilisations internes de données plus sensibles, l’équipe innovation de l’Insee a rendu disponible le code source derrière le <code>SSP Cloud</code> dans le cadre d’un logiciel nommé <code>Onyxia</code> (https://www.onyxia.sh/). Ce logiciel est pensé comme un kit qui peut être installé sur un <em>cluster</em> <code>Kubernetes</code>, technologie détaillée précédemment.</p>
</section>
</section>
<section id="onyxia-en-bref" class="level2">
<h2 class="anchored" data-anchor-id="onyxia-en-bref">Onyxia en bref</h2>
<p><img src="https://ssphub.netlify.app/post/onyxia/onyxia_ecailles.svg" class="img-fluid"></p>
<p><code>Onyxia</code> propose principalement deux composants de valeur :</p>
<ul>
<li>une <strong>interface web</strong> qui agit comme la porte d’entrée du <em>data scientist</em> sur son datalab, lui facilitant l’accès aux technologies cloud et lui permettant de démarrer ses environnements de traitement de la donnée. L’interface ergonomique permet aux utilisateurs de données néophytes de démarrer des services standardisés sans se préoccuper de la configuration mais aussi aux <em>data scientists</em> plus aguerris de bénéficier de vastes possibilités de personnalisation du service.</li>
<li>des <strong>catalogues de logiciels</strong> : une petite vingtaine de services interactifs dont les plus utilisés sont <code>RStudio</code>, <code>Jupyter</code>, <code>VScode</code>, une quinzaine de services spécialisés dans les bases de données (<code>Postgres</code>, <code>ElasticSearch</code>…), 5 services d’automatisation (<code>MLflow</code>…) et 2 services de <em>dataviz</em> (<code>Redash</code> et <code>Superset</code>)</li>
</ul>
<p><img src="https://ssphub.netlify.app/post/onyxia/catalogue.svg" alt="Le catalogue Onyxia"></p>
<p><em>Le catalogue des services disponibles dans <code>Onyxia</code>.</em></p>
<p><br></p>
<p>Ces deux composants peuvent être adaptés en fonction des besoins internes de chaque organisation. Tous les services interactifs sont automatiquement connectés à l’espace de stockage <code>S3</code>, et au coffre de secret <code>Vault</code>. La gestion des droits d’accès aux données stockées dans l’espace de stockage <code>S3</code> ou dans des services de bases de données (<code>ElasticSearch</code>, <code>PostGreSQL</code>…) est automatisée afin que chaque service puisse accéder aux données sur lesquelles l’utilisateur détient des droits.</p>
<p><code>Onyxia</code> étant un ensemble malléable de logiciels conteneurisés, il est possible de ne pas adopter l’ensemble des services proposés par l’équipe de l’Insee qui maintient <code>Onyxia</code>. Il est également possible de changer certaines des briques de base pour l’adapter à des éléments d’infrastructure interne. Par exemple, il est possible d’adapter la destination du service de stockage ou les configurations des environnements data science pour l’adapter à des ressources.</p>
</section>
<section id="linterface-et-les-services-proposés-par-onyxia" class="level2">
<h2 class="anchored" data-anchor-id="linterface-et-les-services-proposés-par-onyxia">L’interface et les services proposés par <code>Onyxia</code></h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/onyxia/multiple_instances.svg" class="img-fluid figure-img"></p>
<figcaption><em>Onyxia offre des marges de manoeuvre sur l’interface</em></figcaption>
</figure>
</div>
<p>L’une des principales forces d’<code>Onyxia</code> est d’offrir une multiplicité de services différents avec une interconnexion entre eux gérée de manière cohérente.</p>
<p>Les conteneurs sont démarrés comme des services à la demande et la configuration automatique de ceux-ci permet d’assurer aux <em>data scientists</em> l’accès aux données disponibles dans des espaces de stockage ou des bases de données créées par l’utilisateur.</p>
<p>Le catalogue de services se présente par le biais d’un formulaire ergonomique où l’utilisateur choisit la brique qu’il désire utiliser:</p>
<p><img src="https://ssphub.netlify.app/post/onyxia/onyxia_ui1.png" class="img-fluid"></p>
<p>Les <em>data scientists</em> et statisticiens n’ont donc pas besoin de connaître les détails du fonctionnement des briques techniques d’<code>Onyxia</code> pour utiliser la plateforme. Les éléments techniques comme la connexion au système de stockage sont, par défaut, déjà configurés :</p>
<p><img src="https://ssphub.netlify.app/post/onyxia/onyxia_ui2.png" class="img-fluid"></p>
<p>L’interface ergonomique permet de paramétrer certaines configurations si besoin, notamment les ressources à disposition du conteneur. Néanmoins l’allocation dynamique des ressources offre déjà de la flexibilité :</p>
<p><img src="https://ssphub.netlify.app/post/onyxia/onyxia_ui3.png" class="img-fluid"></p>
<p>L’utilisateur a accès à l’ensemble des services qu’il a ouvert depuis une page dédiée :</p>
<p><img src="https://ssphub.netlify.app/post/onyxia/onyxia_ui4.png" class="img-fluid"></p>
<p>Les services interactifs comme <code>Jupyter</code>, <code>VSCode</code> ou <code>RStudio</code> permettent alors à l’utilisateur d’accéder à une interface pour exécuter des traitements <code>Python</code> ou <code>R</code>.</p>
<p><img src="https://ssphub.netlify.app/post/onyxia/onyxia_ui5.png" class="img-fluid"></p>
<p>L’accès aux données peut se faire depuis la ligne de commande (via un utilitaire <a href="https://min.io/docs/minio/linux/reference/minio-mc.html">Minio Client</a>) ou par un package <code>Python</code> ou <code>R</code> dédié qui permet de traiter le système de stockage distant comme un système local. Les traitements sont exécutés sur les serveurs de la plateforme qui héberge les notebooks, indépendamment de la machine par laquelle l’utilisateur accède au service. Par exemple, dans le cas du <code>SSPCloud</code>, les traitements sont exécutés depuis des serveurs hébergés à l’Insee.</p>
</section>
<section id="la-communauté-onyxia" class="level2">
<h2 class="anchored" data-anchor-id="la-communauté-onyxia">La communauté <code>Onyxia</code></h2>
<p>Tous les composants sont proposés en <em>open source</em> par l’Insee ce qui permet de fédérer une communauté d’utilisateurs et de développeurs de ce produit. Il s’agit d’un bel exemple de mutualisation au sein de l’État et au delà. Les dépôts peuvent être retrouvés sur le <code>Github</code> de l’équipe innovation (celui de <a href="https://github.com/InseeFrLab/onyxia-web">l’interface web</a>, celui des <a href="https://github.com/InseeFrLab/images-datascience">images pour la data-science</a>…). La communauté peut proposer de nouveaux services dans le catalogue.</p>
<p>Cette approche <em>bottom up</em> a déjà permis d’adapter des services aux besoins des utilisateurs ou d’améliorer la solution grâce à des retours des ré-utilisateurs d’<code>Onyxia</code>.</p>
<!-----
<iframe width="100%" height="500px" marginheight="0" marginwidth="0" src="./template/index.html">
Fallback text here for unsupporting browsers, of which there are scant few.
</iframe>
----->
</section>
<section id="les-plateformes-basées-sur-onyxia" class="level2">
<h2 class="anchored" data-anchor-id="les-plateformes-basées-sur-onyxia">Les plateformes basées sur Onyxia</h2>
<p>La plateforme d’origine, le <code>SSPCloud</code>, est ouverte à tous les agents de l’État et à plusieurs écoles. Celle-ci est exclusivement limitée à l’exploitation de données <em>open data</em>. Cette stratégie d’offreur de services de traitement sur l’open data permet de montrer l’expertise de l’Insee sur les sujets <em>data science</em>.</p>
<p>Les principaux usages de cette plateforme sont les suivants :</p>
<ul>
<li>la formation ;</li>
<li>l’organisation de hackathons ;</li>
<li>la mise à disposition de services innovants et visualisations utilisant de l’open data ;</li>
</ul>
<p>Grâce à la mise à disposition de la solution <code>Onyxia</code> sur <code>Github</code>, il est néanmoins possible d’adapter cette plateforme pour des <em>datalab</em> internes, sur données plus sensibles.</p>
<p>L’Insee n’est donc désormais plus seul et fédère de nombreux acteurs autour de son projet. Fin 2021, <code>Eurostat</code> a été la première organisation en dehors de l’Insee à choisir <code>Onyxia</code> pour construire son <a href="https://github.com/eurostat/datalab"><em>Cloud Agnostic Data Lab</em></a>. Expertise France pour le projet <code>DATAFID</code> a fait le choix d’Onyxia tout comme le CASD, le GENES ou encore le <em>BercyHub</em> avec le projet <code>Nubonyxia</code>.</p>
<p>D’autres organisations sont plus dans une phase de POC ou d’étude : l’INS norvégien, Pole Emploi, Data4Good, le ministère de l’Intérieur, le ministère de la Justice, l’Inria…</p>
<p>Dans le cadre du <a href="https://tosit.fr/">TOSIT</a>, association qui réunit de gros acteurs publics et privés autour de solutions <em>open source</em>, un certain nombre d’entreprises s’intéressent à <code>Onyxia</code>.</p>
</section>
<section id="vidéo-de-présentation-donyxia" class="level2">
<h2 class="anchored" data-anchor-id="vidéo-de-présentation-donyxia">Vidéo de présentation d’<code>Onyxia</code></h2>
{{&lt; youtube zwKiO6LcIWE &gt;}}
<div class="quarto-video ratio ratio-16x9"><iframe data-external="1" src="https://www.youtube.com/embed/zwKiO6LcIWE" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
</section>
<section id="références" class="level2">
<h2 class="anchored" data-anchor-id="références">Références</h2>
<div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-davenport2022data" class="csl-entry">
Davenport, Thomas H, et DJ Patil. 2022. <span>«&nbsp;Is Data Scientist Still the Sexiest Job of the 21st Century?&nbsp;»</span> <em>Harvard business review</em>. <a href="https://hbr.org/2022/07/is-data-scientist-still-the-sexiest-job-of-the-21st-century">https://hbr.org/2022/07/is-data-scientist-still-the-sexiest-job-of-the-21st-century</a>.
</div>
</div>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Notes de bas de page</h2>

<ol>
<li id="fn1"><p>On peut ajouter que cette question n’est pas exclusivement technologique. Même s’il est volontairement polémique, l’article de <a href="https://motherduck.com/blog/big-data-is-dead/">Jordan Tigani “Big Data is Dead”</a> illustre bien le changement de paradigme du monde de la <em>tech</em>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Insee</category>
  <category>onyxia</category>
  <category>sspcloud</category>
  <guid>https://ssphub.netlify.app/post/onyxia/</guid>
  <pubDate>Wed, 10 May 2023 00:00:00 GMT</pubDate>
  <media:content url="https://ssphub.netlify.app/post/onyxia/featured.png" medium="image" type="image/png" height="161" width="144"/>
</item>
<item>
  <title>Polars, une alternative fraîche à Pandas</title>
  <link>https://ssphub.netlify.app/post/polars/</link>
  <description><![CDATA[ 





<p>Le concept de <strong><a href="https://www.databricks.com/glossary/what-are-dataframes"><em>dataframe</em></a></strong> est central pour le <em>data scientist</em> qui manipule des données tabulaires. En <code>Python</code>, <a href="https://pandas.pydata.org/"><code>Pandas</code></a> est la solution de loin la plus populaire. En moyenne, le <em>package</em> est téléchargé 4 millions de fois par semaine, depuis des années.</p>
<p>Un petit nouveau apporte un vent de fraîcheur dans le domaine : <a href="https://www.pola.rs/"><code>Polars</code></a>.</p>
<p>Ses atouts ? D’excellentes performances et une expressibilité qui le rapproche d’un <a href="https://dplyr.tidyverse.org/"><code>dplyr</code></a>.</p>
<p>Ce post de blog revient sur les principaux atouts de <code>Polars</code>, sans vouloir être exhaustif. Un <em>notebook</em> illustrant les principales fonctionnalités du package vise à le compléter :</p>
<p><a href="https://datalab.sspcloud.fr/launcher/ide/jupyter-python?version=1.13.22&amp;autoLaunch=true&amp;init.personalInit=«https%3A%2F%2Fraw.githubusercontent.com%2FInseeFrLab%2Fssphub%2Fmain%2Fpost%2Fpolars%2Finit.sh»&amp;init.personalInitArgs=«polars-tuto»&amp;onyxia.friendlyName=«Tutoriel Polars»" target="_blank" rel="noopener"><img src="https://img.shields.io/badge/SSPcloud-Tester%20via%20SSP--cloud-informational&amp;color=yellow?logo=Python" alt="Onyxia"></a> <a href="https://colab.research.google.com/github/inseefrlab/ssphub/blob/main/post/polars/polars-tuto.ipynb" target="_blank" rel="noopener"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a></p>
<section id="les-secrets-de-la-performance" class="level1">
<h1>Les secrets de la performance</h1>
<p>Les <em>benchmarks</em> disponibles <a href="https://h2oai.github.io/db-benchmark/">sont clairs</a> : <code>Polars</code> est un ours qui court vite !</p>
<p>Le benchmark suivant, <a href="https://h2oai.github.io/db-benchmark/">effectué par H2O</a>, propose un comparatif de la vitesse des principaux frameworks de manipulation de données pour effectuer une agrégation par groupe avec un jeu de données de 50GB:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/polars/polars-benchmark-short.png" class="img-fluid figure-img"></p>
<figcaption>Benchmark H2O</figcaption>
</figure>
</div>
<p><code>Polars</code> devance des solutions connues pour leur efficacité sur ce type d’opérations, comme le package <code>R</code> <code>data.table</code>. L’utilisateur habituel de <code>Pandas</code> ne pourrait même pas traiter ces données, qui excèdent les capacités computationnelles de sa machine.</p>
<section id="lévaluation-lazy" class="level2">
<h2 class="anchored" data-anchor-id="lévaluation-lazy">L’évaluation <em>lazy</em></h2>
<p>Plusieurs éléments expliquent cette rapidité.</p>
<p>En premier lieu, <code>Polars</code> est conçu pour optimiser les requêtes : grâce au mode <em>lazy</em> (<em>“paresseux”</em>), on laisse la possibilité au moteur d’analyser ce qu’on souhaite faire pour proposer une exécution optimale (pour la lecture comme pour la transformation des jeux de données). La <em>lazy evaluation</em> est une méthode assez commune pour améliorer la vitesse des traitements et est utilisée, entre autres, par <code>Spark</code>.</p>
<p>Du fait de la <em>lazy evaluation</em> il est ainsi possible, par exemple, si un filtre sur les lignes arrive tardivement, de le remonter dans l’ordre des opérations effectuées par <code>Python</code> afin que les opérations ultérieures ne soient effectuées que sur l’ensemble optimal de données. Ces optimisations sont détaillées dans la <a href="https://pola-rs.github.io/polars-book/user-guide/optimizations/intro.html">documentation officielle</a>.</p>
</section>
<section id="lecture-optimisée-des-fichiers" class="level2">
<h2 class="anchored" data-anchor-id="lecture-optimisée-des-fichiers">Lecture optimisée des fichiers</h2>
<p>L’utilisateur <code>Pandas</code> est habitué à lire du CSV avec <code>pd.read_csv</code>. Avec <code>Polars</code>, il existe deux manières, très ressemblantes de le faire.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> polars <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pl</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Création d'une requête</span></span>
<span id="cb1-4">q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (</span>
<span id="cb1-5">    pl.scan_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iris.csv"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Lecture lazy</span></span>
<span id="cb1-6">    .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sepal_length"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb1-7">    .groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"species"</span>)</span>
<span id="cb1-8">    .agg(pl.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>())</span>
<span id="cb1-9">)</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exécution de la requête</span></span>
<span id="cb1-12">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> q.collect()</span></code></pre></div></div>
<p>Avec cette syntaxe, les connaisseurs de <code>Pyspark</code> retrouveront facilement leurs petits (ours 🐻).</p>
<p>On peut toujours lire de manière plus directe (en mode <em>eager</em>, <em>“impatient”</em>) en utilisant la fonction <code>read_csv</code>, et ensuite appliquer des transformations optimisables en glissant habilement <code>lazy</code> :</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pl.read_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iris.csv"</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3">df_res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.lazy() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ←  ici :)</span></span>
<span id="cb2-4">  .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sepal_length"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb2-5">  .groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"species"</span>)</span>
<span id="cb2-6">  .agg(pl.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>())</span>
<span id="cb2-7">  .collect()</span></code></pre></div></div>
<p><code>Polars</code> fonctionne également très bien avec le format <code>Parquet</code>, comme illustré dans le <em>notebook</em> qui accompagne ce <em>post</em>.</p>
</section>
<section id="parallélisation" class="level2">
<h2 class="anchored" data-anchor-id="parallélisation">Parallélisation</h2>
<p><code>Polars</code> parallélise les traitements dès que cela est possible, notamment dans le cas d’agrégation. Chaque coeur se charge d’une partie de l’agrégation et envoie des données plus légères à <code>Python</code> qui va finaliser l’agrégation.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/polars/polars-split-parallel-apply-combine.svg" class="img-fluid figure-img"></p>
<figcaption>Parallélisation</figcaption>
</figure>
</div>
<p><em>Illustration du principe de la parallélisation</em></p>
<p>Sur les systèmes proposant de nombreux coeurs, cela peut faire gagner beaucoup de temps.</p>
</section>
<section id="des-couches-basses-à-la-pointe" class="level2">
<h2 class="anchored" data-anchor-id="des-couches-basses-à-la-pointe">Des couches basses à la pointe</h2>
<p>Enfin, le choix d’utiliser à la fois le format de représentation en mémoire <a href="https://arrow.apache.org/">Arrow</a> et le langage <a href="https://www.rust-lang.org/fr">Rust</a> pour le coeur de la bibliothèque n’est pas étranger à cette performance.</p>
</section>
<section id="calculs-out-of-memory" class="level2">
<h2 class="anchored" data-anchor-id="calculs-out-of-memory">Calculs <em>out of memory</em></h2>
<p><code>Polars</code> travaille vite mais présente aussi l’avantage de lire naturellement des jeux de données hors des limites de la mémoire de l’ordinateur grâce à <a href="https://www.youtube.com/watch?v=3-C0Afs5TXQ">sa capacité de lire en flux</a> (méthode qu’on appelle le <em>streaming</em>).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># La même requête que tout à l'heure va lire le fichier "en flux"</span></span>
<span id="cb3-2">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> q.collect(streaming<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div></div>
<p>De plus, <code>Polars</code> lit nativement les fichiers <code>Parquet</code> qui par ses propriétés permet d’aller beaucoup plus vite que le CSV !</p>
</section>
</section>
<section id="une-api-fluide" class="level1">
<h1>Une API fluide</h1>
<p>C’est un reproche régulièrement fait à <code>Pandas</code> : la syntaxe de manipulations des données est parfois complexe ou peu lisible, et les choix d’écriture ne sont pas transparents du point de vue des performances.</p>
<p>L’API proposée par Polars est à la fois expressive et transparente. Voici un exemple d’exploitation de la BPE, issu du <em>notebook</em> accompagnant ce <em>post</em> :</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">df.lazy()</span>
<span id="cb4-2">  .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb4-3">    pl.col(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TYPEQU"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B316"</span></span>
<span id="cb4-4">  )</span>
<span id="cb4-5">  .groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DEP"</span>)</span>
<span id="cb4-6">  .agg(</span>
<span id="cb4-7">    pl.count().alias(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NB_STATION_SERVICE"</span>)</span>
<span id="cb4-8">  )</span>
<span id="cb4-9">  .collect()</span></code></pre></div></div>
<p>On retrouve une sémantique d’opérations de haut niveau qui s’enchaînent à la manière de <a href="https://www.book.utilitr.org/03_fiches_thematiques/fiche_tidyverse#comment-utiliser-lop%C3%A9rateur-pipe-avec-le-tidyverse">ce que l’on peut faire en <code>dplyr</code></a>.</p>
</section>
<section id="les-autres-concurrents" class="level1">
<h1>Les autres concurrents</h1>
<p><code>Pandas</code> et <code>Polars</code> ne sont pas seuls dans le grand zoo de la manipulation de données en Python : des solutions comme <a href="https://github.com/vaexio/vaex">Vaex</a> ou <a href="https://github.com/dask/dask">Dask</a> ont des arguments à faire valoir. <code>DuckDB</code>, un autre <em>framework</em> de manipulation de données, s’intègre quand à lui très bien avec <code>Polars</code> dans la ménagerie.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/polars/dalle_polar_duck.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:40.0%"></p>
</figure>
</div>
<p><em>Le duo DuckDB-Polars illustré par Dall-E-2</em></p>
</section>
<section id="ressources-supplémentaires" class="level1">
<h1>Ressources supplémentaires</h1>
<ul>
<li><a href="https://github.com/ddotta/awesome-polars"><em>Awesome Polars</em> par Damien Dotta (INSEE)</a></li>
<li><a href="https://rpolars.github.io/">Polars pour R</a></li>
<li><a href="https://www.rhosignal.com/tags/polars/">rhosignal.com/tags/polars/</a></li>
<li><a href="https://kevinheavey.github.io/modern-polars/">kevinheavey.github.io/modern-polars/</a></li>
</ul>
<p>Le <em>notebook</em> accompagnant ce <em>post</em>:</p>
<p><a href="https://datalab.sspcloud.fr/launcher/ide/jupyter-python?autoLaunch=false&amp;init.personalInit=%C2%ABhttps%3A%2F%2Fraw.githubusercontent.com%2Fromaintailhurat%2Fssphub%2Fblog%2Fpolars%2Fcontent%2Fnotebooks%2Finit.sh%C2%BB&amp;init.personalInitArgs=%C2%ABpolars-tuto%C2%BB&amp;onyxia.friendlyName=%C2%ABTutoriel%20Polars%C2%BB" target="_blank" rel="noopener"><img src="https://img.shields.io/badge/SSPcloud-Tester%20via%20SSP--cloud-informational&amp;color=yellow?logo=Python" alt="Onyxia"></a> <a href="http://colab.research.google.com/github/inseefrlab/ssphub/blob/main/content/notebooks/polars-tuto.ipynb" target="_blank" rel="noopener"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a></p>


</section>

 ]]></description>
  <category>Python</category>
  <category>Pandas</category>
  <category>Polars</category>
  <category>Data wrangling</category>
  <guid>https://ssphub.netlify.app/post/polars/</guid>
  <pubDate>Fri, 10 Feb 2023 00:00:00 GMT</pubDate>
  <media:content url="https://ssphub.netlify.app/post/polars/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Infolettre n°9</title>
  <link>https://ssphub.netlify.app/post/recap2022/</link>
  <description><![CDATA[ 





<p><em>Vous désirez intégrer la liste de diffusion ? Un mail à <a href="mailto:ssphub-contact@insee.fr" class="email">ssphub-contact@insee.fr</a> suffit</em></p>
<p>La <a href="https://ssphub.netlify.app/post/retrospective2022/">rétrospective de l’année 2022</a> promettait une version plus personnalisée, inspirée des visualisations proposées par les réseaux sociaux pour synthétiser l’activité de leurs utilisateurs.</p>
<p>Cette <em>newsletter</em> un peu spéciale propose un retour sur la première année du réseau des data scientists de la statistique publique dont la préfiguration a commencé en mars 2022 et qui a été lancé officiellement en septembre. Vous pourrez retrouver à la fin de la <em>newsletter</em> des informations plus classiques: événements, retour sur les actions du réseau, formations, etc.</p>
<p>Elle permet aussi d’illustrer le potentiel d’outils qui ont été présentés dans la <a href="https://ssphub.netlify.app/post/retrospective2022/">rétrospective de l’année 2022</a>. Toutes les figures sont réactives, notamment quand vous passez votre souris dessus. Les principaux ingrédients qui ont été ici utilisés, et qui avaient été mentionnés dans la première partie de la rétrospective, sont <code>Observable</code>, <code>Quarto</code> et <code>DuckDB</code>. Les données sont stockées sur le système de stockage <code>S3</code> du <code>SSPCloud</code>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Ce <em>notebook</em> utilise certaines les fonctionalités d’<code>Observable</code> pour proposer des visualisations interactives de manière efficace.</p>
<details>
<summary>
<p>Si vous êtes intéressés par le <em>making-of</em> de cette page <em>web</em>, cliquez sur le menu déroulant (partie plus technique).</p>
</summary>
<p><br> <code>Observable</code> est à la fois un langage visant à simplifier l’usage de <code>JavaScript</code> pour mettre en oeuvre des visualisations interactives et une <a href="https://observablehq.com/">plateforme</a> permettant de simplifier la mise à disposition de ces visualisations sous une forme de <em>notebook</em>.</p>
<p>Les statistiques de comptage sont enregistrées sous format <code>Parquet</code> sur le système de stockage <code>S3</code> du <code>SSPCloud</code>. L’intégration native de <code>DuckDB</code> à <code>Observable</code> permet au navigateur <em>web</em> de lire et d’effectuer des manipulations de données à travers des requêtes SQL de manière très efficace. Sur ce sujet, outre la <a href="https://observablehq.com/@observablehq/duckdb">documentation officielle d’<code>Observable</code></a>, je recommande vivement le <a href="https://observablehq.com/@ericmauviere/duckdb-redonne-nouvelle-vie-sql">tutoriel d’Eric Mauvière</a>.</p>
<p>La librairie <a href="https://observablehq.com/@observablehq/plot"><code>Plot</code></a> propose de nombreuses fonctionalités utiles pour construire des visualisations interactives. Sa logique est assez proche de celle des <em>frameworks</em> <a href="https://observablehq.com/@observablehq/plot-from-ggplot2"><code>ggplot2</code> en <code>R</code></a> ou <a href="https://observablehq.com/@observablehq/plot-overview-for-matplotlib-users"><code>matplotlib</code> en <code>Python</code></a>. Lorsque la librairie <code>Plot</code> n’est plus suffisante, comme pour le <em>treemap</em> sur cette page, le <em>framework</em> <code>d3.js</code> est utile.</p>
<p>L’intégration de figures construites à partir du langage <code>Observable</code> peut être faite de plusieurs manières:</p>
<ul>
<li>Utiliser <a href="https://quarto.org/docs/interactive/ojs/"><code>Quarto</code></a> qui permet de créer une page <em>web</em> statique autosuffisante à partir d’une suite d’instructions dans des blocs <code>{ojs}</code>. Cette méthode est très intéressante pour l’intégration de figures <code>JavaScript</code> dans des sites <em>web</em> complets générés de manière automatique. Bien qu’initialement envisagée, cette méthode ne fait pas encore bon ménage avec <a href="https://gohugo.io/"><code>Hugo</code></a> qui n’attend pas des <code>Quarto Markdown</code> mais des <code>Markdown</code> classiques</li>
<li>Utiliser la plateforme <a href="https://observablehq.com/@linogaliana"><code>observablehq</code></a> pour créer un <em>notebook</em> proposant le code à l’origine des visualisations puis intégrer ces dernières par le biais d’<code>&lt;iframe&gt;</code> ou de l’intégration via <code>Runtime JS</code>. Cela permet d’avoir ces visualisations sur une page statique sans stocker au même endroit le code ayant permis de les générer et permettant de les reproduire, qui n’intéresse pas nécessairement le même public.</li>
</ul>
<p>La deuxième approche, celle de l’intégration depuis un <em>notebook</em> <code>observable</code> a été choisie. Ce <em>notebook</em> est disponible sur <a href="https://observablehq.com/@linogaliana/2022-year-recap-data-scientists-network">la plateforme</a> pour les utilisateurs intéressés par la réutilisation des figures, des données sous-jacentes ou des utilitaires ayant permis d’aboutir à certaines visualisations. Le code source de cette page, disponible sur le <a href="https://raw.githubusercontent.com/InseeFrLab/ssphub/main/content/post/recap2022/index.md"><code>Github inseefrlab/ssphub</code></a> illustre la manière dont les figures peuvent être intégrées à un site <em>web</em> depuis la plateforme <a href="https://observablehq.com/"><code>observablehq</code></a>. Bien que j’ai privilégié la méthode <code>Runtime JS</code>, qui permet d’intégrer la visualisation sans le bandeau <code>Observable</code> sous la figure, pour certaines d’entre elles, j’ai dû utiliser la méthode <code>&lt;iframe&gt;</code> du fait de certaines limitations dans l’héritage de règles CSS aux <code>svg</code> générés par <code>Plot</code> qui affectaient la colorisation et donc la lecture de certaines figures.</p>
</details>
</div>
</div>
<section id="lannée-du-réseau" class="level2">
<h2 class="anchored" data-anchor-id="lannée-du-réseau">L’année du réseau</h2>
<p>Le réseau comporte deux canaux de communication: une liste de diffusion mail et un canal de discussions instantanées. Intéressons nous d’abord à la liste de diffusion mail !</p>
<div id="observablehq-evolution-bbfd8ffe">

</div>
<p>Pendant l’année 2022, <strong>7 <em>newsletters</em></strong> ont été diffusées par mail. Chacune a permis d’augmenter sensiblement le nombre de personnes dans la liste de diffusion. A la fin de l’année, il y avait <strong>312 inscrits</strong><sup>1</sup> dans la liste de diffusion.</p>
<p>Le réseau a organisé <strong>trois événements</strong> pendant l’année 2022. D’abord, avant l’été, deux <em>open hours</em> ont eu lieu. Cet événement informel prenant la forme de retour d’expérience a été l’occasion de discussions stimulantes autour de d’usage de la <em>data science</em> pour l’administration. En novembre, l’<a href="../../talk/presentation-dobservable-par-nicolas-lambert/">événement autour d’<code>Observable</code></a> animé par <a href="https://observablehq.com/@neocartocnrs">Nicolas Lambert</a> a réuni près de 50 personnes.</p>
</section>
<section id="répartition-des-modes-daccès-au-réseau" class="level2">
<h2 class="anchored" data-anchor-id="répartition-des-modes-daccès-au-réseau">Répartition des modes d’accès au réseau</h2>
<p>Le réseau propose <strong>deux canaux de diffusion</strong> de l’information: une liste de diffusion par mail et un canal de discussion instantanée qui utilise la messagerie sécurisée de l’Etat <a href="https://www.tchap.gouv.fr/"><code>Tchap</code></a>. Environ <strong>55%</strong> des membres de la liste de diffusion (soit plus de 180 personnes) sont également inscrits sur le canal de discussion instantanée.</p>
<iframe width="100%" height="529" frameborder="0" src="https://observablehq.com/embed/@linogaliana/2022-year-recap-data-scientists-network?cells=repartition">
</iframe>
</section>
<section id="composition-du-réseau" class="level2">
<h2 class="anchored" data-anchor-id="composition-du-réseau">Composition du réseau</h2>
<p>La diffusion d’informations par le réseau a permis de réunir des <em>data scientists</em> de <strong>27 organisations différentes</strong>. L’<a href="https://www.insee.fr/fr/accueil">Insee</a>, qui représente 47% de l’effectif du réseau, est majoritaire. Suivent dans le palmarès, les services statistiques du <a href="https://drees.solidarites-sante.gouv.fr/">Ministère de la Santé (DREES)</a> et du <a href="https://www.statistiques.developpement-durable.gouv.fr/">Ministère du Développement Durable (SDES)</a>.</p>
<div id="observablehq-treemap_network-81108356">

</div>
</section>
<section id="évolution-de-la-composition-du-réseau" class="level2">
<h2 class="anchored" data-anchor-id="évolution-de-la-composition-du-réseau">Évolution de la composition du réseau</h2>
<p>La diffusion progressive d’informations par le biais des <em>newsletters</em> a permis de diversifier progressivement la composition de la liste de diffusion. Alors que la première <em>newsletter</em> de l’année 2022 avait été diffusée auprès de <strong>14 institutions</strong>, ce sont des agents de <strong>27 organisations</strong> qui ont reçues la dernière.</p>
<p>Les événements organisés par le réseau ou les présentations spéciales, comme celle pour les <a href="https://prez-nouveaux-admin-ssphub-20221125.netlify.app/#/title-slide">administrateurs de l’INSEE en poste à l’ENSAE</a>, ont également pu motiver des personnes à intégrer le réseau.</p>
<div id="observablehq-grid-bbfd8ffe">

</div>
</section>
<section id="programme-10" class="level2">
<h2 class="anchored" data-anchor-id="programme-10">Programme 10%</h2>
<p>Les membres du réseau des <em>data scientists</em> ont été particulièrement actifs dans le cadre du <strong><a href="https://10pourcent.etalab.studio/">programme interministériel 10%</a></strong>, issu des recommandations d’un <a href="https://www.numerique.gouv.fr/uploads/RAPPORT-besoins-competences-donnee.pdf">rapport INSEE-DINUM “Évaluation des besoins de l’État en compétences et expertises en matière de donnée”</a>.</p>
<p>La saison 1 a donné sa chance à <strong>quatre projets</strong>, portés par différentes administrations. Si l’un d’eux existait déjà depuis plus de deux ans (projet <a href="https://github.com/spyrales/gouvdown"><code>Gouvdown</code></a>), trois sont nés pour l’occasion, avec la mise en ligne de code immédiate (<code>Cartiflette</code>) ou postérieure au <em>bootcamp</em> de lancement (<code>Socratext</code> et <code>matchSIRET</code>) .</p>
<p>Tous les projets sont ouverts et disponible sur <code>Github</code>. Une statistique qui permet de représenter leur succès est le nombre de ⭐: c’est un peu un mélange entre un site en favori sous <code>Firefox</code> puisque cela permet de facilement retrouver un projet dans <code>Github</code> et le nombre de <em>followers</em> d’une page sur <code>Facebook</code> ou sur <code>Twitter</code> puisque cela permet de suivre l’activité d’un dépôt <code>Github</code>.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Cette visualisation fait appel à l’API <code>Github</code>. Si les figures ne s’affichent pas, cela peut être dû à un dépassement du nombre de requêtes par heure autorisées par l’API <code>Github</code> sans jeton. A l’heure actuelle, il n’existe pas encore de fonctionalité gratuite sous <code>Observable</code> pour stocker de manière sécurisée un jeton pour l’API <code>Github</code>.</p>
<details>
<summary>
Dérouler pour afficher une version non réactive
</summary>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/recap2022/cartiflette.png" class="img-fluid figure-img"></p>
<figcaption>Projet <code>cartiflette</code></figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/recap2022/socratext.png" class="img-fluid figure-img"></p>
<figcaption>Projet <code>Socratext</code></figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/recap2022/gouvdown.png" class="img-fluid figure-img"></p>
<figcaption>Projet <code>Gouvdown</code></figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/recap2022/matchsiret.png" class="img-fluid figure-img"></p>
<figcaption>Projet <code>matchSIRET</code></figcaption>
</figure>
</div>
</details>
</div>
</div>
<iframe width="100%" height="967" frameborder="0" src="https://observablehq.com/embed/@linogaliana/2022-year-recap-data-scientists-network?cells=containeur_github"></iframe>
</section>
<section id="autres-actualités-du-réseau" class="level2">
<h2 class="anchored" data-anchor-id="autres-actualités-du-réseau">Autres actualités du réseau</h2>
<section id="présentation-de-gridviz-par-julien-gaffuri" class="level3">
<h3 class="anchored" data-anchor-id="présentation-de-gridviz-par-julien-gaffuri">Présentation de <code>Gridviz</code> par Julien Gaffuri</h3>
<p>Pour rappel, le <strong>20 Janvier 2023 de 11h à 12h30</strong> Julien Gaffuri (Eurostat) viendra nous présenter la librairie open-source <a href="https://eurostat.github.io/gridviz/"><code>Gridviz</code></a>. Réservez ce créneau pour découvrir cette librairie qui ouvre de nouvelles perspectives pour la mise à disposition de données géographiques !</p>
<p><a href="gridviz.ics">Télécharger l’invitation à l’événement sous format <code>Outlook</code></a></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/recap2022/gridviz.png" class="img-fluid figure-img" style="width:70.0%"></p>
<figcaption>Source: <a href="https://observablehq.com/@neocartocnrs/hello-gridviz">Notebook <code>Hello Gridviz</code> par <code>neocarto</code> sur <code>Observable</code></a></figcaption>
</figure>
</div>
</section>
<section id="première-place-européenne-au-hackathon-big-data-de-lonu" class="level3">
<h3 class="anchored" data-anchor-id="première-place-européenne-au-hackathon-big-data-de-lonu">Première place européenne au <em>hackathon</em> Big Data de l’ONU</h3>
<p>Les résultats du <em>hackathon big data</em> de l’ONU, ayant eu lieu du 7 au Novembre 2022, ont été annoncés ! L’<a href="https://github.com/InseeFrLab/hackathon-un-2022">équipe <code>Datadive</code></a> - constituée de membres du réseau de l’INSEE, de la DGFIP et du CASD - est arrivée à la <a href="https://unstats.un.org/bigdata/events/2022/hackathon/winners-bde.cshtml">première place des équipes européennes</a> 🎉.</p>
</section>
<section id="git-et-bonnes-pratiques-des-formations-de-formateurs-prévus-pour-les-statisticiens-publics" class="level3">
<h3 class="anchored" data-anchor-id="git-et-bonnes-pratiques-des-formations-de-formateurs-prévus-pour-les-statisticiens-publics"><code>Git</code> et bonnes pratiques: des formations de formateurs prévus pour les statisticiens publics</h3>
<p>Les nouvelles formations à <code>Git</code> et aux bonnes pratiques avec <code>R</code>, testées récemment à l’Insee et au service statistique du Ministère du Travail, la DARES, (voir <em>newsletters</em> de <a href="../../uploads/infolettre_6.pdf">Novembre</a> et <a href="(/uploads/infolettre_7.pdf)">Décembre</a>), deviennent des formations nationales.</p>
<p>Pour pouvoir diffuser les bonnes pratiques favorisant le partage de codes et la qualité des projets statistiques, il est nécessaire d’avoir le plus d’enseignants possibles pour cette formation. Pour permettre cela, un appel à candidat pour une <strong>formation de formateurs</strong> a été diffusée à l’Insee et dans les services statistiques ministériels. Si vous êtes intéressés et ne l’avez pas reçu, n’hésitez pas à envoyer un mail à ssphub-contact@insee.fr</p>
<p>En attendant, les supports de ces formations sont déjà disponibles sur <a href="https://inseefrlab.github.io/formation-bonnes-pratiques-git/#/title-slide">inseefrlab.github.io/formation-bonnes-pratiques-git/</a> et sur <a href="https://inseefrlab.github.io/formation-bonnes-pratiques-R/#/title-slide">inseefrlab.github.io/formation-bonnes-pratiques-R/</a>. Les codes sources sont bien-sûr ouverts et disponibles sur <code>Github</code>, tant pour la <a href="https://github.com/InseeFrLab/formation-bonnes-pratiques-git">première partie</a> que pour <a href="https://github.com/InseeFrLab/formation-bonnes-pratiques-R">la seconde</a>. Ceux-ci sont construits collectivement, n’hésitez pas à suggérer des modifications depuis <code>Github</code>.</p>
<p>Un site web plus complet devrait prochainement voir le jour pour accompagner cette formation. En complément de celui-ci, des éléments peuvent déjà être trouvés dans le cours de 3e année de l’ENSAE sur la <a href="https://ensae-reproductibilite.netlify.app/">mise en production de projets <em>data science</em></a> et dans la documentation collaborative <a href="https://www.utilitr.org/"><code>utilitR</code></a>.</p>
<style media="screen">


  #observablehq-evolution-bbfd8ffe .has-title {
    font-size: 18px;
  }


  #observablehq-treemap_network-81108356 .has-title {
    fill: black;
    font-size: 18px;
    font-weight: bold;
  }

  .slidecontainer {
    width: 100%;
  }

  .slider {
    -webkit-appearance: none;
    width: 100%;
    height: 15px;
    border-radius: 5px;
    background: #d3d3d3;
    outline: none;
    opacity: 0.7;
    -webkit-transition: .2s;
    transition: opacity .2s;
  }

  .slider:hover {
    opacity: 1;
  }

  .slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 25px;
    height: 25px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
  }

  .slider::-moz-range-thumb {
    width: 25px;
    height: 25px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
  }

  svg {
    background-color: white !important;
    color: black !important;
  }
  
  figure figcaption {
    /* text-align: center; */
    text-align: left;
  }
.fullwidth {
  width: 100vw;
  position: relative;
  left: 50%;
  right: 50%;
  margin-left: -50vw;
  margin-right: -50vw;
}  
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@observablehq/inspector@5/dist/inspector.css">
<script type="module">
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@5/dist/runtime.js";
import define from "https://api.observablehq.com/@linogaliana/2022-year-recap-data-scientists-network.js?v=3";
new Runtime().module(define, name => {
  if (name === "evolution") return new Inspector(document.querySelector("#observablehq-evolution-bbfd8ffe"));
  if (name === "treemap_network") return new Inspector(document.querySelector("#observablehq-treemap_network-81108356"));
  if (name === "grid") return new Inspector(document.querySelector("#observablehq-grid-bbfd8ffe"));
});
</script>


</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Notes de bas de page</h2>

<ol>
<li id="fn1"><p>Les actions de communication du mois de janvier ont permis d’augmenter sensiblement le nombre de personnes dans cette liste (340 début janvier). Un retour spécial sur le mois de janvier sera l’occasion idéale pour une autre rétrospective quantitative.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Insee</category>
  <category>Retrospective</category>
  <category>Infolettre</category>
  <category>Retrospective</category>
  <guid>https://ssphub.netlify.app/post/recap2022/</guid>
  <pubDate>Tue, 10 Jan 2023 00:00:00 GMT</pubDate>
  <media:content url="https://ssphub.netlify.app/post/recap2022/featured.png" medium="image" type="image/png" height="80" width="144"/>
</item>
<item>
  <title>Infolettre n°8</title>
  <link>https://ssphub.netlify.app/post/retrospective2022/</link>
  <description><![CDATA[ 





<p><em>Vous désirez intégrer la liste de diffusion ? Un mail à <a href="mailto:ssphub-contact@insee.fr" class="email">ssphub-contact@insee.fr</a> suffit</em></p>
<p>La fin de l’année est généralement synonyme de bétisiers, <em>best of</em> ou rétrospectives personnalisées qui nous permettent de nous rappeler les événements marquants de l’année.</p>
<p>Pour célébrer la fin de l’année 2022, la <em>newsletter</em> de janvier adopte un format un peu spécial pour proposer, en deux temps, deux rétrospectives.</p>
<p>Cette première <em>newsletter</em> revient sur les principaux événements de l’année 2022 dans le monde de la <em>data science</em>. La <a href="https://ssphub.netlify.app/post/recap2022/">seconde <em>newsletter</em></a> proposera une rétrospective quantitative sur le réseau des <em>data scientists</em> de la statistique publique, à la manière des rétrospectives personnalisées de nos applications préférées.</p>
<section id="les-ia-créatrices-de-contenu-à-lhonneur" class="level1">
<h1>Les IA créatrices de contenu à l’honneur</h1>
<p>Si l’année 2022 a été particulièrement riche dans le domaine de la <em>data science</em>, c’est principalement grâce à deux coups médiatiques d’<a href="https://openai.com/">OpenAI</a>, à savoir <a href="https://openai.com/dall-e-2/"><code>Dall-E</code></a> et <a href="https://openai.com/blog/chatgpt/"><code>ChatGPT</code></a>.</p>
<p>Ces deux outils ont beaucoup fait parler d’eux, au-delà de la sphère traditionnelle de la <em>data science</em>. Le <em>buzz</em> a été intense sur <code>Twitter</code> ou sur <code>Mastodon</code>, le réseau social dont le nombre d’utilisateurs a nettement augmenté en réaction au rachat de <code>Twitter</code> par Elon Musk en fin d’année.</p>
<p><img src="https://i.chzbgr.com/full/9717138688/hC5E738A3/chatgpt-will-see-all-criticisms-its-training-data-on-next-update-some-are-not-surviving-uprising.png" class="img-fluid" style="width:60.0%"></p>
<p>Ces innovations, parce qu’elles pourraient avoir des effets à long terme sur la manière dont le grand public appréhende l’intelligence artificielle, ont beaucoup intéressé les médias traditionnels, notamment <a href="https://www.lemonde.fr/economie/article/2022/12/06/chatgpt-le-logiciel-capable-d-ecrire-des-petits-textes-confondants_6153252_3234.html"><em>Le Monde</em></a>, <a href="https://www.economist.com/news/2022/06/11/how-a-computer-designed-this-weeks-cover"><em>The Economist</em> et sa <em>“Nouvelle Frontière”</em></a> ou <a href="https://www.theguardian.com/commentisfree/2022/dec/10/i-wrote-this-column-myself-but-how-long-before-a-chatbot-could-do-it-for-me">le <em>Guardian</em></a> qui s’interroge sur la nature des tâches que l’intelligence artificielle pourra remplacer à terme : procédurales et régies par des règles bien définies ou bien également des activités nécessitant de la créativité et des capacités d’analyse ?</p>
<p>Pour une fois, il ne s’agit donc pas de souligner exclusivement les limites de ces modèles voire leurs dérives (<a href="https://fr.wikipedia.org/wiki/Deepfake"><em>deep fake</em></a>, <a href="https://www.washingtonpost.com/technology/2022/07/16/racist-robots-ai/">biais racistes</a>…) mais aussi de s’enthousiasmer sur leur <a href="https://www.platformer.news/p/how-dall-e-could-power-a-creative">potentiel créatif</a>. Il est difficile de rester insensible à certaines des créations artistiques des modèles <a href="https://dalle2.gallery/#search-random"><code>Dall-E</code></a>, <a href="https://stablediffusion.fr/gallery"><code>Stable Diffusion</code></a>, <a href="https://www.midjourney.com/showcase/recent/">Midjourney</a> et consorts ou de résister à la tentation de tester la capacité de <code>ChatGPT</code> à répondre à des questions complexes Les chercheurs, et pas des moindres (notamment <a href="https://twitter.com/andrewyng/status/1600284752258686976?lang=fr">Andrew Ng</a> ou <a href="https://theconversation.com/beau-parleur-comme-une-ia-196084">Gaël Varoquaux</a>) se sont également saisis de cette question et ont souligné les biais de raisonnement et excès de confiance de ces IA.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://raw.githubusercontent.com/CompVis/stable-diffusion/2ff270f4e0c884d9684fa038f6d84d8600a94b39/assets/stable-samples/txt2img/merged-0006.png" class="img-fluid figure-img"></p>
<figcaption>https://github.com/Stability-AI/stablediffusion</figcaption>
</figure>
</div>
<p>Si vous désirez utiliser <code>Python</code> de manière créative pour générer du contenu avec <code>Stable Diffusion</code>, vous pouvez consulter <a href="https://pythonds.linogaliana.fr/dalle/">ce tutoriel</a> qui fonctionne sur le <a href="https://www.sspcloud.fr/"><code>SSPCloud</code></a> ou sur <code>Google Colab</code>.</p>
<p><img src="https://i.chzbgr.com/full/9717140480/hD779E54E/writing-shit-like-elon-musk-suicide-note-and-robert-ebert-goncharov-review-please-stop-need-sleep.png" class="img-fluid" style="width:50.0%"></p>
</section>
<section id="le-succès-des-modèles-de-diffusion" class="level1">
<h1>Le succès des modèles de diffusion</h1>
<p>Ces <a href="https://pub.towardsai.net/generative-ai-and-future-c3b1695876f2">IA génératrices de contenu</a> reposent toutes, à plusieurs niveaux, sur des réseaux de neurone.</p>
<p>Le premier étage de la fusée est un <a href="https://en.wikipedia.org/wiki/Language_model">modèle de langage</a> (<em>large language model</em>) qui synthétise un langage en un ensemble complexe de paramètres. Les plus connus sont <a href="https://en.wikipedia.org/wiki/BERT_(language_model)">BERT</a> et <a href="https://en.wikipedia.org/wiki/GPT-3">GPT-3</a>. L’inflation dans le nombre de paramètres n’est pas prête de s’arrêter. Si les ressources nécessaires à entraîner en 2018 le modèle BERT (110 millions de paramètres) avaient déjà <a href="https://arxiv.org/abs/1906.02243">été critiquées</a> en raison de leur coût financier et environnemental, cette complexité a encore augmenté depuis. Le modèle <code>GPT-3</code>, sorti en 2020, et qui sert de base à <code>Dall-E</code> et <code>ChatGPT</code> intègre 175 milliards de paramètres. Un chiffre qui apparaît minime par rapport aux 17O trillions de paramètres attendus pour le modèle <code>GPT-4</code> en 2023.</p>
<p>En ce qui concerne les IA créatrices de contenu visuel, le deuxième étage de la fusée est un modèle d’analyse d’image qui apprend à associer des images à une description textuelle afin de détecter des structures communes entre des mots ou des séquences de mots et des formes sur des images. Il s’agit de déconstruire une forme en une structure minimale de pixels qui permet de l’identifier.</p>
<figure class="figure">
<img src="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdb4ef038-9c72-42b5-9383-26952d95aea0_740x141.jpeg" alt="Illustration d'un modèle de diffusion" class="figure-img">
<figcaption>
<i>Source: <a href="https://magazine.sebastianraschka.com/p/ahead-of-ai-1-a-diffusion-of-innovations" target="_blank">Sebastian Raschka</a></i>
</figcaption>
</figure>
<p>Ensuite, pour générer une image à partir d’une description inédite intervient le <a href="https://en.wikipedia.org/wiki/Diffusion_model">modèle de diffusion</a> qui reconstruit une image à partir du mélange de l’ensemble des pixels qui traduisent les concepts principaux d’une instruction. L’une des explications les plus pédagogiques pour comprendre le fonctionnement de ces modèles vient du <a href="https://www.washingtonpost.com/technology/interactive/2022/ai-image-generator/"><em>Washington Post</em></a>.</p>
<p>Sinon, on peut demander directement à <code>ChatGPT</code> de nous expliquer:</p>
<p><img src="https://ssphub.netlify.app/post/retrospective2022/diffusion.png" class="img-fluid"></p>
</section>
<section id="lactualité-dans-le-monde-du-deep-learning" class="level1">
<h1>L’actualité dans le monde du <em>deep learning</em></h1>
<p>Si le succès d’estime de ces IA génératrices consacre les modèles de diffusion, l’année du <em>deep learning</em> ne se réduit pas à cette actualité.</p>
<p>L’année a notamment été marquée par la compétition entre les librairies et écosystèmes <a href="https://www.tensorflow.org/?hl=fr"><code>TensorFlow</code></a>, développé par <code>Google</code>, et <a href="https://pytorch.org/"><code>PyTorch</code></a> projet initié par <code>Facebook/Meta</code>. <code>PyTorch</code>, plus récent, bénéficie d’une dynamique plus ascendante que <code>TensorFlow</code>. Le <a href="https://twitter.com/huggingface/status/1609162974626779136?s=20&amp;t=XUCGBC_PL60IdHjia8wXNA">succès d’<code>HuggingFace</code></a>, plateforme de mise à disposition de modèles, et où les <a href="https://www.assemblyai.com/blog/pytorch-vs-tensorflow-in-2022/#pytorch-vs-tensorflowmodel-availability">implémentations <code>PyTorch</code> sont systématiques alors que celles en <code>TensorFlow</code> sont rares</a> a participé à la diffusion de <code>PyTorch</code>.</p>
<p>Preuve du succès de <code>PyTorch</code>, cet écosystème est dissocié de <code>Meta</code> depuis septembre afin de devenir un outil généraliste <a href="https://www.linuxfoundation.org/blog/blog/welcoming-pytorch-to-the-linux-foundation">géré par la <code>Linux Foundation</code></a>. À l’inverse, <a href="https://twitter.com/ylecun/status/1538419932475555840"><code>Google</code> semble se détacher graduellement de <code>TensorFlow</code></a> pour privilégier son nouvel écosystème <code>JAX</code>.</p>
</section>
<section id="du-changement-côté-rstudio" class="level1">
<h1>Du changement côté <code>RStudio</code></h1>
<p>Depuis quelques années, <code>RStudio</code> a fait le choix de devenir un écosystème de <em>data science</em> généraliste et non plus exclusivement attaché au langage <code>R</code>.</p>
<p>Cette année, cela s’est traduit par la publication, très commentée, de <a href="https://quarto.org/"><code>Quarto</code></a> qui vise à proposer, dans de nombreux langages de programmation, des fonctionalités de publications reproductibles équivalentes à l’un des produits emblématiques de <code>RStudio</code>, à savoir <a href="https://rmarkdown.rstudio.com/"><code>R Markdown</code></a>. Rien de mieux pour être convaincu de l’intérêt de cet outil que d’observer <a href="https://quarto.org/docs/gallery/">la galerie d’exemples</a>, d’explorer <a href="https://quarto.org/">la documentation très riche</a>, ou de <a href="https://quarto.org/docs/get-started/hello/rstudio.html">tester soi-même</a> sur un exemple. Cet été, <code>RStudio</code> a également annoncé que <code>Shiny</code>, un autre produit emblématique, serait maintenant disponible sous <a href="https://shiny.rstudio.com/py/"><code>Python</code></a>, comme alternative à <a href="https://dash.plotly.com/"><code>Dash</code></a> ou <a href="https://streamlit.io/"><code>Streamlit</code></a>.</p>
<p>L’année 2022 a été l’occasion, pour <code>RStudio</code>, d’un autre changement, symbolique celui-ci. Afin de détacher son image du langage <code>R</code>, l’entreprise a en effet changé de nom pour devenir <a href="https://posit.co/"><code>posit</code></a>. L’entreprise n’a néanmoins pas abandonné son activité foisonnante dans <code>R</code> puisque Hadley Wickham a commencé à publier de nouveaux chapitres pour une nouvelle édition augmentée de l’ouvrage de référence <a href="https://r4ds.had.co.nz/"><em>R For Data Science</em></a>.</p>
</section>
<section id="observable-devient-un-incontournable-dans-le-monde-de-la-dataviz" class="level1">
<h1><code>Observable</code> devient un incontournable dans le monde de la dataviz</h1>
<p>Pour permettre des visualisations interactives, cela fait plusieurs années que <code>JavaScript</code> est un incontournable et que le <a href="https://fr.wikipedia.org/wiki/WebAssembly"><em>web assembly</em></a> retient <a href="https://twitter.com/solomonstre/status/1111004913222324225">de plus en plus d’attention</a>.</p>
<p>Les journaux traditionnels utilisent ainsi de plus en plus le <em>data scrollytelling</em> , cette technique de narration qui consiste à présenter des informations sous forme de récit interactif, en utilisant une combinaison de texte et de graphiques qui apparaissent et disparaissent en fonction des actions du lecteur. L’un des exemples les plus réussis des dernières années a sans doute été la visualisation du <a href="https://www.nytimes.com/interactive/2020/03/22/world/coronavirus-spread.html"><em>New York Times</em> <em>“How the virus got out”</em></a>. Cette approche a également été adoptée par le Ministère de l’Agriculture pour diffuser les chiffrés clés du <a href="https://vizagreste.agriculture.gouv.fr/">recensement agricole</a>. Nos voisins anglais ne sont pas en reste puisque les derniers résultats du recensement sont proposés sur un <a href="https://www.ons.gov.uk/census/maps/choropleth/population/age/resident-age-3a/aged-16-to-64-years">site web remarquable de fluidité</a>.</p>
<p>Afin de permettre une diffusion accrue de visualisations en <code>JavaScript</code>, <a href="https://en.wikipedia.org/wiki/Mike_Bostock">Mike Bostock</a>, déjà créateur de la librairie de dataviz de référence <a href="https://en.wikipedia.org/wiki/D3.js"><code>D3.js</code></a>, est à l’origine de la plateforme <a href="https://observablehq.com/"><code>observable</code></a>, sorte de <code>Github</code> de la <em>dataviz</em> permettant du partage et de la réutilisation de <em>notebooks</em> réactifs. En cette année 2022, la plateforme a connu un véritable <em>boom</em> et est devenu un incontournable dans le domaine. L’une des raisons est l’ajout de fonctionalités qui permettent d’étendre le public cible au delà des développeurs <em>web</em>, déjà accoutumés à <code>Javascript</code>. Parmi les fonctionalités les plus remarquables, la possibilité depuis Novembre d’utiliser des <a href="https://observablehq.com/@ericmauviere/duckdb-redonne-nouvelle-vie-sql">requêtes SQL</a> grâce à <a href="https://observablehq.com/@observablehq/duckdb"><code>DuckDB</code></a> permet aux habitués de <code>R</code> ou de <code>Python</code> de retrouver des manipulations auxquels ils sont habitués. La librairie <a href="https://observablehq.com/@observablehq/plot"><code>Plot</code></a> offre une <a href="https://observablehq.com/@observablehq/plot-from-ggplot2">grammaire proche de <code>ggplot2</code></a>.</p>
<p>La communauté des cartographes a été particulièrement active sur <code>Observable</code>, notamment à l’occasion du <a href="https://observablehq.com/collection/@observablehq/30-day-map-challenge">#30daymapchallenge</a>. <a href="https://observablehq.com/@neocartocnrs">Nicolas Bertin (<code>neocarto</code>)</a>, dont on ne peut que recommander l’<a href="../../talk/presentation-dobservable-par-nicolas-lambert/">introduction à <code>Observable</code></a> faite pour le réseau, ou <a href="https://observablehq.com/@ericmauviere">Eric Mauvière</a> font partie des comptes à suivre dans la communauté francophone.</p>
<p><code>Observable</code>, en tant que langage construit sur <code>JavaScript</code>, est également disponible pour les utilisateurs de <a href="https://quarto.org/docs/interactive/ojs/"><code>Quarto</code></a>, ce qui permet de mettre à disposition des visualisation réactives sans passer nécessairement par la plateforme <a href="https://observablehq.com/">observablehq.com</a> pour mettre à disposition des visualisations réactives, ce qui constitue une alternative intéressante aux applications qui nécessitent un serveur en arrière plan, comme <code>Shiny</code> ou <code>Dash</code>.</p>
<p><img src="https://ssphub.netlify.app/post/retrospective2022/observable1.png" class="img-fluid"> <img src="https://ssphub.netlify.app/post/retrospective2022/observable2.png" class="img-fluid"></p>
</section>
<section id="les-autres-actualités-en-france" class="level1">
<h1>Les autres actualités en France</h1>
<p>Le rapport du conseil d’État pour la <a href="https://www.conseil-etat.fr/content/download/175739/file/Etude%20%C3%A0%20la%20demande%20du%20PM%20-%20IA%20et%20action%20publique.pdf">construction d’une IA de “confiance”</a> a donc été publié en une année 2022 où les avancées techniques des dernières années commencent à être accessibles grâce à des outils plus grand public, ce qui va nécessairement soulever des enjeux éthiques et juridiques.</p>
<p>Le <a href="https://www.onyxia.sh/">projet <code>Onyxia</code></a>, qui vise à proposer une infrastructure de <em>data science</em> à l’état de l’art pour <em>data scientists</em>, a organisé son deuxième <em>Openlab</em>. L’occasion de revenir sur le projet, sa philosophie, ses dernières avancées mais aussi d’échanger sur les perspectives de réutilisation dans de multiples environnements et de nouer des partenariats qui permettront au projet de grandir encore en 2023.</p>
<!-----
- onyxia
- IA: rapport conseil d'Etat: https://www.conseil-etat.fr/content/download/175739/file/Etude%20%C3%A0%20la%20demande%20du%20PM%20-%20IA%20et%20action%20publique.pdf + UNECE Adasmm
- Ryanair v lastminute.com, j’ai grossièrement résumé le cas page 85 dans le support
- Data scientist 10 ans après
------->


</section>

 ]]></description>
  <category>NLP</category>
  <category>Retrospective</category>
  <category>Observable</category>
  <category>Quarto</category>
  <category>Deep learning</category>
  <category>Insee</category>
  <category>Retrospective</category>
  <category>Infolettre</category>
  <guid>https://ssphub.netlify.app/post/retrospective2022/</guid>
  <pubDate>Sat, 31 Dec 2022 00:00:00 GMT</pubDate>
  <media:content url="https://ssphub.netlify.app/post/retrospective2022/featured.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Le plongement lexical ou comment apprendre à lire à un ordinateur</title>
  <link>https://ssphub.netlify.app/post/embedding/</link>
  <description><![CDATA[ 





<p>Avec le développement de la collecte automatisée d’information numérique, les données textuelles sont devenues omniprésentes, que ce soit sous la forme d’e-mails, de réponses à des enquêtes, d’articles de presse ou encore de commentaires sur les réseaux sociaux. Ces données peuvent être une source très riche d’informations mobilisable par les statisticiens, pour peu qu’ils parviennent à en faire un traitement statistique. Ainsi, une problématique récurrente dans la statistique publique consiste à classer des informations formulées en langage courant (professions, noms de produits, noms de communes, etc.) dans des nomenclatures standardisées (PCS<sup>1</sup>, NAF<sup>2</sup>, COG<sup>3</sup>…).</p>
<p>Or, le traitement des données textuelles pose une difficulté particulière: <strong>le langage naturel n’a pas de sens pour un ordinateur !</strong> Un ordinateur ne travaille qu’avec des nombres, et ne peut pas manipuler directement des mots, des expressions ou des phrases. C’est pourquoi de multiples méthodes ont été développées au cours des dernières décennies pour proposer des solutions génériques permettant de traiter des corpus de données textuelles à la fois peu structurés et hétérogènes. Cet ensemble de méthodes de traitement automatisé du langage, plus connues sous l’acronyme <code>NLP</code> (<em>natural langage processing</em>) constituent encore aujourd’hui un champ de recherche particulièrement actif.</p>
<p>Ce billet de blog n’a pas l’ambition de proposer un aperçu des méthodes de NLP, mais simplement de présenter deux méthodes fréquemment utilisées pour transformer l’information textuelle pour la rendre compréhensible et utilisable par une machine:</p>
<ul>
<li>le sac de mots (<em>bag of words</em>),</li>
<li>le plongement lexical (<em>word embedding</em>).</li>
</ul>
<section id="traiter-un-texte-comme-une-information-numérique-les-approches-possibles" class="level2">
<h2 class="anchored" data-anchor-id="traiter-un-texte-comme-une-information-numérique-les-approches-possibles">Traiter un texte comme une information numérique : les approches possibles</h2>
<section id="bag-of-words" class="level3">
<h3 class="anchored" data-anchor-id="bag-of-words">L’approche <em>bag of words</em></h3>
<p>Le principe du <em>bag of words</em> est qu’on peut décrire un document comme un dictionnaire de mots (un <em>sac de mots</em>) dans lequel on pioche plus ou moins fréquemment un terme en fonction de son nombre d’occurrences.</p>
<p>La manière la plus simple de transformer des phrases ou des libellés textuels en une information numérique est de passer par un objet que l’on appelle la <strong>matrice document-terme</strong>. L’idée est de compter le nombre de fois où les mots (les termes, en colonne) sont présents dans chaque phrase ou libellé (le document, en ligne). Cette matrice fournit alors une représentation <strong>numérique</strong> des données textuelles.</p>
<p>Considérons un corpus constitué des trois phrases suivantes :</p>
<ul>
<li>_“La pratique du tricot et du crochet_”</li>
<li><em>“Transmettre la passion du timbre”</em></li>
<li><em>“Vivre de sa passion”</em></li>
</ul>
<p>La matrice document-terme associée à ce corpus est la suivante :</p>
<table class="caption-top table">
<colgroup>
<col style="width: 29%">
<col style="width: 7%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 3%">
<col style="width: 7%">
<col style="width: 8%">
<col style="width: 3%">
<col style="width: 6%">
<col style="width: 10%">
<col style="width: 6%">
<col style="width: 5%">
</colgroup>
<thead>
<tr class="header">
<th></th>
<th style="text-align: center;">crochet</th>
<th style="text-align: center;">de</th>
<th style="text-align: center;">du</th>
<th style="text-align: center;">et</th>
<th style="text-align: center;">la</th>
<th style="text-align: center;">passion</th>
<th style="text-align: center;">pratique</th>
<th style="text-align: center;">sa</th>
<th style="text-align: center;">timbre</th>
<th style="text-align: center;">transmettre</th>
<th style="text-align: center;">tricot</th>
<th style="text-align: center;">vivre</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>La pratique du tricot et du crochet</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">2</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
</tr>
<tr class="even">
<td>Transmettre sa passion du timbre</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
</tr>
<tr class="odd">
<td>Vivre de sa passion</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">1</td>
</tr>
</tbody>
</table>
<p><strong>Mission accomplie !</strong> 🎉 Chaque phrase du corpus est associée à un vecteur numérique.</p>
<p>Il est maintenant possible de manipuler cette matrice comme des données tabulaires classiques. Par exemple, on pourrait appliquer l’un des algorithmes usuels de classification (régression logistique, forêt aléatoire, <em>gradient boosting</em>, etc.) pour classer ces phrases dans des catégories.</p>
<p>L’approche <em>bag-of-words</em> répond donc au besoin initial de transformer les données pour les rendre manipulables par une machine, en représentant les données textuelles sous la forme d’une matrice document-terme. Cette approche présente néanmoins une limite: elle traite tous les termes de façon indépendante et ne restitue pas la proximité de certains termes. Par exemple, rien dans la matrice document-terme de l’exemple précédent n’indique que les termes <em>’tricot”</em> et <em>“crochet”</em> relèvent du même champ lexical. Un autre type de représentation plus complexe et plus riche constitue souvent comme une meilleure option : le plongement lexical.</p>
</section>
<section id="embedding" class="level3">
<h3 class="anchored" data-anchor-id="embedding">Le plongement lexical</h3>
<p>Le plongement lexical (<em>word embedding</em> en anglais) consiste à projeter l’ensemble des termes qui apparaissent dans le corpus dans un espace numérique à <img src="https://latex.codecogs.com/png.latex?n"> dimensions. Chaque mot est représenté par un vecteur de taille fixe (comprenant <img src="https://latex.codecogs.com/png.latex?n"> nombres), de façon à ce que deux mots dont le sens est proche possèdent des représentations numériques proches. Ainsi les mots « chat » et « chaton » devraient avoir des vecteurs de plongement assez similaires, eux-mêmes également assez proches de celui du mot « chien » et plus éloignés de la représentation du mot « maison ».</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/embedding/word_embedding.png" class="img-fluid figure-img"></p>
<figcaption>Illustration du word embedding</figcaption>
</figure>
</div>
<div style="text-align: center">
Illustration du plongement lexical. Source : Post de blog <a href="https://medium.com/@hari4om/word-embedding-d816f643140" target="_blank">Word Embedding : Basics</a>
</div>
<p>&nbsp;</p>
<p>Chacune des <img src="https://latex.codecogs.com/png.latex?n"> composantes va encoder des informations différentes, comme le fait d’être un être vivant ou un objet, le genre, l’âge, le niveau d’abstraction, etc. C’est pour cette raison que des termes appartenant au même champ lexical auront des représentations numériquement proches. En pratique, les vecteurs de plongement ont des dizaines voire des centaines de composantes et il est impossible d’associer à chacune une interprétation univoque : toutes les notions s’entremêlent, mais chaque composante a un rôle à jouer.</p>
<p>Le plongement lexical possède deux avantages par rapport à l’approche <em>bag of words</em>. D’une part, il fournit une représentation dense des termes, qui est plus adaptée aux algorithmes d’apprentissage statistique que la représentation creuse (matrice contenant beaucoup de zéros) de l’approche <em>bag of words</em>. D’autre part, les opérations mathématiques ont un sens sur les vecteurs du plongement. C’est là la magie du plongement lexical: <strong>il devient possible de faire des mathématiques avec les mots</strong>. Ainsi par exemple, les vecteurs résultant de la différence entre les représentations des mots « femme » et « homme » d’une part, et des mots « reine » et « roi » d’autre part, devraient être proches, car conceptuellement ces couples de mots sont régis par la même relation : un changement de genre.</p>
<p>Cette formule, souvent résumée sous la forme,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ctext%7Bking%7D%20-%20%5Ctext%7Bman%7D%20+%20%5Ctext%7Bwoman%7D%20%E2%89%88%20%5Ctext%7Bqueen%7D"></p>
<p>a assuré le succès des <em>embeddings</em>, car elle permet à une machine d’appréhender les relations logiques entre les mots.</p>
<p>Jusqu’ici, nous avons parlé du plongement de mots, mais comment obtenir le plongement d’un libellé textuel ? Une possibilité est de considérer tous les mots qui composent le libellé et de calculer la moyenne de leurs vecteurs de plongement.</p>
</section>
</section>
<section id="construction-dun-plongement-lexical" class="level2">
<h2 class="anchored" data-anchor-id="construction-dun-plongement-lexical">Construction d’un plongement lexical</h2>
<p>Un plongement lexical se construit en parcourant un grand corpus de textes et en repérant les mots qui apparaissent souvent dans le même contexte. L’ensemble des articles <code>Wikipedia</code> est un des corpus de prédilection des personnes ayant construit des plongements lexicaux. Il comporte en effet des phrases complètes, contrairement à des informations issues de commentaires de réseaux sociaux, et propose des rapprochements intéressants entre des personnes, des lieux, etc.</p>
<p>Le contexte d’un mot est défini par une fenêtre de taille fixe autour de ce mot. La taille de la fenêtre est un paramètre de la construction de l’<em>embedding</em>. Le corpus fournit un grand ensemble d’exemples mots-contexte, qui peuvent servir à entraîner un réseau de neurones.</p>
<p>Plus précisément, il existe deux approches :</p>
<ul>
<li><em>Continuous bag of words</em> (CBOW), où le modèle est entraîné à prédire un mot à partir de son contexte ;</li>
<li><em>Skip-gram</em>, où le modèle tente de prédire le contexte à partir d’un seul mot.</li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/embedding/CBOW_Skipgram_training.png" class="img-fluid figure-img"></p>
<figcaption>Illustration de la différence entre les approches CBOW et Skip-gram</figcaption>
</figure>
</div>
<div style="text-align: center">
Illustration de la différence entre les approches CBOW et Skip-gram. Source : Anwarvic sur <a href="https://stackoverflow.com/questions/57507056/difference-between-max-length-of-word-ngrams-and-size-of-context-window" target="_blank">StackOverflow</a>
</div>
<p>&nbsp;</p>
<section id="algorithmes-célèbres" class="level3">
<h3 class="anchored" data-anchor-id="algorithmes-célèbres">Algorithmes célèbres</h3>
<p>La méthode de construction d’un plongement lexical présentée ci-dessus est celle de l’algorithme <a href="https://fr.wikipedia.org/wiki/Word2vec"><code>Word2Vec</code></a>. Il s’agit d’un modèle <em>open-source</em> développé par une équipe de <code>Google</code> en 2013. <code>Word2Vec</code> a été le pionnier en termes de modèles de plongement lexical.</p>
<p>Le modèle <a href="https://nlp.stanford.edu/projects/glove/"><code>GloVe</code></a> constitue un autre exemple<sup>4</sup>. Développé en 2014 à Stanford, ce modèle ne repose pas sur des réseaux de neurones mais sur la construction d’une grande matrice de co-occurrences de mots. Pour chaque mot, il s’agit de calculer les fréquences d’apparition des autres mots dans une fenêtre de taille fixe autour de lui. La matrice de co-occurrences obtenue est ensuite factorisée par une décomposition en valeurs singulières. Il est également possible de produire des plongements de mots à partir du <a href="https://jalammar.github.io/illustrated-bert/">modèle de langage <code>BERT</code></a>, développé par <code>Google</code> en 2019, dont il existe des déclinaisons dans différentes langues, notamment en Français (les modèles <a href="https://camembert-model.fr/"><code>CamemBERT</code></a> ou <a href="https://github.com/getalp/Flaubert"><code>FlauBERT</code></a>)</p>
<p>Enfin, le modèle <a href="https://fasttext.cc/"><code>FastText</code></a>, développé en 2016 par une équipe de <code>Facebook</code>, fonctionne de façon similaire à <code>Word2Vec</code> mais se distingue particulièrement sur deux points :</p>
<ul>
<li>En plus des mots eux-mêmes, le modèle apprend des représentations pour les n-grams de caractères (sous-séquences de caractères de taille \(n\), par exemple <em>« tar »</em>, <em>« art »</em> et <em>« rte »</em> sont les trigrammes du mot <em>« tarte »</em>), ce qui le rend notamment robuste aux variations d’orthographe ;</li>
<li>Le modèle a été optimisé pour que son entraînement soit particulièrement rapide.</li>
</ul>
<p>A <code>l’Insee</code>, plusieurs modèles de classification de libellés textuels dans des nomenclatures reposent sur l’algorithme de plongement lexical <a href="https://fasttext.cc/"><code>FastText</code></a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/embedding/fasttext.png" class="img-fluid figure-img"></p>
<figcaption>Illustration du modèle fastText</figcaption>
</figure>
</div>
<div style="text-align: center">
Illustration du fonctionnement du modèle fastText sur un libellé de profession
</div>
<p>&nbsp;</p>
</section>
<section id="comment-utiliser-ces-modèles-en-pratique" class="level3">
<h3 class="anchored" data-anchor-id="comment-utiliser-ces-modèles-en-pratique">Comment utiliser ces modèles en pratique ?</h3>
<p>Collecter à nouveau les données ayant servi à entrainer un modèle puis le ré-entraîner implique énormément de ressources, ce qui est coûteux en temps et peu écologique<sup>5</sup>.</p>
<p>En <code>Python</code>, plusieurs librairies proposent les modèles <code>Word2Vec</code>, <code>GloVe</code>, <code>BERT</code> ou <code>FastText</code>. Le <a href="https://radimrehurek.com/gensim/">package <code>gensim</code></a> les met toutes en œuvre à l’exception de <code>BERT</code>. Ce dernier est disponible sur <a href="https://huggingface.co/docs/transformers/model_doc/bert"><code>HuggingFace</code></a>, la principale plateforme de mise à disposition de modèles pré-entraînés. Il est ainsi possible d’utiliser <code>BERT</code> avec les librairies <a href="https://pytorch.org/hub/huggingface_pytorch-transformers/"><code>PyTorch</code></a> ou <a href="https://keras.io/examples/nlp/text_extraction_with_bert/"><code>Keras</code></a>. Chacun des modèles présentés possède également son package dédié, généralement développé par l’équipe de recherche ayant entraîné le modèle.</p>
<p>En <code>R</code>, il faut utiliser les packages <code>word2vec</code>, <code>text2vec</code> (pour le modèle GloVe) et <code>fastTextR</code>.</p>
</section>
</section>
<section id="bonus-le-plongement-lexical-en-version-ludique" class="level2">
<h2 class="anchored" data-anchor-id="bonus-le-plongement-lexical-en-version-ludique">Bonus : le plongement lexical en version ludique</h2>
<p>Le résultat d’un plongement lexical peut avoir de nombreux usages. Il rend notamment possible le calcul de la proximité entre deux mots quelconques.</p>
<p>Une manière de procéder est de calculer la similarité cosinus entre les vecteurs de plongement des deux mots. Plus précisément, la similarité entre deux mots de représentations vectorielles \(u\) et \(v\) est définie comme le cosinus de leur angle \( \) : <img src="https://latex.codecogs.com/png.latex?cos(%5Ctheta)%20=%20%5Cfrac%7Bu%20%5Ccdot%20v%7D%7B%5ClVert%20u%5CrVert%20%5ClVert%20v%5CrVert%7D"></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ssphub.netlify.app/post/embedding/similarite_cosinus.png" class="img-fluid figure-img"></p>
<figcaption>Illustration de la similarité cosinus</figcaption>
</figure>
</div>
<div style="text-align: center">
Illustration de la similarité cosinus en deux dimensions
</div>
<p>&nbsp;</p>
<p>Le calcul de la proximité entre les mots est à la base du jeu <a href="https://cemantix.certitudes.org/">cemantix</a>. Le principe est proche du jeu <code>Wordle</code> mais s’en distingue sur un point : il y a certes un mot à trouver chaque jour et il s’agit de faire des propositions de mots mais le jeu répond en donnant la proximité entre les mots proposés et le mot du jour. Ainsi, au fil des propositions, on a une vision de plus en plus précise du champ lexical associé au mot mystère, jusqu’à finalement le trouver.</p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Notes de bas de page</h2>

<ol>
<li id="fn1"><p>La nomenclature <code>PCS</code> (professions et catégories socioprofessionnelles) sert à la codification des professions dans le recensement et les enquêtes auprès des ménages. Elle permet ainsi de classer un ensemble de professions dans une même catégorie. Par exemple, dans sa dernière version (<a href="https://www.insee.fr/fr/information/6205305">PCS 2020</a>), la catégorie des <em>“Professions libérales de santé”</em> (31A) regroupe diverses professions médicales: médecins libéraux, dentistes, psychologues, véterinaires, pharmaciens libéraux… Une description plus complète de cette nomenclature et de son historique est disponible sur <a href="https://www.insee.fr/fr/information/6208292">le site de l’Insee</a>↩︎</p></li>
<li id="fn2"><p>La <code>NAF</code> (nomenclature d’activités française), est une nomenclature des activités économiques productives, principalement élaborée pour faciliter l’organisation de l’information économique et sociale. Il s’agit d’une typologie facilitant la représentation de l’économie sous forme de secteurs. Par exemple, au sein de l’industrie manufacturière (section C), la NAF distingue les industries alimentaires de l’industrie de l’habillement ou de l’industrie automobile. Une description plus complète de cette nomenclature et de son historique est disponible sur <a href="https://www.insee.fr/fr/information/2406147">le site de l’Insee</a>↩︎</p></li>
<li id="fn3"><p>Le Code Officiel Géographique est le référentiel permettant de relier des adresses, des noms de communes ou encore des noms de collectivités locales à un identifiant unique. Pour plus d’informations, voir le <a href="https://www.insee.fr/fr/information/5230987">site de l’Insee</a>↩︎</p></li>
<li id="fn4"><p>Jeffrey Pennington, Richard Socher, and Christopher D. Manning. 2014. GloVe: Global Vectors for Word Representation↩︎</p></li>
<li id="fn5"><p>Strubell, Ganesh, and McCallum (2019) estiment que l’entraînement d’un modèle à l’état de l’art dans le domaine du NLP nécessite autant d’énergie que ce que consommeraient cinq voitures, en moyenne, au cours de l’ensemble de leur cycle de vie.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Insee</category>
  <category>NLP</category>
  <guid>https://ssphub.netlify.app/post/embedding/</guid>
  <pubDate>Mon, 03 Oct 2022 00:00:00 GMT</pubDate>
  <media:content url="https://ssphub.netlify.app/post/embedding/featured.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
