Leaning To Program With ANKI
Leaning To Program With ANKI
Leaning To Program With ANKI
repetition) and even used it years before to try to learn Croatian (to admittedly samo malo
success).
Unfortunately, using Anki itself never stuck, so I was never as successful using it as I
could have been. But I knew how incredibly powerful it could be, so I spent two days
setting up Anki again and putting every function on this Data Wrangling Cheatsheet
(https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf) into
it.
And then…magic happened. Within a week, I wrote code from memory. I didn’t have to
look up how to transform a data frame from wide to long format, how to select only certain
rows or remove columns that I didn’t need.
I wrote R code like it was normal text! It was completely surreal. I still had to think about
what I wanted to do with my code, how the data or the analysis should look like in the end.
But I didn’t need to think about how to get there anymore.
Suddenly, I had the feeling of making progress. I still had a lot of work to do for my thesis,
and I still spent a lot of time on StackOverflow. But I did so for solving real problems, not
for figuring out how to do basic R things.
In this guide I’ll show you how you too can flip that switch and write R code like it’s
nothing.
There have been written great essays about Spaced Repetition and the science behind it
(https://www.gwern.net/Spaced-repetition) and how spaced repetition is the perfect way to
augment your long term memory (http://augmentingcognition.com/ltm.html) and I
encourage you strongly to read them both. They might just change your life forever, just as
they have changed mine, showing you that
remembering is a choice, that
you can control what you can recall at a moments notice.
The one thing these essays don’t talk about as much is implementing spaced repetition.
So that’s what I want to do here. Show you that you are able to learn writing R code, and
that you can learn to write R fast. The one thing this does require is consistency, but we’ll
return to that a bit later.
So, how are you going to learn R using Anki and spaced repetition? In this guide I’ll show
you
1. how to set up Anki so that it’s super easy to add new code snippets to it,
2. how to write good flash cards - especially for programming,
3. how to make this a habit that makes you a badass R programmer for the rest of your
life and
4. if you fiddle and optimize a bit, how to configure it so you get the most out of the time
you spend reviewing
If you like this guide and want to learn more about R, data processing and management
and how to make your life dealing with data easier, also sign up for my email newsletter
(http://eepurl.com/gbqGhD) so I can let you know about new articles.
Figure 1: What you should see when you open Anki the first time
(I am using the macOS version of Anki, so yours will look slightly different if you’re on
Windows or Linux, but it will work the same.)
With Anki installed, you will need to install two other things, two Anki Add-ons:
1. Mini Format Pack
2. Syntax Highlighting for Code
To install Add-ons, go to “Tools” in the Anki Menu and then go to the “Add-ons” menu:
In this window, you will need to enter the numeric IDs of the addons you want to install. To
get these IDs you either click on “Browse Add-ons”, which will take you here
(https://ankiweb.net/shared/addons/2.1), and then search manually for the two extensions
we want to install, or you follow these two links which will take you to the Add-on pages
directly:
Mini Format Pack (https://ankiweb.net/shared/info/295889520)
Syntax Highlighting for Code (https://ankiweb.net/shared/info/1463041493)
On these pages, you will find a section “Download” with a number code that looks
something like the picture on the left, which you will need to paste into the “Code” field of
the “Get Add-on” window:
Click on “OK” and repeat the process to install the second Add-on.
Once you have both Add-ons installed, you’ll need to tweak the settings in two places.
First, in the Add-ons overview window, select the “Syntax Highlighting for Code” Add-on
and click the “Config” button:
In the window that opens, adjust the section that reads “limitToLangs”. Depending on
which programming languages you want to learn, provide their names as a comma-
separated list inside the brackets, with each language enclosed in quotes. Note: For R to
be recognized, the language you have to give is actually S for historical reasons. So you
would have something like:
"limitToLangs": [
"S",
"Python",
"Bash"
],
if you wanted to learn R, Python and how to use the command line. Adjust the “style”
section to read "style": "manni" and click on “OK”.
Back in the “Tools” menu, find the “Syntax Highlighting Options” menu and adjust the
setting there to have “Line Numbers” unchecked, “Center Code Fragments” unchecked,
“Use CSS classes” checked and “Default to last language used per deck” checked, like
this:
You have now installed everything to start learning R and memorizing its syntax with Anki.
Now we can move on to the nitty gritty.
memorize”. This is particularly important when learning programming. You need a good
understanding of what the code, the functions, and the rules you are learning do and are
used for. In theory, you could go through the whole R Documentation and make a
flashcard for every function or behaviour, commit that to memory using Anki and then
“know” how to write R. And that would definitely work! But you would have two problems:
you’d have a miserable time doing it and you’d still not know when to apply it and where.
Anki is a tool for remembering things you haved learned, not for learning things you’ve
never seen before.
Rule 3 and 4: “Build upon the basics” and “Stick to the minimum information
principle”. You do not need to have read and understood all of Hadley Wickhams
“Advanced R” before you start memorizing things.
On the contrary, start memorizing tiny facts that you have understood. Make the
most basic behaviors of R so automatic that you forget that you ever needed to learn
them.
Rule 5: “Cloze deletion is easy and effective”. Cloze deletion is when you are shown a
sentence with a part missing and replaced by dots, which you then have to remember.
Cloze deletion is essential for learning R.
By looking at code and identifying what’s missing, you build an essential skill for a
programmer and memorize code in its actual real-world context where you want to use it.
I also want to share two rules with you that I’ve figured out on my own that apply directly to
programming flashcards.
1. You have to type your answers. Anki is great in that it not only lets you use cloze
deletions, but also provides the option to type the answer to the clozes. This is critical for
any programming-related memorization.
A lot of what you want to train is in essence muscle memory. You come across a task or
problem and your fingers just move to type the solution. If you use cloze deletions to only
see the answer instead of typing it you are not doing that, and with code in particular it’s
super easy to cheat yourself unconsciously by thinking “yeah, yeah, I knew that, next”.
Typing the clozes makes that harder because you get direct visual feedback of where you
missed even a single character, which is critical when writing code.
2. Give an explicit task and provide all necessary information.
In the beginning, I often asked myself general questions, like “How does assignment work
in R?” and then had an answer on the back of the card. That’s bad, because it doesn’t
reflect how you’re going to use your knowledge when programming.
When you’re programming, you’ll need to actually assign some value or data frame or
whatever, not explain how assignment works1.
So, make cards that ask you to do a concrete task with a concrete solution. In fact, I
think it’s best if I demonstrate this directly and show you how to create new cards in Anki.
To start making cards, click on “Create Deck” (more on decks and cards here) and give it
a name. Then click on that deck and then click on “Add”. You’ll see something like this:
From the drop-down menu that says Basic , select the note type Programming -
Typing :
Now let’s make two Anki cards. In the first one we are going to start really early and train
how to assign a data frame, and in the second one we are jumping into the Tidyverse to
learn how to make a new column.
I’m going to do this in a list of steps, which you can simply repeat for every card you make
on your own.
1. Think of a concrete task that you want to practice
Write this task into the “Task” field of your new Card:
“Assign the number 10 to the variable x ”
2. Write the code for the example, either in RStudio or any other text editor
x <- 10
3. Make sure “S” is selected in the box next to the Lightning symbol
Option / Alt - S .
6. Select portion you want to memorize Select the code you want to memorize, which
here is everything. Make sure to select no whitespace at the beginning or the end,
this might mess up formatting.
7. Press Shift-Cmd-C to turn into a cloze Press Shift-Cmd-C to turn the selected code
into a cloze. When you want to create multiple clozes for one piece of code, repeat
this for the other parts. Note that clozes can’t overlap.
8. If there is anything going on that you want to explain, type this explanation into the
“Explain” field.
9. Write “R” into the “Category” field (unless it’s already in there)
10. Click on “Cards” to see a preview and check if everything is looking as expected.
over the top to say it like this, but I myself have made the mistake of letting my Ankis slip
too often. And then I came back to reviews in the 1000s (no joke) and threw away my
cards, starting from scratch because I was so discouraged.
Don’t suffer like I did. It takes ~10-20min per day, depending on how much knowledge you
choose to put into it. Start slow and steady, and explore these steps to see if they make it
easier for you:
1. Opening the app and doing one single card counts. The most important thing is to
establish the habit of using Anki, and there’s some great research on using tiny
actions to build a habit. I also recommend reading books on building habits in general
(Duhigg (https://www.amazon.com/Power-Habit-What-Life-Business
/dp/081298160X/), Clear (https://www.amazon.com/Atomic-Habits-Proven-Build-
Break/dp/0735211299/)).
2. Do Ankis at a fixed time of day. I do mine directly after getting up and dressing. That
way no matter how my day goes I have accomplished something and nothing can
come in the way of me doing my Ankis. Some people suggest doing Ankis directly
before sleep, and if that works for you, go for it. I know that I’m way less disciplined in
the evening compared to the morning, so I do them in the morning. Know thyself.
3. Keep a Lights Spreadsheet (https://www.ultraworking.com/lights) or use other habit-
supporting apps (something like Strides (https://www.stridesapp.com/)) to help you
visualize your progress in doing Ankis. I find it incredibly motivating to know I have
done Ankis 20, 30 days in a row.
4. Do not learn too many new cards per day. The default of 20 new cards per day is
good, maybe do 30 if you’re confident that you are sticking with it. Do not give in to
the hubris of plowing through 100 cards a day and then stopping to use Anki because
you suddenly have to do 500, 600 or even more reviews per day. Anki is a slow and
steady game. Be humble here so you can be a badass later.
More Resources
Anki is powerful, and has an active community that is very helpful. And I am certainly not
the first to have written about using Anki to learn to code. I’ve collected a couple of links
that might be useful for diving deeper into Anki-Land:
1. Anki Manual (https://apps.ankiweb.net/docs/manual.html): The official manual for
Anki. Can be intimidating and a bit dense, but is still invaluable.
2. Guide to Anki Intervals and Learning Steps (https://www.youtube.com
Appendix
Setting Up Flash Cards in Anki
Anki is an extremely powerful tool for learning just about anything because it allows you to
create custom formatted flashcards that look and behave exactly like you want them to
behave. However, with great power comes great complexity: setting up Anki to your liking
can quickly become a hobby in itself and beginners in particular are often easily
overwhelmed when starting to use Anki. In the following, I’ll show you how to create the
ideal card type for learning R and any other programming language. If this seems
intimidating to you or you just want to get on with your life and learn R already (very
sensible, get after it!), no worries, I’ve created a sample collection of cards, including a
custom card type for you to reuse with your own cards, that you can download here
1. Is the card type (here: “Basic”), which determines which fields a card has and how it
behaves. We will make a new one that is taylored to our needs.
2. This is the Deck, the pile of cards we want to add our new cards to. This can stay the
way it is.
3. Here we customize which fields our card has. Currently we have the “Front” and
“Back” fields to which we can add information that Anki uses to create the cards.
4. This lets us edit the cards that Anki shows us, such as how they look and how they
behave.
5. Here we select for which programming language we want to have syntax highlighting.
6. This field will let us add cloze deletions, the power feature for learning programming
and your new secret weapon. You’ll learn more about how exactly this works in the
next section.
To make our own custom cards to learn R, we first need to add and rename some of the
fields. Because we want to leave the type “Basic” intact, we will make a copy of the type
and then edit this copy. To do so, click on Type: “Basic”, then on “Manage”, “Add”, select
“Clone: Basic”, confirm by clicking “OK” and then give the new card type you want to
create a name. I chose “Programming - Typing” as a name. Confirm by clicking “OK”,
close the pop-up windows and select your new note type:
Now, click on “Fields…” and then rename “Front” to “Task” and “Back” to “Code”:
Then, click on “Add” and add two new fields. One called “Explanation” and one called
“Category”. For the “Category” field, make sure to select “remember last input”.
Then confirm by clicking “Close”. Then click on the “Cards” button, which will take you to a
window looking something like this:
Front Template (1) shows you the HTML of the front side of the card
Styling (2) shows you the CSS code for the highlighting
Back Template (3) Shows you the HTML for the back side of the card
In the front and back template you see things like {{Task}} , which signify the fields you
created. You can reference any field of your card in your template by enclosing it in {{}} .
To set your new card type up for learning R (or any programming language), copy the
following code into the Front Template:
Copy the following code into the Front Template:
<b>{{Category}}:</b><br>
<hr>
{{Task}}<br>
<div class='code'>
{{cloze:Code}}
</div>
{{type:cloze:Code}}
If you know HTML this code is obvious to you, if you don’t know HTML do not worry, you
do not need to. Just make sure you have copied everything into the field and then copy
the following code into the Back Template:
<b>{{Category}}</b><br>
<hr>
{{Task}}<br>
<div class='code'>
{{cloze:Code}}</div><br>
<div style='text-align: center;'>{{type:cloze:Code}}</div>
<br>
<div style='font-family: Helvetica; font-size: 20px;'>{{Explanation}}</div>
The code for the Styling field is very long, so here’s a link to the styling text file (/files
/anki_highlighting_css.txt). Simply copy and paste its content into the appropriate field.
This will ensure that you have proper highlighting of the different variables and functions
when Anki shows you code.
Once you have done all that, you are now set to make flash cards.
For example, in this card I can answer whether I didn’t know it at all, if it was hard to
remember, I remembered it or if it was really easy to remember. Depending on how I
answer, the interval when I will next see the card changes: either in 30 minutes, 2 days, 3
days or 4 days. How long these steps are differes depending on which stage of learning
your card is in. Ankis default settings for the three stages (learning, review, lapsed) are not
entirely optimal, so we are going to tweak them. Remember that you can always return
these settings to the default if you are not happy with them or tweak them to your own
liking. This is just a best practice to get you started.
On your main Anki screen, click on the Gear icon next to the Default deck and select
Options . You are now in the options tab that edits the behavior for new cards, cards you
are just learning. Change the settings to the following:
In the Learning Tab:
in 10 minutes, if you know it again in one day and if you then know it again in six
days.
Order : Show new cards in random order This way you are not cheating yourself
because you are helped in remembering the content of a card because you know its
context in the deck, like the card that came before.
New cards per day :
You can adjust this to your liking. I personally have it set to 30. Remember that the
higher you set this, the more reviews you will have tomorrow and the days after in
addition to the new cards you are seeing for the first time. The Anki Manual states
that with a limit of 20 you’ll on average have to review 200 cards per day. Act
accordingly.
Graduating Interval : 15 days
This means that if you know a card after having it not seen for 15 days it will move
from the “learning” stage to the “review” stage. The purpose of increasing this so
much is so that your have truly adequate time for learning a card before Anki
penalizes you for forgetting it once.
Easy Interval : 20 days
If you press easy when reviewing a card, Anki immediately moves it to the Review
stage and skips any learning steps.
check the box at Bury related cards Another option that helps you learn better by
removing the “context” of a card.
In the Reviews Tab:
Maximum reviews per day : 9999 You want to do all reviews that Anki tells you to
do, because otherwise you are messing up the algorithm. The best way to limit how
many reviews you have to do per day is to only learn a low number of new cards per
day and to do your Ankis literally every day so that you never have a backlog of
reviews to come back to.
check the box at Bury related reviews
Again, remove context to remember you cards better on their own.
In the Lapses Tab:
after and a hundred times more? No, so 20% of the previous interval is good.
If you have any feedback or question, do not hesitate to email me! You can also use the
anonymous feedback form (https://goo.gl/forms/6nlrRQ6bNe31J6Lw1) if you prefer.
1. Obviously, if you do think you’re going to get asked such a question, maybe for an
exam or an interview, make a card that addresses that! Refer to Rule 17:
Redundancy does not contradict minimum information principle
(https://www.supermemo.com/en/articles/20rules).↩
SEARCH
Search
CATEGORIES
TAGS
SPACED-REPETITION (/TAGS/SPACED-REPETITION)
Devoted to provide the tools and skills to (/blog LEARNING R - HOW TO CREATE A
DATAFRAME (/BLOG/R-CREATE-
help you make sense of the world, /r- DATAFRAME/)
empirically. create-
dataframe/)
spaced-
repetition/)
Copyright (c) 2018 - 2019, Lukas Kawerau; all rights reserved. | GDPR/Privacy Policy (/privacy-policy) | Imprint/Impressum
(/imprint)
/hugo-universal-theme)