14 Using a Chatbot
A chatbot is the simplest way to use AI, and for most of what you will do in this course it is also the most useful. You open a web page, type a question and get an answer. There is nothing to install and nothing to configure.
The chatbot we will use in this chapter is Microsoft Copilot, which is available free to USask students (access through PAWS under the channel name ‘AI Tools’). But everything that we cover in this chapter works just as well in Claude or ChatGPT if you prefer one of those.
The one thing to keep in mind throughout: a chatbot only knows what you put in the conversation. It cannot see your files, your script, your data or your error message unless you paste them in. Most of the difference between a useless answer and a good one comes from what you provided, not from how you phrased the question.
14.1 Give it your context first
Chatbots are trained on the internet, so they know a lot about R and statistics. But they do not know what you are doing in this course, what coding program and packages you are using, what datasets you are using, or how you like your code structured. You have to tell them.
You can type this into every conversation you start, but it is easier to keep all this information in a file and paste it into a new conversation. Once you paste this into a conversation, the chatbot will remember it for the rest of that conversation. That is why it will be good for you to maintain a single conversation with the chatbot for the whole term, rather than starting a new one each time you have a question.
A README.md file is a good place to keep all the information that you want to share with the chatbot. It is a plain text file – the .md is for Markdown, where # marks a heading – and you can open it in any editor. Here is a suggested template:
# AREC 261
I am a student in AREC 261 Agricultural Data Analytics. In this class we
work with agricultural datasets to learn data analysis, data visualization
and data communication.
## Writing code
I am using R, in Positron. When you write R code, use the tidyverse package with pipes. Include a comment before each line so that I can follow what each step does.
I am new to R. Explain things rather than just giving me the answer.
## Packages
In addition to the tidyverse package, we will be loading data using the openmeteo package, and the cansim package for downloads from Statistics Canada.
## Datasets
I am working with Saskatchewan crop data: yields by rural municipality,
and variety trial results.Save this .md in your project folder. When you start a conversation about your code, paste it in first.
Keep updating this file with more instructions as you learn more about what you want from the chatbot. For example, if you find that you want your code to be in a certain style, or that you want the chatbot to explain things in a certain way, add that to the README.
14.2 Explaining an error
This is how most people first use a chatbot, and it is the use that will save you the most time. R’s error messages are written for people who already know R.
Say you run this and it stops:
rm_yields |>
filter(Crop == "Canola") |>
summarise(mean_yield = mean(yeild))Error in `summarise()`:
i In argument: `mean_yield = mean(yeild)`
Caused by error:
! object 'yeild' not found
Paste the whole thing – the code and the error. Not a description of the error, and not a screenshot if you can avoid it, because the text is what the model reads best.
I got this error in R. What does it mean and how do I fix it?
[paste your code]
[paste the error]
The answer here is that yeild is a typo for yield. You would have found that one yourself eventually. The real value shows up with messages like object of type 'closure' is not subsettable or argument is not numeric or logical: returning NA, which tell you almost nothing until someone translates them.
Ask for the translation, not just the patch. “What does this mean” teaches you the error; “fix my code” gets you past this one and leaves you stuck on the next.
14.3 Explaining code
You will read more code than you write – in this book, in examples online, and in your own scripts three weeks after writing them. A chatbot is a patient explainer. For example, perhaps in the Module 2 test bank you saw an answer that was:
# Load the tidyverse
library(tidyverse)
# Read the long yield file
rm_yields <- read_csv("data/rm_yields_1990_2025.csv")
# (a) Recent flax means by RM, retaining RMs with at least four reports
flax_summary <- rm_yields |>
filter(Crop == "Flax" & Year >= 2020) |> # recent flax rows
group_by(RM) |> # one group per RM
summarise(mean_yield = mean(Yield), # average within the RM
n = n()) |> # observations behind it
filter(n >= 4) |> # enough years to compare
arrange(desc(mean_yield)) And you didn’t quite understand what the code was doing. You can ask the chatbot to explain it line by line.
Explain what this R code does, line by line. I am new to the tidyverse.
[paste the code]
You can then follow up with any questions that would clarify the explanation such as: “What does n() do?” or “Why is there a pipe after filter()?”.
14.4 Explaining a concept
You could also ask for the explanation of a function. For example:
Explain how pipes work in the tidyverse package.
The same applies to the statistics, not just the syntax.
What is the difference between a standard deviation and a standard error? Explain it with a farm example.
If an explanation does not land, say so and ask for another angle. “Explain it again without the formula” or “give me a case where they would be very different” usually works better than reading the same paragraph twice.
14.5 Brainstorming
Before you write any code, a chatbot is useful for working out what you are even trying to do.
I have Saskatchewan canola yields by rural municipality from 1990 to 2025. I want to say something about whether yields have become more variable over time. What are some ways I could approach this, and what are the drawbacks of each?
Ask for drawbacks and alternatives rather than the answer. You want three options you can choose between, which is a question a chatbot is well suited to, instead of one recommendation presented with more confidence than it deserves.
This is also the right moment to ask what you are missing. “What would a reviewer question about this approach?” tends to surface the assumption you had not noticed you were making.
14.6 Writing code
As I mentioned earlier, we are writing code in this class for the same reason that fourth-graders are doing long division: to learn the process, not to get the answer. Eventually, the fourth grader will divide 125,334 by 53 by using a calculator, and you will have AI write your code for you.
To get AI to write code for you, you have to give it the same information that you would give a human. The README.md file is the first helpful piece of context for the AI chatbot. But for specific questions, you will also need to give it the structure of your data and what you want the output to look like.
For example, try putting the following two prompts into a chatbot:
I am trying to compute the average of Brandon wheat in Manitoba in 2025 using the dataset mb_wheat_reported_2020_2025.csv. The data looks like the dataset mb_wheat_reported_2020_2025.csv.
versus
I am trying to compute the average of Brandon wheat in Manitoba in 2025 using the dataset mb_wheat_reported_2020_2025.csv. The data looks like the dataset mb_wheat_reported_2020_2025.csv. Here is my code so far and a glimpse of the data:
> library(tidyverse)
+
+ mb_wheat <- read_csv("data/mb_wheat_reported_2020_2025.csv")
+
+ glimpse(mb_wheat)
Rows: 2398 Columns: 7
── Column specification ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Municipality, Variety
dbl (4): Year, Farms, Acres, Yield_bu_ac
lgl (1): Reported
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 2,398
Columns: 7
$ Year <dbl> 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, …
$ Municipality <chr> "ALEXANDER", "ALEXANDER", "ALONSA", "ARGYLE", "ARGYLE", "ARGYLE", "ARMSTRONG", "BIFROST-RIVERTON", "BIFROST-RIVERTON", "BIFROST-RIVERTON",…
$ Variety <chr> "AAC BRANDON (BW 932)", "AAC VIEWFIELD <FP GENETICS>|BW965| EXP", "AAC BRANDON (BW 932)", "AAC BRANDON (BW 932)", "AAC REDBERRY", "CARDALE…
$ Farms <dbl> 5, 3, 11, 67, 7, 3, 9, 71, 12, 3, 3, 45, 6, 8, 3, 3, 3, 48, 3, 22, 4, 35, 14, 25, 6, 4, 49, 3, 41, 7, 9, 6, 8, 4, 27, 64, 5, 5, 12, 16, 11…
$ Acres <dbl> 1411.0, 1448.0, 5732.0, 28908.0, 1590.0, 537.0, 1790.0, 26811.0, 5035.0, 782.0, 710.0, 23424.0, 3739.0, 1634.0, 701.0, 1473.0, 745.0, 3256…
$ Yield_bu_ac <dbl> 50.3, 73.1, 65.1, 59.4, 47.8, 30.0, 58.6, 68.5, 79.1, 72.3, 77.3, 71.5, 74.2, 68.3, 69.5, 67.6, 75.7, 64.7, 63.0, 62.0, 64.7, 65.2, 68.9, …
$ Reported <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, …
>
When I tried the first prompt the AI gave me the following code:
# Load the tidyverse package
library(tidyverse)
# Read the wheat dataset
mb_wheat <- read_csv("data/mb_wheat_reported_2020_2025.csv")
# Filter to Brandon in 2025 and calculate the average yield
mb_wheat |>
# Keep only Brandon records from 2025
filter(Location == "Brandon", Year == 2025) |>
# Calculate the average yield
summarise(mean_yield = mean(Yield, na.rm = TRUE))Notably – it guessed at the columns in the data, and it thought that I wanted the average wheat yield in the location of Brandon, MB – not the variety AAC Brandon.
Based on the second prompt, I received this code:
# Load the tidyverse
library(tidyverse)
# Read the dataset
mb_wheat <- read_csv("data/mb_wheat_reported_2020_2025.csv")
# Calculate the average Brandon wheat yield in 2025
mb_wheat |>
# Keep only 2025 observations
filter(Year == 2025) |>
# Keep only AAC Brandon wheat records
filter(str_detect(Variety, "BRANDON")) |>
# Calculate the average yield
summarise(mean_yield = mean(Yield_bu_ac, na.rm = TRUE))which actually ran and gave me a sensible answer. Note that this is the unweighted average across municipalities. If I had wanted the average weighted by acres – a different and often more useful number – I would have had to say so.
14.7 Writing with AI
Using AI tools in writing is a controversial topic and I strongly encourage you to review the University’s AI policies for students. Personally, I think not using AI to help improve your writing is like not using spell check or a calculator to check your math. In many ways, you can use AI to help you write in the same way you use AI to help you code.
My own personal workflow for writing is:
- Brainstorm with AI about topics and ideas.
- Create an outline with the help of AI.
- Have AI write a draft based on this outline, filling in details etc. Often this brings in more ideas and information that I had not thought of.
- Write my own draft.
- Work with AI to edit and improve my draft, checking for grammar, spelling, and clarity.
Within these five steps there is a lot of back-and-forth with AI. For example, I might not like how a sentence sounds and I will ask the chatbot for three alternative versions of the sentence.
Other times, I will ask AI to diagnose why a sentence sounds awkward. This reveals issues about my own writing style that I was not aware of. I think this is particularly helpful for students.
14.8 What not to put in
The data we use in this course is public or synthetic, so you can paste it freely. That will not always be true. Do not paste confidential, personal or proprietary data into a chatbot unless the system has been approved for it. Where you are unsure, paste the column headers and a few made-up rows – that is almost always enough for the assistant to write the code you need.
Your @usask.ca Copilot account operates under the university’s agreement with Microsoft, which is a different arrangement from a personal consumer account. That is a reason to use the university account for coursework, not a reason to treat any chatbot as a safe place for sensitive data.
14.9 Other resources – chatbots
- Microsoft Copilot – through PAWS, under the channel ‘AI Tools’
- Claude and ChatGPT – the same techniques apply