Skip to content

Commit

Permalink
neater way to prevent code execution
Browse files Browse the repository at this point in the history
  • Loading branch information
akochari committed Jun 17, 2024
1 parent f1a591a commit 371bd33
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 59 deletions.
20 changes: 10 additions & 10 deletions FF2024/data_analysis.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,33 @@ <h3 class="anchored" data-anchor-id="loading-the-dataset">Loading the dataset</h
<h3 class="anchored" data-anchor-id="asking-general-questions">Asking general questions</h3>
<p>Now that the dataset is in LLMs memory we can ask different kinds of questions. Below are some examples of questions about the dataset or questions asking for the specific value from the dataset.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># For how many years do we have data on divorces?</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>For how many years do we have data on divorces?</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># How many divorces were registered in 1984?</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>How many divorces were registered <span class="cf">in</span> <span class="dv">1984</span>?</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># How many people emigrated in 2023?</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>How many people emigrated <span class="cf">in</span> <span class="dv">2023</span>?</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Find the year with the fewest deaths</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>Find the year with the fewest deaths</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="asking-questions-that-require-calculations-based-on-the-provided-data" class="level3">
<h3 class="anchored" data-anchor-id="asking-questions-that-require-calculations-based-on-the-provided-data">Asking questions that require calculations based on the provided data</h3>
<p>Sometimes the answer we are looking for is not directly in the data that we have. In these cases we can ask the LLMs to do those calculations.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># When will the population reach 11 million people?</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>When will the population reach <span class="dv">11</span> million people?</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Calculate whether a higher proportion of the population marry now than 30 years ago</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>Calculate whether a higher proportion of the population marry now than <span class="dv">30</span> years ago</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="creating-plots" class="level3">
<h3 class="anchored" data-anchor-id="creating-plots">Creating plots</h3>
<p>We can also ask LLMs to create some plots to look at the data visually. It is possible to ask for a plot that will be displayed directly in the chat or alternatively for example R or Python code for a visualisation (see below under ADVANCED topics). Here is an example prompt.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Show me a bar plot of population growth over years. Plot population growth in terms percentage change from the previous year.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>Show me a bar plot of population growth over years. Plot population growth <span class="cf">in</span> terms percentage change from the previous year.</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<blockquote class="blockquote">
<p>☝️ <strong>TIP</strong>: One cool use case of LLMs is uploading an image of a plot and asking it to generate the same plot with your own data.</p>
Expand All @@ -140,12 +140,12 @@ <h3 class="anchored" data-anchor-id="advanced-generating-code-for-plots-and-dash
<p>Another way to approach visualisation is asking LLM tools to generate code for creating a visualisation. This will require to already have at least some beginner-level knowledge of some relatively popular (only in those cases will LLMs have seen enough data to help us) coding tool for making visualizations. For example, <em>ggplot2</em> in R or <em>Plotly</em> in Python.</p>
<p>For example, we can ask the following:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Write code to produce a line plot in R that will show the population growth over the last 10 years.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a>Write code to produce a line plot <span class="cf">in</span> R that will show the population growth over the last <span class="dv">10</span> years.</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>While the generated code will be generally correct and in some cases work immediately sometimes you will need to take a look and make corrections.</p>
<p>If we want to go even further in this process and create an interactive dashboard instead of a static plot, we can also try to ask for that.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Now turn this plot into an interactive dashboard where the user can select any of the columns to replace the data in the Y axis.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>Now turn this plot into an interactive dashboard where the user can select any of the columns to replace the data <span class="cf">in</span> the Y axis.</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<blockquote class="blockquote">
<p>☝️ <strong>TIP</strong>: When it comes to writing code it is often useful to not ask for everything in one go but to build the complex function or visualization as in this case step-by-step.</p>
Expand All @@ -155,7 +155,7 @@ <h3 class="anchored" data-anchor-id="advanced-generating-code-for-plots-and-dash
</blockquote>
<p>In this case the LLM is probably going to generate code for an R Shiny dashboard. Again, it may not work immediately but might will give a good starting point. Already knowing how to build an R Shiny dashboard is useful here because it allows to spot and correct the errors. We can fix the errors ourselves or ask the LLM to correct them, for example. as shown in the following prompt.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># When I select columns containing a space in the column names I see an error message. Correct the code to avoid this error.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>When I select columns containing a space <span class="cf">in</span> the column names I see an error message. Correct the code to avoid this error.</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="challenges" class="level3">
Expand Down
23 changes: 13 additions & 10 deletions FF2024/data_analysis.qmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
format: html
editor: visual
execute:
eval: false
---

[Exercises](index.html) / [Exercise session 2](exercise_session_2.html) /
Expand Down Expand Up @@ -30,39 +32,40 @@ In our case you can either [copy the dataset from the webpage](https://www.scb.s
Now that the dataset is in LLMs memory we can ask different kinds of questions. Below are some examples of questions about the dataset or questions asking for the specific value from the dataset.

```{R}
# For how many years do we have data on divorces?
#| warning: false
For how many years do we have data on divorces?
```

```{R}
# How many divorces were registered in 1984?
How many divorces were registered in 1984?
```

```{R}
# How many people emigrated in 2023?
How many people emigrated in 2023?
```

```{R}
# Find the year with the fewest deaths
Find the year with the fewest deaths
```

### Asking questions that require calculations based on the provided data

Sometimes the answer we are looking for is not directly in the data that we have. In these cases we can ask the LLMs to do those calculations.

```{R}
# When will the population reach 11 million people?
When will the population reach 11 million people?
```

```{R}
# Calculate whether a higher proportion of the population marry now than 30 years ago
Calculate whether a higher proportion of the population marry now than 30 years ago
```

### Creating plots

We can also ask LLMs to create some plots to look at the data visually. It is possible to ask for a plot that will be displayed directly in the chat or alternatively for example R or Python code for a visualisation (see below under ADVANCED topics). Here is an example prompt.

```{R}
# Show me a bar plot of population growth over years. Plot population growth in terms percentage change from the previous year.
Show me a bar plot of population growth over years. Plot population growth in terms percentage change from the previous year.
```

> ☝️ **TIP**: One cool use case of LLMs is uploading an image of a plot and asking it to generate the same plot with your own data.
Expand All @@ -74,15 +77,15 @@ Another way to approach visualisation is asking LLM tools to generate code for c
For example, we can ask the following:

```{R}
# Write code to produce a line plot in R that will show the population growth over the last 10 years.
Write code to produce a line plot in R that will show the population growth over the last 10 years.
```

While the generated code will be generally correct and in some cases work immediately sometimes you will need to take a look and make corrections.

If we want to go even further in this process and create an interactive dashboard instead of a static plot, we can also try to ask for that.

```{R}
# Now turn this plot into an interactive dashboard where the user can select any of the columns to replace the data in the Y axis.
Now turn this plot into an interactive dashboard where the user can select any of the columns to replace the data in the Y axis.
```

> ☝️ **TIP**: When it comes to writing code it is often useful to not ask for everything in one go but to build the complex function or visualization as in this case step-by-step.
Expand All @@ -92,7 +95,7 @@ If we want to go even further in this process and create an interactive dashboar
In this case the LLM is probably going to generate code for an R Shiny dashboard. Again, it may not work immediately but might will give a good starting point. Already knowing how to build an R Shiny dashboard is useful here because it allows to spot and correct the errors. We can fix the errors ourselves or ask the LLM to correct them, for example. as shown in the following prompt.

```{R}
# When I select columns containing a space in the column names I see an error message. Correct the code to avoid this error.
When I select columns containing a space in the column names I see an error message. Correct the code to avoid this error.
```

### 💪 Challenges
Expand Down
Loading

0 comments on commit 371bd33

Please sign in to comment.