From fc0f367760b9b03280e97eba42ad828ccc1f1253 Mon Sep 17 00:00:00 2001 From: trekonom Date: Sat, 16 Mar 2024 18:46:46 +0100 Subject: [PATCH] Update index.qmd --- _freeze/index/execute-results/html.json | 4 +-- _quarto.yml | 2 +- index.qmd | 47 ++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/_freeze/index/execute-results/html.json b/_freeze/index/execute-results/html.json index 9d4e83a..16b6dcd 100644 --- a/_freeze/index/execute-results/html.json +++ b/_freeze/index/execute-results/html.json @@ -1,8 +1,8 @@ { - "hash": "6b66cae8302b77debe544886f45671d9", + "hash": "612f49f6ca4fc9b3c71614fc22d55d29", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Intro-tidy\"\nformat: revealjs\nengine: knitr\nexecute: \n freeze: auto\n---\n\n\nThis is a webr-enabled code cell in a Quarto HTML document.\n\n\n::: {.cell}\n\n:::\n\n\n```{webr-r}\nggplot()\n```", + "markdown": "---\ntitle: \"Intro-tidy\"\nformat: revealjs\nengine: knitr\nexecute: \n freeze: auto\n---\n\n\n\n\n\n# tidyverse {background-color=\"aquamarine\"}\n\n> The [tidyverse](https://www.tidyverse.org) is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.\n\n## Why should we care about the tidyverse?\n\n> What's the mean of `mpg` (miles per gallon) and the number of observations by `cyl` (cylinders) in the `mtcars` dataset?\n\n::: columns\n\n::: {.column width=\"50%\"}\n#### tidyverse\n\n::: {.cell}\n\n```{.r .cell-code}\nmtcars %>%\n group_by(cyl) %>%\n summarise(mean = mean(mpg), n = n())\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 3 × 3\n cyl mean n\n \n1 4 26.7 11\n2 6 19.7 7\n3 8 15.1 14\n```\n\n\n:::\n:::\n\n:::\n\n::: {.column width=\"50%\"}\n#### base R\n\n::: {.cell}\n\n```{.r .cell-code}\nmtcars_by <- by(mtcars, mtcars$cyl, function(df) {\n with(df, data.frame(\n cyl = cyl[[1]],\n mean = mean(mpg),\n n = nrow(df)\n ))\n})\ndo.call(rbind, mtcars_by)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n cyl mean n\n4 4 26.66364 11\n6 6 19.74286 7\n8 8 15.10000 14\n```\n\n\n:::\n:::\n\n:::\n\n:::\n\n::: aside\nAdapted from [dplyr base R](https://dplyr.tidyverse.org/articles/base.html#summarise-reduce-multiple-values-down-to-a-single-value)\n:::", "supporting": [ "index_files" ], diff --git a/_quarto.yml b/_quarto.yml index 344e5ad..44f93ea 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -19,5 +19,5 @@ filters: - webr webr: show-startup-message: true - packages: ["ggplot2"] + packages: ["ggplot2", "dplyr"] diff --git a/index.qmd b/index.qmd index 3566167..f3d041a 100644 --- a/index.qmd +++ b/index.qmd @@ -6,12 +6,49 @@ execute: freeze: auto --- -This is a webr-enabled code cell in a Quarto HTML document. +```{r include=FALSE} +library(ggplot2) +library(dplyr) +``` + + +# tidyverse {background-color="aquamarine"} + +> The [tidyverse](https://www.tidyverse.org) is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures. + +## Why should we care about the tidyverse? + +> What's the mean of `mpg` (miles per gallon) and the number of observations by `cyl` (cylinders) in the `mtcars` dataset? +::: columns + +::: {.column width="50%"} +#### tidyverse ```{r} -library(ggplot2) +#| echo: true +mtcars %>% + group_by(cyl) %>% + summarise(mean = mean(mpg), n = n()) +``` +::: + +::: {.column width="50%"} +#### base R +```{r} +#| echo: true +mtcars_by <- by(mtcars, mtcars$cyl, function(df) { + with(df, data.frame( + cyl = cyl[[1]], + mean = mean(mpg), + n = nrow(df) + )) +}) +do.call(rbind, mtcars_by) ``` +::: + +::: -```{webr-r} -ggplot() -``` \ No newline at end of file +::: aside +Adapted from [dplyr base R](https://dplyr.tidyverse.org/articles/base.html#summarise-reduce-multiple-values-down-to-a-single-value) +::: \ No newline at end of file