Today I learned (TIL) · 2023

TIL yearly
Published

November 26, 2023

26. November

Keep the date of a git commit after changing the history with rebase

git rebase -i --committer-date-is-author-date <SHA>

21. November

There is a magic method in iPython to install packages with pip. It comes handy for a quick install in a notebook.

%pip install -q requests

Specifying dependencies in requirements.in or pyproject.toml and pinning them with pip-tools is the better solution to create reproducible environments.

31. July

Simple debugging with IPython — found at Real Python (now gated).

import IPython

var_1 = "hello"

IPython.embed()

var_2 = "world"

30. June

Set default printing function for data frames in Quarto notebook with html option df-print: kable.

Useful to have html tables instead of monospace tables in notebooks.

So far, I prefer kable (knitr::kable()) over paged (rmarkdown::paged_table())).

---
title: "Document"
format:
   html:
     df-print: kable
---

29. June

Didn’t know n_distinct() and used length(unique()) for too long. A small and nice thing i learned that makes pipes more readable.

13. June

Adding a comment in Markdown by using standard html comments. It works for Markdown in pandoc used by Quarto.

<!--- comment  -->

<!---
multi-line comments
-->

I used it now with the current TIL page to have a short intro about a new TIL on the landing page. The previous one sentence summary of the page is now a comment.

10. June

Adding a page break to a pdf in Quarto.

---
format: pdf
---

Page 1

{{< pagebreak >}}

Page 2

In addition, I learned to ignore shortcuts in a cell with {.markdown shortcodes="false"}

09. June

I wanted to run a Bash shell script from any folder. The initial solution worked on my local computer but not on GitHub Codespaces. Here is the solution I came up with. The script runs from any location or stops and gives a message that it should be run from the script folder.

# Use path relative to script location, if feasible
if [ -n "$BASH_SOURCE" ]; then
    cd "$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )"
else
    if ! [ -e "package-lock.json" ]; then
        echo "Run script in project folder"
        exit 1
    fi
fi

17. May

Another use of the Base-R shorthand anonymous function and explaining it to a colleague. I like to use .x now for the local variable name to make the local context more explicit.

add_three <- \(.x) .x+3

06. May

Excluding variables in tidyselect is now done with ! as I found out by reading the documentation more carfully. I have still used - to exclude variables by their name. This is not documented in the tidyselect help page anymore.

readstata13 creates more R friendly data structures than haven. Its default option converts labeled data into factors if appropriate.

05. May

Used Quarto a lot and got more comfortable with the options — esp. caching, creating pdf, figures.

03. May

Convert an RMarkdown file into an R file with knitr::purl("purl.Rmd") – see details.

ChatGPT did not provide the solution. I remembered doing this years ago. An internet search quickly led to the information needed.

02. May

A bug in the current version of RStudio kept me busy. I reinstalled R and RStudio on Windows, but RStudio did not find the new R version afterwards. So I reinstalled the two and it kept not working. Fortunately, I found information about the RStudio bug somewhat quickly. I installed the daily build and had R/RStudio running again.

Originally, I only wanted to do a quick data analysis but my previous R/RStudio updates came in between.

25. April

I got back into Quarto and created a website for a learning project I have started. My goal is to have some structured information about topics of a book I am reading and some code snippets that apply concepts of the book.

Setting up the page worked well. I got a little confused by the quarto difference between freeze and cache. I also wondered why the cache was added into several folders in the main directory and not into a _cache folder.

03. April

I read about the new pandas release. None of the changes seem to be relevant for my work with pandas

25. February

I worked on code from a causal inference book. It includes Stata, R and Python versions of the examples.

For R and Python I wanted to have one environment to install all dependencies. This led me to conda.

Creating a new conda environment in a new file was extremly slow. It also took me a while to fully configure Jupyter Lab with Python and R.

At the end, VS Code seems to be the more convenient interface.