Here’s the agenda for today:
Markdown is a format for writing in plain text. There are many “flavors” of markdown, but they’re all fairly similar. For example there’s wordpress-flavored markdown, wikipedia-flavored markdown, and stack overflow-flavored markdown. There’s also something like “R-flavored markdown”, which is typed directly into a plain-text document with extension .Rmd
(like this one).
Markdown documents usually end up getting compiled by some system or another. Basically what the compiler does is it takes your plain text document and builds a fancy version of it: the “compiled”/“output” version has the same content but is now a different kind of document – like .docx or .pdf or .html – and has whatever kind of style/formatting you’d expect for that kind of document.
When you’re actually writing, special formatting makes the structure of the document visible (e.g. with ###’s for section headings). And when you actually compile the document, those same structural components will still be there – they’ll just be prettified and adapted to whatever the output format it (e.g. html, pdf, or docx).
The point of markdown is that it’s simultaneously easy for humans to use and easy for computers to understand. Writing in markdown is much, much simpler than writing in e.g. Microsoft Word. There’s no dropdowns and no options relating to indentation, page-margins, etc.: you’re just typing whatever you want to type into a text editor. All the formatting is done externally, e.g. by some css rules that you have somewhere else, or by the engine that’s making your output document (which will be knitr in our case).
note: key to markdown is the idea that content is specified separate from style. This principle also drives the internet, and (I’d say) is directly reflected in the recent move away from older html protocols and towards the complete integration of HTML5 and CSS.
.rmd
file).rmd
filesYou can use the shortcut ctrl+shift+k
to build an output document, even on a normal R script. This will cause knitr to build a “quick report” – basically, a visually nice version of your R script.
Here’s the simplest R Markdown file that one could make, alongside its output:
It’s just a plain text file with a few weird lines at the top. But notice that on the right is what you get when you use knitr::
on it (R Studio shortcut: cmd+shift+k
). The output file has the same content as the source file, but it’s in a viewer-friendly format.
Here’s an example of an R Markdown file with some more useful stuff in it, also alongside its output:
The file itself is only 35 lines, but it illustrates a lot of useful things. See also the default R Studio .Rmd
template, which you can view by going to “File” –> “New File” –> “R Markdown…” and then selecting your output format of choice.
exercise: go through the file template.rmd
line-by-line, and compare it to the output file template.html
to get an idea of how different kinds of things get rendered.
exercise: do the same for week4_skeleton.rmd
, comparing it to week4_skeleton.html
Now here’s the task, which simultaneously functions as an in-class activity, as the exercises for the fourth and final week, and as a “final project”:
exercise: using week4_skeleton.rmd
as a starting point, create an R Markdown document that…
knitr::kable()
for nicer output);table()
s or an aggregate()
; or, use group_by()
and summarize()
from dplyr::
if you want to get fancy);plot()
, barplot()
, or hist()
; andggplot()
, or using the qplot()
function from ggplot2::
(a nice quick way to visualize info – forgot to touch on that last week!)If you can make a document with all of these components, then you have learned quite a lot. If you can make such a document and understand how everything works, then…you’ve learned quite a lot plus even more.
Anyway, final thought:
PRACTICE MAKES PERFECT\(^***\)
\(^***\) I mean of course nothing is ever perfect. But I do think that practice is both a necessary and a sufficient condition for becoming proficient.
If you put the hours in, you will be rewarded more than you might expect. With all the resources the internet has to offer, it is totally possible to become a proficient R user with a year or less of consistent practice.
Most importantly of all, try to stay calm when you get stuck. And you will get stuck. Many times. Getting stuck is perfectly fine and perfectly normal. No one learning to code doesn’t get stuck many, many times. Use google, and use it often. Use stack overflow, and use it often. If you get sidetracked when googling one thing and find yourself reading about some other R-related thing, that is actually a good thing. The more you expose yourself to anything R-related, the faster you’ll learn.
On the course page there’s a list of interesting links that I’ve been gathering the last few weeks. Some of them are as entertaining as they are instructive, so check them out if you want to.
Okay that’s it. Happy hacking yalls!