From 67709819ab73b54e58187006062a64f92b57ac53 Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 00:23:41 -0400 Subject: [PATCH 1/8] reformatting --- .../30-ai-tutorial-test/tutorial.Rmd | 249 +++++++----------- 1 file changed, 89 insertions(+), 160 deletions(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 7d597a61..8e5a8f5b 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -42,10 +42,9 @@ options(tutorial.exercise.timelimit = 600, This tutorial covers [Chapter 12: Logical vectors](https://r4ds.hadley.nz/logicals.html), [Chapter 13: Numbers](https://r4ds.hadley.nz/numbers.html), [Chapter 14: Strings](https://r4ds.hadley.nz/strings.html), [Chapter 15: Regular expressions](https://r4ds.hadley.nz/regexps.html), and [Chapter 16: Factors](https://r4ds.hadley.nz/factors.html), from [*R for Data Science (2e)*](https://r4ds.hadley.nz/) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund. - + -You will learn how to parse numbers, count values and variables, find minimua and maxima of vectors, and round values with the commands [`parse_double()`](https://readr.tidyverse.org/reference/parse_atomic.html), [`parse_number()`](https://readr.tidyverse.org/reference/parse_number.html), [`count()`](https://dplyr.tidyverse.org/reference/count.html), [`pmin()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html), -[`round()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html), and [`min_rank()`](https://dplyr.tidyverse.org/reference/row_number.html). +You will learn about how to work with numbers using commands such as [`pmin()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html) with the **nycflights13** package. You will learn about some of the power of the [**stringr**](https://stringr.tidyverse.org/) package: how to create, combine, and extract strings with the **babynames** package. @@ -57,34 +56,9 @@ Connect the repo to a project on your computer using `File -> New Folder from Gi You need two Positon windows: this one for running the tutorial and the one you just created for writing your code and interacting with the Console. -Paste the link to your Github repo down below. +Select `File -> New File -> Quarto Document ...`. Provide a title -- `"Transforming Data"` -- and an author (you). Render the document and save it as `analysis.qmd`. -```{r introduction-1} -question_text(NULL, - answer(NULL, correct = TRUE), - allow_retry = TRUE, - try_again_button = "Edit Answer", - incorrect = NULL, - rows = 3) -``` - -### - -Professionals keep their data science work in the cloud because laptops fail. - - - - -## Plotting **flights** -### - - - -### Exercise 1 - -Select `File -> New File -> Quarto Document ...`. Provide a title -- `"Flights"` -- and an author (you). Render the document and save it as `analysis1.qmd`. - -Create a `.gitignore` file with `analysis1_files` on the first line and then a blank line. Save and push. +Create a `.gitignore` file with `analysis_files` on the first line and then a blank line. Save and push. In the Console, run: @@ -95,8 +69,7 @@ show_file(".gitignore") If that fails, it is probably because you have not yet loaded `library(tutorial.helpers)` in the Console. CP/CR. - -```{r numbers-1} +```{r introduction-1} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -105,15 +78,13 @@ question_text(NULL, rows = 3) ``` -### - +### +Professionals keep their data science work in the cloud because laptops fail. ### Exercise 2 - - - + In your QMD, put `library(tidyverse)` in a new code chunk. Render the file. @@ -127,12 +98,12 @@ execute: In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. -```{r numbers-2} +```{r introduction-2} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -152,7 +123,7 @@ Note that this causes `library(tidyverse)` to be copied down to the Console and CP/CR. -```{r numbers-3} +```{r introduction-3} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -164,22 +135,33 @@ question_text(NULL, ### + -### Exercise 4 +## Plotting **flights** +### -Load `nycflights13` in your QMD with the `library()` function. + - +This section covers [Chapter 13: Numbers](https://r4ds.hadley.nz/numbers.html) from [*R for Data Science (2e)*](https://r4ds.hadley.nz/) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund. We will be utilizing two core packages of Tidyverse, [**readr**](https://readr.tidyverse.org/) and [**dplyr**](https://dplyr.tidyverse.org/). Key commands of this section will include [`parse_double()`](https://readr.tidyverse.org/reference/parse_atomic.html) for parsing numbers directly from strings, [`parse_number()`](https://readr.tidyverse.org/reference/parse_number.html) for removing useless characters and parsing numbers from strings, [`count()`](https://dplyr.tidyverse.org/reference/count.html) which counts the unique values of one or more variables, [`pmin()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html) which take one or more vectors in and returns the minima or maxima of these vectors, +[`round()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html) which rounds values in its first argument to the specified number of decimal places, and [`min_rank()`](https://dplyr.tidyverse.org/reference/row_number.html) which gives every tie the same value and ranks an inputted vector. + + +### Exercise 3 + +In the same setup chunk with **tidyverse**, load `nycflights13` with the `library()` function. + +Place your cursor on that line and run `Cmd/Ctrl + Enter` to load the package in the Console. + In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. -```{r numbers-4} +```{r numbers-3} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -192,11 +174,11 @@ question_text(NULL, This dataset from the contains information about all flights that departed from NYC (e.g. EWR, JFK and LGA) to destinations in the United States, Puerto Rico, and the American Virgin Islands) in 2013. -### Exercise 5 +### Exercise 4 Type `flights` in the Console to view the dataset. Copy and paste the resulting tibble. -```{r numbers-5} +```{r numbers-4} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -209,22 +191,50 @@ question_text(NULL, Typing out `flights` is the same as typing out `print(flights)`. You might be wondering what tibble is, A tibble is a new form of a data frame in R that is part of the tidyverse library, and Tibbles print the data in a more efficient format than a data frame, showing the values of the columns, their datatype, and the size of the dataset. +### Exercise 5 +Now, ask your favorite AI to use `select()` on `flights` to obtain only the columns `dep_time`, `sched_dep_time`, and `dep_delay`. +Make sure to provide a tibble of **flights** for the AI to use. + +Assign the result of this pipe to `flights_clean`, and then add your code to a new chunk in the QMD. +Place your cursor on that line and run `Cmd/Ctrl + Enter` to load `flights_clean` in the Console. +Then, type `flights_clean` in the Console and hit `Enter`. + +CP/CR the resulting tibble. + +```{r numbers-5} +question_text(NULL, + answer(NULL, correct = TRUE), + allow_retry = TRUE, + try_again_button = "Edit Answer", + incorrect = NULL, + rows = 3) +``` + +### + +Our code: + +```{r, echo=TRUE} +flights_clean <- flights |> + select(dep_time:dep_delay) +flights_clean +``` ### Exercise 6 - -Using your favorite AI, prompt it to generate R code that takes `flights` and breaks scheduled and actual departure times into hours and minutes using %/% and %%. Make sure to provide a tibble of **flights** for the AI to use. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in a new chunk. Save the file. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. +Next, prompt the AI to generate R code that takes `flights` and breaks scheduled and actual departure times into hours and minutes using %/% and %%. If you have not done so before, provide a tibble of **flights** for the AI to use. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in a new chunk. Save the file. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -244,8 +254,7 @@ question_text(NULL, Our code: ```{r, echo=TRUE} -library(nycflights13) -flights |> +flights_clean |> mutate( sched_hour = sched_dep_time %/% 100, sched_minute = sched_dep_time %% 100, @@ -261,7 +270,7 @@ Next, prompt the AI to generate R code that continues the pipe and adds a new co In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -281,7 +290,7 @@ question_text(NULL, Our code: ```{r, echo=TRUE} -flights |> +flights_clean |> mutate( sched_hour = sched_dep_time %/% 100, sched_minute = sched_dep_time %% 100, @@ -298,7 +307,7 @@ If necessary, change the code so that the result of the pipe is not assigned to In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -318,7 +327,7 @@ question_text(NULL, Our code: ```{r, echo=TRUE} -flights |> +flights_clean |> mutate( sched_hour = sched_dep_time %/% 100, sched_minute = sched_dep_time %% 100, @@ -335,7 +344,7 @@ Now assign the pipe to the variable `x`. Place your cursor on the first line of In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -355,7 +364,7 @@ question_text(NULL, Our code: ```{r, echo=TRUE} -x <- flights |> +x <- flights_clean |> mutate( sched_hour = sched_dep_time %/% 100, sched_minute = sched_dep_time %% 100, @@ -372,7 +381,7 @@ Now that we have filtered our dataset through a pipe, we need to be on track wit In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -400,7 +409,7 @@ Within `labs()` edit or add a proper title, subtitle, and caption. If axis label In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -420,7 +429,7 @@ question_text(NULL, Our code: ```{r echo=FALSE} -x <- flights |> +x <- flights_clean |> mutate( sched_hour = sched_dep_time %/% 100, sched_minute = sched_dep_time %% 100, @@ -520,7 +529,7 @@ question_text(NULL, Our code: ```{r echo=TRUE} -y <- flights |> +y <- flights_clean |> mutate( sched_hour = sched_dep_time %/% 100, sched_minute = sched_dep_time %% 100, @@ -545,7 +554,7 @@ Now that we have all these functions in the same code cell, we can see all of th In the Console, run: ``` -show_file("analysis1.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -566,7 +575,7 @@ question_text(NULL, Publish your rendered QMD to GitHub Pages. In the Terminal --- not the Console! --- run: ```` -quarto publish gh-pages analysis1.qmd +quarto publish gh-pages analysis.qmd ```` Copy/paste the resulting URL below. @@ -588,101 +597,21 @@ Commit/push everything. ## Plotting **babynames** ### -In this section we are going to focus on generating code to create a graph based on a dataset called `babynames`. The `babynames` is a dataset which contained names used for American babies from 1880 to 2017. If you want to explore related data, checkout [popular baby names](https://www.ssa.gov/oact/babynames/limits.html). We will be using AI to generate the code for us and then we will break down the functions within the code and how they work. - - -### Exercise 1 - -Select `File -> New File -> Quarto Document ...`. Provide a title -- `"Baby Names"` -- and an author (you). Render the document and save it as `analysis2.qmd`. - -Create a `.gitignore` file with `analysis2_files` on the first line and then a blank line. Save and push. - -In the Console, run: +This section covers [Chapter 14: Strings](https://r4ds.hadley.nz/strings.html) from [*R for Data Science (2e)*](https://r4ds.hadley.nz/) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund. +You will learn about some of the power of the [**stringr**](https://stringr.tidyverse.org/) package: how to create, combine, and extract strings, and about some of the challenges you might face with non-English strings. Some important functions which we will learn include: +[`str_c()`](https://stringr.tidyverse.org/reference/str_c.html), +[`str_glue()`](https://stringr.tidyverse.org/reference/str_glue.html), +[`str_flatten()`](https://stringr.tidyverse.org/reference/str_flatten.html), +[`separate_longer_delim()`](https://tidyr.tidyverse.org/reference/separate_longer_delim.html), and more. -``` -show_file(".gitignore") -``` - -If that fails, it is probably because you have not yet loaded `library(tutorial.helpers)` in the Console. - -CP/CR. - -```{r strings-1} -question_text(NULL, - answer(NULL, correct = TRUE), - allow_retry = TRUE, - try_again_button = "Edit Answer", - incorrect = NULL, - rows = 3) -``` - -### - -Professionals keep their data science work in the cloud because laptops fail. - - -### Exercise 2 - -In your QMD, put `library(tidyverse)` in a new code chunk. Render the file. - -Notice that the file does not look good because the code is visible and there are annoying messages. To take care of this, add `#| message: false` to remove all the messages in this `setup` chunk. Also add the following to the YAML header to remove all code echos from the HTML: - -``` -execute: - echo: false -``` - -In the Console, run: - -``` -show_file("analysis2.qmd", chunk = "Last") -``` - -CP/CR. - -```{r strings-2} -question_text(NULL, - answer(NULL, correct = TRUE), - allow_retry = TRUE, - try_again_button = "Edit Answer", - incorrect = NULL, - rows = 3) -``` - -### - - -### Exercise 3 - -Place your cursor in the QMD file on the `library(tidyverse)` line. Use `Cmd/Ctrl + Enter` to execute that line. - -Note that this causes `library(tidyverse)` to be copied down to the Console and then executed. - -CP/CR. - -```{r strings-3} -question_text(NULL, - answer(NULL, correct = TRUE), - allow_retry = TRUE, - try_again_button = "Edit Answer", - incorrect = NULL, - rows = 3) -``` - -### - -The **stringr** library is a part of the [*Tidyverse*](https://www.tidyverse.org/). We can just load the **tidyverse** library and the **stringr** library will be automatically loaded. +In this section we are going to focus on generating code to create a graph based on a dataset called `babynames`. The `babynames` is a dataset which contained names used for American babies from 1880 to 2017. If you want to explore related data, checkout [popular baby names](https://www.ssa.gov/oact/babynames/limits.html). We will be using AI to generate the code for us and then we will break down the functions within the code and how they work. ### Exercise 4 -Load `babynames` in your QMD with the `library()` function. +In the setup chunk of the QMD, load `babynames` with the `library()` function. -In the Console, run: - -``` -show_file("analysis2.qmd", chunk = "Last") -``` +Place your cursor on that line and run `Cmd/Ctrl + Enter` to load the package in the Console. CP/CR. @@ -726,7 +655,7 @@ Using your favorite AI, prompt it to generate R code that takes `babynames` and In the Console, run: ``` -show_file("analysis2.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -800,7 +729,7 @@ If your AI uses `group_by()`, get rid of it and add `.by = c(...)` as an argumen In the Console, run: ``` -show_file("analysis2.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -834,7 +763,7 @@ Now that we have filtered our dataset through a pipe, we need to be on track wit In the Console, run: ``` -show_file("analysis2.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -921,7 +850,7 @@ Within `labs()` edit or add a proper title, subtitle, and caption. If axis label In the Console, run: ``` -show_file("analysis2.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -1102,7 +1031,7 @@ In a new code chunk after the graph, copy and paste our generated code into your In the Console, run: ``` -show_file("analysis2.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -1310,7 +1239,7 @@ Now that we have all these functions in the same code cell, we can see all of th In the Console, run: ``` -show_file("analysis2.qmd", chunk = "Last") +show_file("analysis.qmd", chunk = "Last") ``` CP/CR. @@ -1329,7 +1258,7 @@ question_text(NULL, Publish your rendered QMD to GitHub Pages. In the Terminal --- not the Console! --- run: ```` -quarto publish gh-pages analysis2.qmd +quarto publish gh-pages analysis.qmd ```` Copy/paste the resulting URL below. From 894c8a5e469eadf88f0f790db6e22e34f1ecc069 Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 00:26:54 -0400 Subject: [PATCH 2/8] moved publish question --- .../30-ai-tutorial-test/tutorial.Rmd | 51 ++++++++----------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 8e5a8f5b..460e104e 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -568,31 +568,11 @@ question_text(NULL, rows = 15) ``` - - -### Exercise 16 - -Publish your rendered QMD to GitHub Pages. In the Terminal --- not the Console! --- run: - -```` -quarto publish gh-pages analysis.qmd -```` - -Copy/paste the resulting URL below. - -```{r numbers-16} -question_text(NULL, - answer(NULL, correct = TRUE), - allow_retry = TRUE, - try_again_button = "Edit Answer", - incorrect = NULL, - rows = 3) -``` - -### +### Commit/push everything. + ## Plotting **babynames** ### @@ -1253,7 +1233,22 @@ question_text(NULL, rows = 15) ``` -### Exercise 24 +### + +Commit/push everything. + + +## Summary +### + +This tutorial covered [Chapter 12: Logical vectors](https://r4ds.hadley.nz/logicals.html), [Chapter 13: Numbers](https://r4ds.hadley.nz/numbers.html), [Chapter 14: Strings](https://r4ds.hadley.nz/strings.html), [Chapter 15: Regular expressions](https://r4ds.hadley.nz/regexps.html), and [Chapter 16: Factors](https://r4ds.hadley.nz/factors.html), from [*R for Data Science (2e)*](https://r4ds.hadley.nz/) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund. + +You learned about how to work with numbers using commands such as [`pmin()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html) with the **nycflights13** package. + +You learned about some of the power of the [**stringr**](https://stringr.tidyverse.org/) package: how to create, combine, and extract strings with the **babynames** package. + + +### Exercise 1 Publish your rendered QMD to GitHub Pages. In the Terminal --- not the Console! --- run: @@ -1263,7 +1258,7 @@ quarto publish gh-pages analysis.qmd Copy/paste the resulting URL below. -```{r strings-24} +```{r summary-1} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -1277,18 +1272,14 @@ question_text(NULL, Commit/push everything. -## Summary -### - -This tutorial covered [Chapter 12: Logical vectors](https://r4ds.hadley.nz/logicals.html), [Chapter 13: Numbers](https://r4ds.hadley.nz/numbers.html), [Chapter 14: Strings](https://r4ds.hadley.nz/strings.html), [Chapter 15: Regular expressions](https://r4ds.hadley.nz/regexps.html), and [Chapter 16: Factors](https://r4ds.hadley.nz/factors.html), from [*R for Data Science (2e)*](https://r4ds.hadley.nz/) by Hadley Wickham, Mine Çetinkaya-Rundel, and Garrett Grolemund. -You learned about some of the power of the [**stringr**](https://stringr.tidyverse.org/) package: how to create, combine, and extract strings with the **babynames** package. - ```{r download-answers, child = system.file("child_documents/download_answers.Rmd", package = "tutorial.helpers")} ``` ``` +STRING KNOWLEDGE DROPS + ### 1 You might be wondering what tibble is, A tibble is a new form of a data frame in R that is part of the tidyverse library, and Tibbles print the data in a more efficient format than a data frame, showing the values of the columns, their datatype, and the size of the dataset. From a5d5a3da982678ba10c7df80b0e7a15dca95dc5f Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 00:31:55 -0400 Subject: [PATCH 3/8] question numbers --- .../30-ai-tutorial-test/tutorial.Rmd | 132 +++++++++--------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 460e104e..9cd8469b 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -146,7 +146,7 @@ This section covers [Chapter 13: Numbers](https://r4ds.hadley.nz/numbers.html) f [`round()`](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Round.html) which rounds values in its first argument to the specified number of decimal places, and [`min_rank()`](https://dplyr.tidyverse.org/reference/row_number.html) which gives every tie the same value and ranks an inputted vector. -### Exercise 3 +### Exercise 1 In the same setup chunk with **tidyverse**, load `nycflights13` with the `library()` function. @@ -161,7 +161,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-3} +```{r numbers-1} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -174,11 +174,11 @@ question_text(NULL, This dataset from the contains information about all flights that departed from NYC (e.g. EWR, JFK and LGA) to destinations in the United States, Puerto Rico, and the American Virgin Islands) in 2013. -### Exercise 4 +### Exercise 2 Type `flights` in the Console to view the dataset. Copy and paste the resulting tibble. -```{r numbers-4} +```{r numbers-2} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -191,7 +191,7 @@ question_text(NULL, Typing out `flights` is the same as typing out `print(flights)`. You might be wondering what tibble is, A tibble is a new form of a data frame in R that is part of the tidyverse library, and Tibbles print the data in a more efficient format than a data frame, showing the values of the columns, their datatype, and the size of the dataset. -### Exercise 5 +### Exercise 3 Now, ask your favorite AI to use `select()` on `flights` to obtain only the columns `dep_time`, `sched_dep_time`, and `dep_delay`. Make sure to provide a tibble of **flights** for the AI to use. @@ -202,7 +202,7 @@ Then, type `flights_clean` in the Console and hit `Enter`. CP/CR the resulting tibble. -```{r numbers-5} +```{r numbers-3} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -221,7 +221,7 @@ flights_clean <- flights |> flights_clean ``` -### Exercise 6 +### Exercise 4 -### Exercise 11 +### Exercise 9 Go to the AI and ask it to generate R code that plots a clean and insightful heatmap that shows where delays tend to cluster by scheduled vs. actual departure hour. Mention you want to use the data from `x` and copy and paste the `x` you ran in the Console with the resulting tibble. You only need the top 3 lines, mainly to include column names. @@ -414,7 +414,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-11} +```{r numbers-9} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -458,13 +458,13 @@ ggplot(x, aes(x = sched_hour, y = dep_hour, fill = total_delay)) + ) ``` -### Exercise 12 +### Exercise 10 Now, using AI, ask it to generate code that rounds the `total_delay` column to the hundreds place using `round()`. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into a new code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r numbers-12} +```{r numbers-10} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -483,13 +483,13 @@ x |> mutate(total_delay = round(total_delay, -2)) ``` -### Exercise 13 +### Exercise 11 Now, using AI, ask it to generate code that applies `min_rank()` to the `total_delay` column in `x` to see the ranks of the highest total delay times per combination of scheduled departure hour and actual departure hour. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into the same code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r numbers-13} +```{r numbers-11} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -508,13 +508,13 @@ x |> mutate(rank_total_delay = min_rank(desc(total_delay))) ``` -### Exercise 14 +### Exercise 12 Now, using AI, ask it to generate code that uses summarize() in your `x` dataset to calculate the mean, median, and 95th percentile (quantile) of `true_dep_delay`. In this case, copy and paste the pipe that is assigned to `x`, change it to `y`, and then remove the `count()`. Then, run the AI generated code on `y`. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into same the code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r numbers-14} +```{r numbers-12} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -547,7 +547,7 @@ y |> ) ``` -### Exercise 15 +### Exercise 13 Now that we have all these functions in the same code cell, we can see all of the functions we have learned, and we can run any of them at will with `Cmd/Ctrl + Enter`. @@ -559,7 +559,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-15} +```{r numbers-13} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -587,7 +587,7 @@ You will learn about some of the power of the [**stringr**](https://stringr.tidy In this section we are going to focus on generating code to create a graph based on a dataset called `babynames`. The `babynames` is a dataset which contained names used for American babies from 1880 to 2017. If you want to explore related data, checkout [popular baby names](https://www.ssa.gov/oact/babynames/limits.html). We will be using AI to generate the code for us and then we will break down the functions within the code and how they work. -### Exercise 4 +### Exercise 1 In the setup chunk of the QMD, load `babynames` with the `library()` function. @@ -595,7 +595,7 @@ Place your cursor on that line and run `Cmd/Ctrl + Enter` to load the package in CP/CR. -```{r strings-4} +```{r strings-1} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -610,11 +610,11 @@ We load **babynames** into our current QMD so it is accessible to use with the r -### Exercise 5 +### Exercise 2 Type `babynames` in the Console to view the dataset. Copy and paste the resulting tibble. -```{r strings-5} +```{r strings-2} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -628,7 +628,7 @@ question_text(NULL, Typing out `babynames` is the same as typing out `print(babynames)`. You might be wondering what tibble is, A tibble is a new form of a data frame in R that is part of the tidyverse library, and Tibbles print the data in a more efficient format than a data frame, showing the values of the columns, their datatype, and the size of the dataset. -### Exercise 6 +### Exercise 3 Using your favorite AI, prompt it to generate R code that takes `babynames` and filters the dataset to show all names that have a first name starting with A. Make sure to provide a tibble of **babynames** for the AI to use. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in a new chunk. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. @@ -640,7 +640,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r strings-6} +```{r strings-3} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -665,13 +665,13 @@ babynames |> The function `str_starts()` takes a column (in this case it is `name`), and selects the rows where the string of name starts with A. To check for `NA` values, use `any()` function with argument `is.na()`, argument of which should be the dataset babynames. -### Exercise 7 +### Exercise 4 Now edit the previous code so that the letter is not `"A"`, but rather `"As"`. Place your cursor on a line of the code and run `Cmd/Ctrl + Enter`. CP/CR. -```{r strings-7} +```{r strings-4} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -698,7 +698,7 @@ Now there’s no difference in behavior between the single quotes and double quo Within the QMD, revert back to the original code with just the `"A"`. -### Exercise 8 +### Exercise 5 Next, prompt AI to continue the pipe to make a subset that shows the total proportion of babies with first names starting with A for each sex, from each year. Add the code to your QMD in the same chunk. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. @@ -714,7 +714,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r strings-8} +```{r strings-5} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -736,7 +736,7 @@ babynames |> ### -### Exercise 9 +### Exercise 6 Now that we have filtered our dataset through a pipe, we need to be on track with the same, correct, code. Replace your code with our code in your QMD. @@ -748,7 +748,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r strings-9} +```{r strings-6} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -762,7 +762,7 @@ question_text(NULL, Knowledge drop -### Exercise 10 +### Exercise 7 Within the recent code chunk, add the option: `#| cache: true`. Assign the result of our pipe to `x`. @@ -772,7 +772,7 @@ Place your cursor on the line where the pipe is assigned to x, run `Cmd/Ctrl + E CP/CR. -```{r strings-10} +```{r strings-7} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -792,14 +792,14 @@ x <- babynames |> ``` -### Exercise 11 +### Exercise 8 Within the Console, type `x`, which we previously assigned to a pipe and ran in the Console. Hit `Enter`. CP/CR. -```{r strings-11} +```{r strings-8} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -819,7 +819,7 @@ x Insert a knowledge drop related to this project. -### Exercise 12 +### Exercise 9 @@ -835,7 +835,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r strings-12} +```{r strings-9} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -873,7 +873,7 @@ ggplot(x, aes(x = year, y = total_prop, color = sex)) + ### -### Exercise 13 +### Exercise 10 Now, we are going to create a new title using string functions. Using AI, ask it to generate code for creating the title of your plot using the function `str_c()` and assign the result to the variable `title1`. The title should stay the same, but they way it is made should be through `str_c()`. Add this code to a the same code cell with the plot, but before the code for the plot. Then, set the argument `title` equal to `title1` within the call to `labs()`. @@ -881,7 +881,7 @@ Place your cursor on the line with the code, and then run `Cmd/Ctrl + Enter`. Th CP/CR. -```{r strings-13} +```{r strings-10} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -907,7 +907,7 @@ title1 If you were to not use `str_c()`, double quotes "" or single quotes '' will also create a string. To include a literal single or double quote, you can use the backslash `\` to escape it. For example, `"\""` will return `'"'`. To include a literal backlash in your string, it is pretty simple and all you need to do is have two backslashes in a string to have one literal one. -### Exercise 14 +### Exercise 11 Once again, place your cursor on the line with the new `str_c()` code and hit `Cmd/Ctrl + Enter`, but don't run it yet. Now edit the code so that within `str_c()`, the strings include a backslash `\` at the start and doublequotes `""` at the end. Change the assigned variable to `title2` and press `Enter` to run the code. @@ -915,7 +915,7 @@ Then, type `str_view(title2)` in the Console, and press `Enter` to view the stri CP/CR. -```{r strings-14} +```{r strings-11} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -942,7 +942,7 @@ To solve the issue, we utilized a raw string, which is a specific type of string Typically, a raw string starts with `r"(, ends with )"`, and allows for any text representation. However, if the string contains )", alternatives like `r"[]"` or `r"{}"` can be used. Furthermore, you can add dashes to ensure unique opening and closing pairs, such as `r"--()--"`, `r"---()---"`, and so on. Raw strings offer flexibility to handle any text without problems. -### Exercise 15 +### Exercise 12 Now, we are going to do the same, but for a subtitle, and we will use str_glue(). Using AI, ask it to generate code for creating the subtitle of your plot using the the function `str_glue()` and assign the result to the variable `subtitle1`. The subtitle should stay the same, but they way it is made should be through `str_glue()`. Add this code to a the same code cell with the plot, but before the code for the plot. Then, set the argument `subtitle` equal to `subtitle1` within the call to `labs()`. @@ -950,7 +950,7 @@ Place your cursor on the line with the code, and then run `Cmd/Ctrl + Enter`. Th CP/CR. -```{r strings-15} +```{r strings-12} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -971,7 +971,7 @@ subtitle1 If you are mixing many fixed and variable strings with `str_c()`, you’ll notice that you type a lot of `"`s, making it hard to see the overall goal of the code. An alternative approach is provided by the glue package via `str_glue()`. How `str_glue()` works is that if you give it a single string that has a special feature: anything inside `{}` will be evaluated like it’s outside of the quotes. However, `str_glue()` currently converts missing values to the string "NA" unfortunately making it inconsistent with `str_c()`. -### Exercise 16 +### Exercise 13 Copy and paste the new `str_glue()` code into the Console, but don't run it yet. Now manually edit the code so that at the start, there is `\t`, and there is `\n` before the final word. Change the assigned variable to `subtitle2` and press `Enter` to run the code. @@ -979,7 +979,7 @@ Then, type `str_view(subtitle2)` in the Console, and press `Enter` to view the s CP/CR. -```{r strings-16} +```{r strings-13} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -998,7 +998,7 @@ str_view(subtitle2) Besides `\"`, `\'`, and `\\`, there are a few other special characters that can be useful. The most common ones are `\n` for a new line and `\t` for a tab. If you want to check out a complete list of other special characters, check out in [Quotes](https://rdrr.io/r/base/Quotes.html). -### Exercise 17 +### Exercise 14 Using the same AI, ask it to generate code for finding the top five most common names beginning with 'A' in 1950. @@ -1016,7 +1016,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r strings-17} +```{r strings-14} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -1052,7 +1052,7 @@ insight One variation of `str_flatten()` is `str_flatten_comma()` is a variation designed specifically for flattening with commas. It automatically recognizes if last uses the Oxford comma and handles the special case of 2 elements. -### Exercise 18 +### Exercise 15 Using the same AI, ask it to generate code that will find the most common second letters of **babynames** starting with "A" with the function `separate_longer_delim()`. In a new code cell below the previous cells, copy and paste the generated code. Then, place your cursor on any line of the code and press `Cmd/Ctrl + Shift + Enter` to run it in the Console. @@ -1060,7 +1060,7 @@ Then, isolate the part with the relavant function `separate_longer_delim()`, and CP/CR. -```{r strings-18} +```{r strings-15} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -1087,14 +1087,14 @@ The function `separate_longer_delim(col, delim)` makes the input data frame long The function `separate_longer_position(col, width)` makes the input data frame longer through increased rows, with the string split up with specified widths. -### Exercise 19 +### Exercise 16 Using the same AI, ask it to generate code that will combine two columns using `str_c()`, and then separate them again with `separate_wider_position()`. Using the same code cell from the previous exercise, copy and paste the generated code. Then, place your cursor on any line of the code and press `Cmd/Ctrl + Shift + Enter` to run it in the Console. For this one, run and print out the change after the first `mutate()`, and then run and print out what happens after the `separate_wider_positions()`. Do this by only running the code up to that section by placing your cursor at the top, and then run the code line by line with `Cmd/Ctrl + Enter` to see what the function changed in the dataset. -```{r strings-19} +```{r strings-16} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -1125,13 +1125,13 @@ The function `separate_wider_delim(col, delim, names)` makes the input data fram -### Exercise 20 +### Exercise 17 Make a new code cell after the plot, and using AI, ask it to generate code that pipes **babynames** to `count()` with `str_length()` to find the distribution of lengths of US baby names. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into the code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r strings-20} +```{r strings-17} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -1153,13 +1153,13 @@ babynames |> The function `str_length()` tells you the number of letters in the string. -### Exercise 21 +### Exercise 18 Next, get code that pipes `babynames` to `mutate()` and use `str_sub()` to find the first and last letter of each name. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into the same code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r strings-21} +```{r strings-18} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -1185,13 +1185,13 @@ babynames |> The function `str_sub(string, start, end)` can extract parts of a string. The start and end are the positions where the substring should start and end, and the start and end arguments are inclusive. -### Exercise 22 +### Exercise 19 Next, get code that pipes `babynames` to `filter()` with name and `str_starts()` to find all names beginning with a certain string. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into the same code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r strings-22} +```{r strings-19} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -1212,7 +1212,7 @@ babynames |> filter(str_starts(name, "A")) The function `str_starts()` returns TRUE if each string starts with the given pattern. We have used this function earlier in this tutorial! Also, the function `str_ends()` returns TRUE if each string ends with the given pattern. The function `str_detect()` returns TRUE if each string contains the given pattern anywhere inside. -### Exercise 23 +### Exercise 20 Now that we have all these functions in the same code cell, we can see all of the functions we have learned, and we can run any of them at will with `Cmd/Ctrl + Enter`. @@ -1224,7 +1224,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r strings-23} +```{r strings-20} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, From eddbd4355ffd4fae047a1fa4b82640d59d2e59f1 Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 01:37:49 -0400 Subject: [PATCH 4/8] reworked strings separate functions --- .../30-ai-tutorial-test/tutorial.Rmd | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 9cd8469b..9e12823e 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -1054,11 +1054,11 @@ One variation of `str_flatten()` is `str_flatten_comma()` is a variation designe ### Exercise 15 -Using the same AI, ask it to generate code that will find the most common second letters of **babynames** starting with "A" with the function `separate_longer_delim()`. In a new code cell below the previous cells, copy and paste the generated code. Then, place your cursor on any line of the code and press `Cmd/Ctrl + Shift + Enter` to run it in the Console. +Using the same AI, ask it to generate code that will collapse our previously made `top_a_1950`, and then split the names into separate rows with `separate_longer_delim()`. In a new code cell below the previous cells, copy and paste the generated code. -Then, isolate the part with the relavant function `separate_longer_delim()`, and only run the code up to that section by placing your cursor at the top, and then run the code line by line with `Cmd/Ctrl + Enter` to see what the function changed in the dataset. +Then, isolate the part where `top_a_1950` is collapsed, and either run it in the Console to view the tibble. Then, run the whole thing with the relavant function `separate_longer_delim()` by placing your cursor at the top and pressing `Cmd/Ctrl + Enter`. -CP/CR. +CP/CR both tibbles. ```{r strings-15} question_text(NULL, @@ -1074,13 +1074,15 @@ question_text(NULL, Our code: -```{r, echo=TRUE, eval=FALSE} -babynames |> - filter(str_starts(name, "A")) |> - separate_longer_delim(name, delim = '') |> - mutate(pos = row_number(), .by = c(name, year, sex)) |> - filter(pos == 2) |> - count(letter = name, sort = TRUE) +```{r, echo=TRUE} +collapsed <- top_a_1950 |> + summarize(name_list = str_c(name, collapse = ",")) +collapsed +``` + +```{r, echo=TRUE} +collapsed |> + separate_longer_delim(name_list, delim = ",") ``` The function `separate_longer_delim(col, delim)` makes the input data frame longer through increased rows, with the string split up through a specified deliminator as an argument. @@ -1089,10 +1091,11 @@ The function `separate_longer_position(col, width)` makes the input data frame l ### Exercise 16 -Using the same AI, ask it to generate code that will combine two columns using `str_c()`, and then separate them again with `separate_wider_position()`. Using the same code cell from the previous exercise, copy and paste the generated code. Then, place your cursor on any line of the code and press `Cmd/Ctrl + Shift + Enter` to run it in the Console. +Using the same AI, ask it to generate code that will separate the first letter of a name from `top_a_1950` from the rest of the name using `separate_wider_position()`. Using the same code cell from the previous exercise, copy and paste the generated code. Then, place your cursor on any line of the code and press `Cmd/Ctrl + Shift + Enter` to run it in the Console. -For this one, run and print out the change after the first `mutate()`, and then run and print out what happens after the `separate_wider_positions()`. Do this by only running the code up to that section by placing your cursor at the top, and then run the code line by line with `Cmd/Ctrl + Enter` to see what the function changed in the dataset. +CP/CR. +If there is an error, plug it back into AI and see what it says. ```{r strings-16} question_text(NULL, @@ -1109,14 +1112,15 @@ question_text(NULL, Our code: ```{r, echo=TRUE, eval=FALSE} -babynames |> - filter(str_starts(name, "A")) |> - mutate(name_sex = str_c(str_pad(name, width = 10, side = "right"), sex)) |> - separate_wider_position(name_sex, positions = c(10), names = c("name_clean", "sex_clean")) |> - select(name_clean, sex_clean) +top_a_1950 |> + separate_wider_position( + name, + widths = c(first_letter = 1, rest_of_name = 100), + too_few = "align_start" + ) ``` -The function `separate_wider_position(col, widths, names)` makes the input data frame wider through increased columns, with the string split up with a specified widths. Note how because the character length for names varied, we had to pad it with 10 extra characters so we could for sure separate the columns by width. +The function `separate_wider_position(col, widths)` makes the input data frame wider through increased columns, with the string split up with a specified widths. Note how because the character length for names varied, we had to pad it with 10 extra characters so we could for sure separate the columns by width. The function `separate_wider_delim(col, delim, names)` makes the input data frame wider through increased columns, with the string split up with a specified deliminator. From 98515751154c89ab294e5866cb86c80a591656d1 Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 01:39:50 -0400 Subject: [PATCH 5/8] eval=TRUE for everything --- inst/tutorials/30-ai-tutorial-test/tutorial.Rmd | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 9e12823e..4e796101 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -1111,7 +1111,7 @@ question_text(NULL, Our code: -```{r, echo=TRUE, eval=FALSE} +```{r, echo=TRUE} top_a_1950 |> separate_wider_position( name, @@ -1149,7 +1149,7 @@ question_text(NULL, Our code: -```{r, echo=TRUE, eval=FALSE} +```{r, echo=TRUE} babynames |> count(length = str_length(name), sort = TRUE) ``` @@ -1177,7 +1177,7 @@ question_text(NULL, Our code: -```{r, echo=TRUE, eval=FALSE} +```{r, echo=TRUE} babynames |> mutate( first_letter = str_sub(name, 1, 1), @@ -1209,7 +1209,7 @@ question_text(NULL, Our code: -```{r, echo=TRUE, eval=FALSE} +```{r, echo=TRUE} babynames |> filter(str_starts(name, "A")) ``` From 619acececfb987ffb312a97fe6da67d3903bafd4 Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 01:40:57 -0400 Subject: [PATCH 6/8] done on comments --- inst/tutorials/30-ai-tutorial-test/tutorial.Rmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 4e796101..0b18e231 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -31,11 +31,11 @@ options(tutorial.exercise.timelimit = 600, ``` - + - + - + ## Introduction ### From 512e0a94b2d5549ccb06ba3d441c03e6fbfd87cb Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 01:55:25 -0400 Subject: [PATCH 7/8] more on numbers --- .../30-ai-tutorial-test/tutorial.Rmd | 71 +++++++++++++------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 0b18e231..9907aa42 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -193,6 +193,33 @@ Typing out `flights` is the same as typing out `print(flights)`. You might be wo ### Exercise 3 +Now, ask your favorite AI to generate R code that creates a new column from `flights` that just contains the numbers from the `tailnum` column. Then, only show that new column. Copy the code into the Console and hit `Enter` to run it. + +CP/CR. + +```{r numbers-3} +question_text(NULL, + answer(NULL, correct = TRUE), + allow_retry = TRUE, + try_again_button = "Edit Answer", + incorrect = NULL, + rows = 4) +``` + +### + +Our code: + +```{r, echo=TRUE} +flights |> + mutate(tailnum_num = parse_number(tailnum)) |> + select(tailnum_num) +``` + +Knowledge drop on parse_number(), parse_integer(), and parse_double(). + +### Exercise 4 + Now, ask your favorite AI to use `select()` on `flights` to obtain only the columns `dep_time`, `sched_dep_time`, and `dep_delay`. Make sure to provide a tibble of **flights** for the AI to use. @@ -202,7 +229,7 @@ Then, type `flights_clean` in the Console and hit `Enter`. CP/CR the resulting tibble. -```{r numbers-3} +```{r numbers-4} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -221,14 +248,14 @@ flights_clean <- flights |> flights_clean ``` -### Exercise 4 - +### Exercise 5 + Next, prompt the AI to generate R code that takes `flights` and breaks scheduled and actual departure times into hours and minutes using %/% and %%. If you have not done so before, provide a tibble of **flights** for the AI to use. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in a new chunk. Save the file. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. In the Console, run: @@ -239,7 +266,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-4} +```{r numbers-5} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -263,7 +290,7 @@ flights_clean |> ``` -### Exercise 5 +### Exercise 6 Next, prompt the AI to generate R code that continues the pipe and adds a new column `true_dep_delay`, treating early departures (negative values) as 0 using `mutate()` and `pmax()`. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in the same chunk. Save the file. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. @@ -275,7 +302,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-5} +```{r numbers-6} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -299,7 +326,7 @@ flights_clean |> mutate(true_dep_delay = pmax(dep_delay, 0)) ``` -### Exercise 6 +### Exercise 7 Next, prompt the AI to generate R code that finishes the pipe and collapses the dataset, grouping by hour pairs and summing delay using `count(..., wt=...)`. Essentially, we want to sum the total number of minutes of positive departure delay for each combination of scheduled hour and actual departure hour. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in the same chunk. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. @@ -312,7 +339,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-6} +```{r numbers-7} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -337,7 +364,7 @@ flights_clean |> count(sched_hour, dep_hour, wt = true_dep_delay, name = "total_delay") ``` -### Exercise 7 +### Exercise 8 Now assign the pipe to the variable `x`. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. @@ -349,7 +376,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-7} +```{r numbers-8} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -374,7 +401,7 @@ x <- flights_clean |> count(sched_hour, dep_hour, wt = true_dep_delay, name = "total_delay") ``` -### Exercise 8 +### Exercise 9 Now that we have filtered our dataset through a pipe, we need to be on track with the same, correct, code. Replace your code with our code in your QMD. @@ -386,7 +413,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-8} +```{r numbers-9} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -400,7 +427,7 @@ question_text(NULL, -### Exercise 9 +### Exercise 10 Go to the AI and ask it to generate R code that plots a clean and insightful heatmap that shows where delays tend to cluster by scheduled vs. actual departure hour. Mention you want to use the data from `x` and copy and paste the `x` you ran in the Console with the resulting tibble. You only need the top 3 lines, mainly to include column names. @@ -414,7 +441,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-9} +```{r numbers-10} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -458,13 +485,13 @@ ggplot(x, aes(x = sched_hour, y = dep_hour, fill = total_delay)) + ) ``` -### Exercise 10 +### Exercise 11 Now, using AI, ask it to generate code that rounds the `total_delay` column to the hundreds place using `round()`. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into a new code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r numbers-10} +```{r numbers-11} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -483,13 +510,13 @@ x |> mutate(total_delay = round(total_delay, -2)) ``` -### Exercise 11 +### Exercise 12 Now, using AI, ask it to generate code that applies `min_rank()` to the `total_delay` column in `x` to see the ranks of the highest total delay times per combination of scheduled departure hour and actual departure hour. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into the same code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r numbers-11} +```{r numbers-12} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -508,13 +535,13 @@ x |> mutate(rank_total_delay = min_rank(desc(total_delay))) ``` -### Exercise 12 +### Exercise 13 Now, using AI, ask it to generate code that uses summarize() in your `x` dataset to calculate the mean, median, and 95th percentile (quantile) of `true_dep_delay`. In this case, copy and paste the pipe that is assigned to `x`, change it to `y`, and then remove the `count()`. Then, run the AI generated code on `y`. Make sure that the pipe is not assigned to any variable so it is easier to run. Copy and paste the code into same the code cell. Place your cursor on the line with the code, and press `Cmd/Ctrl + Enter` to run the code in the Console. CP/CR. -```{r numbers-12} +```{r numbers-13} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, @@ -547,7 +574,7 @@ y |> ) ``` -### Exercise 13 +### Exercise 14 Now that we have all these functions in the same code cell, we can see all of the functions we have learned, and we can run any of them at will with `Cmd/Ctrl + Enter`. @@ -559,7 +586,7 @@ show_file("analysis.qmd", chunk = "Last") CP/CR. -```{r numbers-13} +```{r numbers-14} question_text(NULL, answer(NULL, correct = TRUE), allow_retry = TRUE, From 7021fcfbe7a1047ce7f6c694b544a6485c17b56d Mon Sep 17 00:00:00 2001 From: Luke Li Date: Tue, 29 Jul 2025 01:56:57 -0400 Subject: [PATCH 8/8] fix --- inst/tutorials/30-ai-tutorial-test/tutorial.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd index 9907aa42..ba30446a 100644 --- a/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd +++ b/inst/tutorials/30-ai-tutorial-test/tutorial.Rmd @@ -256,7 +256,7 @@ flights |> ### Exercise 5 -Next, prompt the AI to generate R code that takes `flights` and breaks scheduled and actual departure times into hours and minutes using %/% and %%. If you have not done so before, provide a tibble of **flights** for the AI to use. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in a new chunk. Save the file. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. +Next, prompt the AI to generate R code that takes `flights_clean` and breaks scheduled and actual departure times into hours and minutes using %/% and %%. If you have not done so before, provide a tibble of **flights_clean** for the AI to use. If necessary, change the code so that the result of the pipe is not assigned to a variable. Add the code to your QMD in a new chunk. Save the file. Place your cursor on the first line of the code and run `Cmd/Ctrl + Enter`. In the Console, run: