Skip to content

Commit

Permalink
Update TIL page
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelasq committed Jun 19, 2024
1 parent 17f4ebe commit 371062c
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 10 deletions.
15 changes: 15 additions & 0 deletions _freeze/til-other/asciicast/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hash": "dffbc2f3ac6dbb7fa66abbc7bbb18bb1",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"How to create a GIF of code and its output\"\ndate: '2022-05-10'\ncategory: Other\n---\n\n\nWe can create a GIF of code and its output using [asciicast](https://github.com/r-lib/asciicast). \n\nInstall the development version:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nremotes::install_github('r-lib/asciicast', ref = remotes::github_pull(24)) \n```\n:::\n\n\nCreate a file called `nzchar.R` with specifications for the GIF and the code to run.\n\n\n::: {.cell}\n\n```{.r .cell-code}\n#' Title: Using nzchar\n#' Columns: 60\n#' Rows: 18\n\n# This is not empty\nSys.getenv(\"R_LIBS_USER\")\n\n# This is empty\nSys.getenv(\"test\")\n\n# This returns TRUE\nnzchar(Sys.getenv(\"R_LIBS_USER\"))\n\n# This returns FALSE\nnzchar(Sys.getenv(\"test\"))\n```\n:::\n\n\nCreate another file that creates the GIF:\n\n\n::: {.cell}\n\n```{.r .cell-code}\n#' Title: Using nzchar\n\nsrc <- \"nzchar.R\"\ncast <- asciicast::record(src)\n\n# <<\n# `cast` is an `asciicast` object, which has some metadata and the\n# recording itself:\n# <<\n\ncast\n\n# <<\n# You can write `cast` to a GIF file with the version installed above.\n# <<\n\nsvg <- tempfile(fileext = \"gif\")\nasciicast::write_gif(cast, svg, theme = \"monokai\")\n```\n:::\n\n\nReally fun for adding to tweets!\n\n![](example.gif){fig-alt=\"An animation created with asciicast that shows that nzchar() returns TRUE when a string is nonempty and FALSE when a string is empty, using Sys.getenv() to query existing 'R_LIBS_USER' and nonexisting 'test' environment variables.\"}\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
15 changes: 15 additions & 0 deletions _freeze/til-r/filter-if-any/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hash": "726c00f0438abdbe545553d61062ca52",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"How to filter on conditions for more than one variable at the time\"\ndate: \"2022-03-21\"\ncategory: R\n---\n\n\n\n\nTIL I learned that you can filter on conditions for more than one variable at a time using `if_any()` or `if_all()`.\n\n<center>\n<blockquote class=\"twitter-tweet\"><p lang=\"en\" dir=\"ltr\">Just to add to the confusion (😅) I think you still do use it for mutate but not for filter?<br><br>⚠️ Using `across()` in `filter()` is deprecated, use `if_any()` or `if_all()`.</p>&mdash; Lucy D’Agostino McGowan (@LucyStats) <a href=\"https://twitter.com/LucyStats/status/1506026504618221579?ref_src=twsrc%5Etfw\">March 21, 2022</a></blockquote> <script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script> \n</center>\n\nTurns out that `across()` is only for selecting functions (like `summarize()` and `mutate()`). This was announced in [dplyr 1.0.4](https://www.tidyverse.org/blog/2021/02/dplyr-1-0-4-if-any/).\n\nYou use `if_any()` vs. `if_all()` depending if you need to match some vs. all columns.\n\n`if_any()`:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmtcars %>%\n as_tibble() %>%\n mutate(across(everything(), as.integer)) %>%\n filter(if_any(contains(\"m\"), ~ . == 0))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 19 × 11\n mpg cyl disp hp drat wt qsec vs am gear carb\n <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>\n 1 21 6 258 110 3 3 19 1 0 3 1\n 2 18 8 360 175 3 3 17 0 0 3 2\n 3 18 6 225 105 2 3 20 1 0 3 1\n 4 14 8 360 245 3 3 15 0 0 3 4\n 5 24 4 146 62 3 3 20 1 0 4 2\n 6 22 4 140 95 3 3 22 1 0 4 2\n 7 19 6 167 123 3 3 18 1 0 4 4\n 8 17 6 167 123 3 3 18 1 0 4 4\n 9 16 8 275 180 3 4 17 0 0 3 3\n10 17 8 275 180 3 3 17 0 0 3 3\n11 15 8 275 180 3 3 18 0 0 3 3\n12 10 8 472 205 2 5 17 0 0 3 4\n13 10 8 460 215 3 5 17 0 0 3 4\n14 14 8 440 230 3 5 17 0 0 3 4\n15 21 4 120 97 3 2 20 1 0 3 1\n16 15 8 318 150 2 3 16 0 0 3 2\n17 15 8 304 150 3 3 17 0 0 3 2\n18 13 8 350 245 3 3 15 0 0 3 4\n19 19 8 400 175 3 3 17 0 0 3 2\n```\n\n\n:::\n:::\n\n\n`if_all()`:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmtcars %>%\n as_tibble() %>%\n mutate(across(everything(), as.integer)) %>%\n filter(if_all(contains(\"am\"), ~ . == 0))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 19 × 11\n mpg cyl disp hp drat wt qsec vs am gear carb\n <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>\n 1 21 6 258 110 3 3 19 1 0 3 1\n 2 18 8 360 175 3 3 17 0 0 3 2\n 3 18 6 225 105 2 3 20 1 0 3 1\n 4 14 8 360 245 3 3 15 0 0 3 4\n 5 24 4 146 62 3 3 20 1 0 4 2\n 6 22 4 140 95 3 3 22 1 0 4 2\n 7 19 6 167 123 3 3 18 1 0 4 4\n 8 17 6 167 123 3 3 18 1 0 4 4\n 9 16 8 275 180 3 4 17 0 0 3 3\n10 17 8 275 180 3 3 17 0 0 3 3\n11 15 8 275 180 3 3 18 0 0 3 3\n12 10 8 472 205 2 5 17 0 0 3 4\n13 10 8 460 215 3 5 17 0 0 3 4\n14 14 8 440 230 3 5 17 0 0 3 4\n15 21 4 120 97 3 2 20 1 0 3 1\n16 15 8 318 150 2 3 16 0 0 3 2\n17 15 8 304 150 3 3 17 0 0 3 2\n18 13 8 350 245 3 3 15 0 0 3 4\n19 19 8 400 175 3 3 17 0 0 3 2\n```\n\n\n:::\n:::\n\n\nAny tidyselect usage is allowable inside `if_*()` just like inside `across()`, so they work very similarly.\n\n**Thanks to [@gvelasq](https://github.com/gvelasq) for his explanation.**\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
15 changes: 15 additions & 0 deletions _freeze/til-r/nzchar/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hash": "0783d2508512214c16493d20a0f8b390",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"How to count the number of characters (or bytes or width)\"\ndate: '2022-05-10'\ncategory: R\noutput: html_document\n---\n\n\nFrom the [documentation](https://rdrr.io/r/base/nchar.html), `nzchar()` is a fast way to find out if elements of a character vector are non-empty strings. It returns `TRUE` for non-empty strings and `FALSE` for empty strings.\n\n::: {.panel-tabset}\n\n### Not Empty Vector\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# This is not empty\nSys.getenv(\"R_LIBS_USER\")\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"/Users/ivelasq/Library/R/x86_64/4.3/library\"\n```\n\n\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\n# This returns TRUE\nnzchar(Sys.getenv(\"R_LIBS_USER\"))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] TRUE\n```\n\n\n:::\n:::\n\n\n### Empty Vector\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# This is empty\nSys.getenv(\"test\")\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"\"\n```\n\n\n:::\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\n# This returns FALSE\nnzchar(Sys.getenv(\"test\"))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] FALSE\n```\n\n\n:::\n:::\n\n\n:::\n\n<center>\n<blockquote class=\"twitter-tweet\"><p lang=\"en\" dir=\"ltr\">TIL: nzchar(). Super useful when working with environment variables in R.<br><br>also, <a href=\"https://twitter.com/hashtag/asciicast?src=hash&amp;ref_src=twsrc%5Etfw\">#asciicast</a> is amazing! install the GIF converter with remotes::install_github(&#39;r-lib/asciicast&#39;, ref = remotes::github_pull(24)) <a href=\"https://twitter.com/hashtag/rstats?src=hash&amp;ref_src=twsrc%5Etfw\">#rstats</a> h/t <a href=\"https://twitter.com/GaborCsardi?ref_src=twsrc%5Etfw\">@GaborCsardi</a> <a href=\"https://t.co/pCZQLCNaDl\">pic.twitter.com/pCZQLCNaDl</a></p>&mdash; Isabella Velásquez (@ivelasq3) <a href=\"https://twitter.com/ivelasq3/status/1524193394037342211?ref_src=twsrc%5Etfw\">May 11, 2022</a></blockquote> <script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script></center>\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions til-python/_metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# options specified here will apply to all posts in this folder

# freeze computational output
# (see https://quarto.org/docs/projects/code-execution.html#freeze)
freeze: true
File renamed without changes.
5 changes: 5 additions & 0 deletions til-r/_metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# options specified here will apply to all posts in this folder

# freeze computational output
# (see https://quarto.org/docs/projects/code-execution.html#freeze)
freeze: true
File renamed without changes.
File renamed without changes.
42 changes: 32 additions & 10 deletions til.qmd
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
---
title: "Today I Learned"
listing:
contents: til
type: table
sort-ui: false
filter-ui: false
sort: "date desc"
fields: [title]
template: til-template.ejs
feed: true
- id: til-r
contents: til-r
type: table
template: til-template.ejs
- id: til-python
contents: til-python
type: table
template: til-template.ejs
- id: til-other
contents: til-other
type: table
template: til-template.ejs
format:
html:
page-layout: full
page-layout: article
filters:
- fontawesome
---

<a href="https://ivelasq.rbind.io/til.xml" target="_blank"><i class="fa-sharp fa-solid fa-rss"></i></a>&nbsp;
<a href="https://gist.github.com/ivelasq" target="_blank"><i class="fa-brands fa-github"></i></a>

## R

<a href="https://ivelasq.rbind.io/til.xml" target="_blank"><i class="fa-solid fa-rss"></i></a>&nbsp;&nbsp;&nbsp;<a href="https://gist.github.com/ivelasq" target="_blank"><i class="fa-brands fa-github"></i></a>
:::{#til-r}
:::

## Python

:::{#til-python}
:::

## Other

:::{#til-other}
:::

{{< fa cat >}}

0 comments on commit 371062c

Please sign in to comment.