[AUDIO LOGO]
YULIA: Hi, everyone.
My name is Yulia, And I am one of their preceptors here at CS50.
And today we'll be taking a tour around R Markdown, which is one of another ways
to represent our R programs.
So before, in lectures, while you were completing your problem sets,
you might have seen something like this, which
is called an R script, which is just some lines of code combined under dot R
file.
And what is going on here, and it is from the visualizing data week,
is we're reading in some votes for the candidates.
And then we're using a ggplot function to actually create
a bar chart of those votes.
And before we jump into R Markdown, let's just run these lines of code
to see what they output.
So I get an error that I can't find ggplot,
and that's because I haven't imported my library yet.
So let's do that together.
I'm going to import library(ggplot2), and now when I run my code,
I see it over here on the right, a nice bar chart of the number of votes
that Bowser, Mario, and Peach received.
But as you can tell, this is not a very efficient way
to present this data to a user.
And this is where R Markdown steps in.
So R Markdown is often used for cases where
we want to [INAUDIBLE] data analysis or report where
we combine our code, some text, and maybe some visualizations
together under one file.
So to do that let's actually jump into votes.Rmd file
right here that I've already created.
And before we actually get to the text and the code part,
let's create our header.
And to do that, I'm going to create some space for it using the three dashes.
In between them, I'm going to set some parameters.
The first one being the title, and I'm going to call it "votes analysis."
The second one is author, which is my name.
And the last one is the output format.
So there are several ways in which you can Knit or sort of like render
your R Markdown, one of them being HTML.
And that's what we're going to talk about today.
But you can also Knit it to a PDF or a Word document.
It just might require installation of some other libraries prior to that.
So I'm going to set it to HTML document right here.
And now that our header is done, we can actually
get to the body of our R Markdown.
So as I mentioned before, it consists of some code and some text.
And while the code is the simple part, we already
have it pre-written right here, how do we actually
include some text into our file?
Well, as I said, and as may be clear from the name,
we use markdown language for it.
And so, as you would do in other files or in other setups,
we use hashtag for our titles.
So for example, one hashtag will indicate the biggest title.
And so I'm going to call it CS50R.
And then two hashtags, you can think about it as a smaller title
or a subtitle, and I'm going to call it "Data Visualizing"
because that's the week that we're on.
And then after that, I'm going to use three hashtags to indicate sections
of my code that I will be writing.
So the first section will be loading the necessary libraries.
And the reason I need to do that is, remember
just a minute ago in the console when we tried to run our ggplot function,
it didn't recognize it because we didn't load the library.
And while we already did in the console, and it
might work when we run it just here right
now, when we actually try to Knit it, our compiler won't recognize that.
So we need to make sure we include it in the R Markdown, as well.
And for this, I'm going to create our first code chunk.
To do that, you can click Option Command-I on your keyboard
if you're using a Mac.
And this creates this gray box where you can put your code,
and it will actually be recognized as code and not just as plain text.
So here, I'm going to import the library ggplot2.
And nice thing about R Markdown, is that not only everything runs
when you Knit it, but you can also run it
as you are coding in R Markdown itself by clicking this Play button right here.
And again, we just import library ggplot2.
Now that we've imported our library, let's go back to votes.R
and actually copy our first line of code, which is just
reading in the CSV of votes.csv.
So I'm going to title my next section "Loading the Data."
and again, by clicking Option Command-I, create another other chunk of code
copying votes--
assigning into votes, read.csv votes.csv,
I can run this line by clicking Command Return,
and now we can see in our environment, we actually have right here,
we actually see that we have our data frame.
And not only can you run things in R Markdown, you can also output them,
but you don't need explicitly to call the print function.
In fact, if you just write the name of the variable
that you would like to print out or output below,
you can just write it on the last line of code, click the Play button,
and it will be outputted right under that chunk of code.
All right, so I'm going to delete this for now.
And next, what we're going to do is actually display our bar plot
from the previous file.
So I'm going to title this section, "Displaying Data,"
create my chunk of code again, and then just copy over the ggplot function right
here, perfect.
And so ideally, if we run this, we should see our plot outputted right here
on the bottom.
So the same plot that we've seen before, but it kind of
goes sequentially, which will be nice when we actually
Knit it and see the outputs.
And actually, before we proceed, let's do just that.
So I'm going to click this button right here that says Knit,
and it automatically Knits to HTML, which is my preferred output method.
And so here, we can see all the titles that we've created before.
So CS50.R, Data Visualizing is our subtitle, and all the other parts
our headings.
And you can see that I deleted my last line of code that said votes.
So I actually don't see the data frame outputted,
but I see this line of code that actually loaded it in.
And if I scroll to the very bottom, I can actually see the plot
that I've seen before in my app.
All right, let's go back and actually talk about some other things.
So, so far, I've just been adding some titles and some chunks of code,
but let's actually try to add in some text.
And nice thing about R Markdown, is that it recognizes all characters as text,
unless you put it in this code chunk.
So if I start typing something like, Here I
am going to include electoral data--
let's be fancy --for Mario and his friends.
Nice thing about it, it's going to be recognized as friends.
It's not going to try in and look for a variable called Mario or friends.
This is just plain text, and that's it.
And let's add some features to it.
So for example, what if I wanted to make electoral data in italics?
I can do that by putting some underscores in front and after it.
And maybe I wanted to bold "Mario," and for that,
I can use these two asterisks before and after to make it look bolder.
So let's Save it and Knit it just to see what it outputs.
And right here, I can see that my text is included, in fact, as plain text,
and my "Mario and his friends" are bolded.
"Electoral data" is in fact in italic.
That's nice.
And let's go back to our code and add in some extra things.
So in the beginning, I mentioned that R Markdown is helpful for data analysis,
right?
Well, we loaded our data, we created a bar chart, we wrote in some tags,
but let's actually add in a conclusion in the end.
For that, I'm again going to create a section using this three hashtag called
"Conclusion."
And here, I'm going to write something like, In the end,
Mario received X votes.
Beach received X votes and Bowser received X votes.
And I'm leaving it as X because I'm not really
sure how many votes it got, right?
I'm looking at the bar chart, and I can kind of tell,
but I don't know the precise numbers.
So here comes another nifty thing about R Markdown,
is that you don't actually need a whole code chunk to call for some variable.
You can actually do it inline.
And to do that, can use two backticks, and write "r" inside of them.
So now this chunk will be recognized as R code.
And inside of here, I'm going to call votes, which is my data frame.
I'm going to grab column votes and then grab the first value of it.
And I'm just going to do the same for Peach and Bowser.
So votes, dollar sign, votes, second index.
And for Bowser, it will be votes, dollar sign, votes, third index.
Let's make sure that we have all the syntax correct,
and let's actually try to Knit it, fingers crossed.
OK, and right here on the very, very bottom,
you can see that Mario, in fact, received 100 votes.
Peach received 200 votes and Bowser received 150 votes.
Even though I did not type in those explicit numbers,
R markdown recognizes that I'm actually looking inside my data frame
and trying to grab those values from there.
Let's go back and add in some other things.
So not only you can just grab a specific value, you can use some functions
and compute values inside these tiny R inline code chunks.
So maybe I want to know how many total votes were casted in this election.
So I can say something, The total number of votes
was, again, using my inline R code chunk.
And I'm going to call a sum function, and I'm just
going to sum up all the votes that appear in my votes column.
And I'm going to make them bolded, just so that the numbers
stand out a little bit more.
And I'm going to do that by using two asterisks before and after, just like we
did on the top and right here, as well.
Save it, and if I Knit it, I should see all of these numbers on the very bottom.
So you can see that my numbers are now bolded.
And I also summed up all the votes in the very end.
Let's go back and see just one more thing that we can do with R Markdown.
So before, when we Knitted, we would see, for example, both the plot
and the code.
But what if I just want to see the plot, right?
I'm not really interested in how I got to the plot itself
but rather just seeing the visualization.
So to do that, you can actually change the specific settings
of that code chunk.
And so right here, what I'm going to say is, echo equals false.
Essentially, what it will do, is it will say, OK, I want to see the output of it.
I want to see the plot, but I don't really
want to see the code in my final Knitted file.
And so if I Knit it again, and I go here,
I see this blank data, which is a good sign.
This is where my section is.
I see the plot, and I don't see the code.
That is what I wanted.
I didn't want to see those few lines of code with all the details.
I just want to see the final result. OK, that's cool.
And maybe let's add in one more thing.
So you notice how I have different sections, my conclusion,
displaying the data, loading the data, loading the libraries.
And it can get really tedious having so many different headings,
and maybe you have like a pages and pages of things.
So cool thing about R Markdown, you can automatically
create a table of contents, that will be generated up
top, that are actually hyperlinks.
And you can click on each of those sections,
and it will automatically take you to that part.
So to do that, I'm actually going to make some changes to our header.
So I'm going to say that my output is HTML document,
but inside that HTML document, I also someone to include Table Of Contents,
or TOC for short.
And I'm going to set it to true.
So now I actually want to have my table of contents.
And if I Knit it, I see, on the very top, my bullet points.
And it cascades in the same way in which you assign hash tags to those titles.
So you can see that says CS50R had only one hashtag, so it
means it's the biggest one.
It's on top.
Data, visualizing is kind of like my big theme of this week,
so it comes as the second subtitle.
And after that, I just have my sections where
I actually broke down my program into parts and actually wrote some code.
And so if we click on conclusion, it takes us to the very, very end.
So to summarize, we've seen some cool things
we can do with R Markdown, including text, headers,
generating table of contents, as well as figuring out what lines of code
you want to show on your final Knitted file, what lines of code,
you don't want to show.
And there are lots of other things that you
can do with R Markdown, including different features
and other different markdown styles that you can include,
which we definitely don't have time to cover today,
but I encourage you to look into it yourself.
I hope this was helpful, and I hope you learned something new today.