Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Welcome to part two of my blog series on R Markdown. In the first part, I went over how to create a basic R Markdown document and how to use R Markdown syntax. In this post, I’m going to talk about how you can use R Markdown to learn R.
So why is R Markdown good for learning R? As you saw in the first post, R Markdown is a method for typing normal and formatted text alongside your R code and its outputs. This is perfect for documenting your analyses by taking notes on specific chunks of code and writing down what worked or didn’t work. This same process is perfect for creating tutorials (like the one here!) and keeping track of what you learn. Eventually, the end goal is to have a series of R Markdown documents that cover all the topics and code that you learn, which include both the code and notes explaining what everything does. These documents then also serve as a guide that you can refer back to for troubleshooting or jogging your memory.
In other words, using R Markdown to learn R allows you to:
-
Have a project-based learning experience
-
Fully document your learning
-
Create a reference that you can look back on in the future if you get stuck or can’t remember something
-
Learn by teaching because you’re explaining things in your own words and taking notes
-
Create a teaching resource for yourself that you can then use to help others as well
You can also follow along with this post as a video if you click on the image below. Start the video at 35:35 to cover the material in this post.
Getting set up
To use R Markdown, you’ll need to have R and RStudio already installed. If you need help with downloading R and RStudio, you can check out my blog post and lessons one, two, and three of my online course.
You’ll also have to install two packages: rmarkdown
and knitr
. To do that you can run install.packages("rmarkdown")
and install.packages("knitr")
. You’ll only need to do this once for your computer (at least until the next time you update R).
If you are completely new to R and R Markdown, then I strongly suggest you start with my previous blog post on how to use R Markdown (after going through the three lessons linked above). It goes through all the most important tools in R Markdown.
With the basic software and packages installed, the first thing is to create a new RStudio project where you’ll be working on your R Markdown documents. RStudio projects are incredibly helpful for file organization and managing working directories, and they remove the need to use functions like setwd()
and getwd()
. You can read more about RStudio projects in our post on the subject here.
To learn more about RStudio Projects and why you should always use them, check out these three other great posts from other blogs:
Go to “File” and click on “New Project…”.
This will open a new window, where you’ll click on “New Directory” and “New Project”. That should take you to the next window, where you can give your project a name, like “Learning R” and then can choose somewhere to save it. Then hit “Create Project”, and you’ll have a new RStudio project.
Now that you’ve created your new project, anytime you want to work on it, all you have to do is just open the ‘.Rproj’ file, and RStudio will open up with the scripts you were working on (R Markdown documents in this case).
With this R project open, go to “File” » “New File” and click on “R Markdown…”
Give your R Markdown document a title and hit OK. I titled my document “The basics”.
Organizing your documents
One method is to create new R Markdown documents for every topic you cover. You might start off with one document that covers the basics, then the next one might cover how to upload data, and then you’ll have another one for data visualization, etc.
If you still want to create one large R Markdown document, or if you have sub-topics within your larger topics, you can add a table of contents to your document. Do this by going to the gear button at the top of the document and clicking on “Output Options”.
From there, a window will open up and you can check the box “Include table of contents”.
After that, you’ll notice that the text “toc: yes” appears at the top of your R Markdown document. When you knit your document, any headers you’ve added will appear in the table of contents at the top (like my header, “The basics”). The table of contents is clickable, so it will take you to wherever that section is in your document.
You can also number the table of contents and the section headings by checking that option in the “Output Options” window.
Numbering the sections can help your document become more clearly organized. If you add subsections, the document will take that into account when numbering. For example, my section 1 is called “The basics”. I made two subsections within “The basics”, called “Defining variables” and “Vectors”. “Defining variables” is given the number 1.1 and “Vectors” is given the number 1.2 because they’re nested under section 1. You can also see that in the table of contents, the subsections are tabbed in under their umbrella section to show that they’re nested.
Dealing with errors
I want to show you one more thing that I like to do when using R Markdown for learning. Sometimes we get errors that show up, and we aren’t sure how to resolve them. For example, in the code below, I get an error that says my variable can’t be found (this is because I haven’t created a variable called “my_number” yet).
When you have an error in your code, R Markdown won’t let you knit the document unless you’ve resolved the error. One thing you could do is to delete the problematic code, but then you might make the same mistake in the future. What you can do instead is copy and paste the error message and insert it in your code chunk as a comment. Then also comment out the code that caused the error, allowing you to knit the document. Then your code chunk might look something like this.
Learning workflow
To summarize, here is a workflow that you can follow for learning R with R Markdown:
-
Start by creating the empty R project and your first R Markdown document (making sure to clear out the example contents of the new R Markdown document). Also make sure to add in a table of contents if you plan on keeping it all in one longer document.
-
Then, as you follow through any tutorials (or online courses! 😄), start new section headings (using ‘#’s) and begin explaining the steps you take to complete the tutorial or lesson.
-
After each set of text or description, add in the associated R code chunk.
-
Knit your document often to see the changes you are making as a stand-alone document and to make sure there are no errors in your code.
-
Follow the steps above for dealing with errors as they come up.
-
Finally, refer back to your knitted HTML document as often as you need, or even print it out as a physical reference if that helps.
All of the tips that I included in this blog post are intended to help you document your learning process. Taking notes and analyzing your own code can help ensure that everything you’re learning is sticking in your head.
And that’s it for my R Markdown tutorial series! I hope you enjoyed these posts. Remember to keep adding to your documents as you learn—it will help you grasp new topics and can even turn the R learning curve into a fun project!
Have any cool R Markdown documents you’ve created? Share links in the comments below! 👇
Also be sure to check out R-bloggers for other great tutorials on learning R
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.