0:00
So, let's continue working with the same R Markdown document, Module 2 RMD1.
Log into your GitHub account,
open RStudio and the RStudio project for this module 2 RMD1.
Next, we're going to learn how to insert images,
photos or pictures, videos and animations, and footnotes.
We don't actually always have to use R code to add images to your document.
You can also bring in external images.
For example, let's use the picture called sunstar.png.
The read ahead materials had instructions on how to download this picture.
And for now, make sure that the picture is in
the same directory as your R Markdown document.
If you want to see this in your document,
we're going to use some simple R Markdown syntax.
For quick reference, I'm going to go to help and open the Markdown Quick Reference.
If I scroll down just a little bit,
there is an example shown here for inserting images.
The syntax begins with an exclamation point,
followed by two square brackets, followed by parentheses,
and in the square brackets,
you could have an alternate text label that describes your image,
and then inside the parentheses,
you have the filename.
So, let's try that.
Let's create a new header in our document.
We'll put in two hashtags followed by insert images.
And we'll say, here is an image inserted,
and we're going to use that Sunstar image.
I can put in the alt text word Sunstar,
and then I need the name of the file, which is sunstar.png.
I'll click Save and we'll Knit to HTML so you can see the result.
If I scroll down here to the bottom where we added, tada!
Here is your image inserted.
You can also insert images off the web,
by linking them directly to your document,
through the web address URL.
For example if I type in,
Here is the R logo,
and put in some alt text here for
R logo and then the URL for where the R logo is located.
Let me show you where that is.
If you go to the r-project.org website and click on logo,
it says that the logo is available in raster and vector formats.
There is a link here for the SVG format.
You can actually put this URL,
if we copy and paste it into the link shown here.
Now, if I click Save and Knit to HTML, now,
when the document compiles, if I scroll down,
I can see the R logo embedded here within my document.
However, there is something important to note.
The image that actually has the web address will not
compile correctly if you knit to Word or knit to PDF,
and that's because the image is not stored locally.
If you wanted the R logo image,
you would need to download it and put it in your local hard drive.
When you get ready to knit this to Word or to PDF,
let's delete this section with the web link.
And now if I knit to Microsoft Word,
the document should compile,
and you'll be able to see at least the Sunstar image embedded in the final word document.
So, if we scroll down here,
now you see this Sunstar image at the bottom of our Microsoft Word document.
Similarly, we can knit to PDF,
and if we scroll to the bottom of the PDF document,
you can see the Sunstar image inserted here.
If you want to include a video,
you can also embed one and view it in an HTML document.
Let's work again with the Sunstar graphic, but this time,
we're going to get a look at it as an animated GIF and as an mp4 video.
The read ahead materials had instructions on how to
download both the animated GIF and the video.
These are in a subfolder in your project called Sunstar.
Since we have these in a separate folder,
you have to list the folder name when you list the file name.
So, let's insert a new section.
We'll put in two hashtags and we'll say insert an Animated GIF and video.
So, when we do this,
it's very similar to inserting an image.
We start with an exclamation point,
followed by square brackets and parentheses.
We type in a short description for Sunstar,
and this time when we type in the file name,
we have to provide the folder name,
Sunstar with a forward slash and then the name of the file, sunstar.gif.
We can also insert the mp4 in a similar fashion and
type in Sunstar mp4.
So, once we finish,
click save and go ahead and knit to HTML to see the result.
You'll notice when the preview window opens,
you can see the animated gif shown here.
However, the mp4 is not showing up in the R Studio preview window.
If you want to see that,
let's actually go to your Module 2 RMD folder and look for the HTML file.
And let's open this in a separate browser window.
In the final HTML document, you'll notice here,
that the animated GIF plays over and over again,
but the mp4 file is embedded with the built in video.
Since these are animated,
the videos are not going to work when you knit to Word.
The document will compile,
but these sections will be blank.
Also, if you try running Knit to PDF, you'll get an error.
Videos and animated GIFs only work in HTML format,
at least using this approach.
There are new functions and methods being created daily.
So, there's probably a way to embed videos and
animated GIFs and other formats if you look for it.
This is a simple introduction to videos,
that only scratches the surface.
There's an R package called vebmedr,
which allows the embedding of videos like YouTube in
your HTML documents and you can learn more about it at their website shown here.
If you scroll down, they've got several different YouTubes embedded here,
and you can click on these and see how the code
works for embedding these different videos.
Finally, you may want to include a footnote in your document.
The syntax for inserting a footnote are square brackets with an up arrow inserted.
And you can add footnotes in one of two ways.
First, you can add a notation in the text for what you want with an index number,
and then at the end of the document,
you have to provide the content that you want displayed with that footnote index.
Or, secondly, you can use what's called an inline note.
With an in line note, you don't have to remember to go
back and add the footnote references at the end.
Let's add footnotes using both of these methods.
Let's start a new section and say,
Insert text with some footnotes.
So, here's a footnote reference
and we'll put the square brackets with an up arrow,
followed by a one.
So, that's our first footnote,
and we can add another footnote.
So, after we put these in,
we have to remember what each of these indexes is going to reference.
For example, for this first one,
we type it in and say,
Here is the footnote,
and then for the second one that says longnote,
Here is one with multiple blocks.
In between these two though,
let's do an example of an inline note.
So, here is an inline footnote.
This time, the up arrow happens before
the square brackets and then inside the square brackets,
you type out what you want the footnote to say.
For example, Inline notes are easier to write,
since you don't have to pick an identifier and move down to type the note.
So, let's click Save and Knit to HTML,
so you can see the result.
So, if we scroll to the bottom,
you can now see how these footnotes work.
There's three footnotes inserted,
and they're all shown here,
at the very bottom of the document.
Let's also try and see what this looks like in Microsoft Word. Click Knit to Word.
In Microsoft Word, a couple of things to note,
since I didn't take out the animated GIF and the video,
this section is just blank but, that's okay,
we scroll down and now you see where the footnotes are,
and we've got the different indexes shown here,
inside the body of the text, and then,
at the very bottom of the document,
you now have your footnotes listed.
You can also try knitting this to PDF if you so wish but, before you do that,
we need to delete the section that has the animated GIF and video shown here.
Finally, let's back everything up and save your changes to your GitHub repository.
So, open your Git bash window,
and we'll go through the same steps that we've been doing previously.
We'll type in git status,
we're going to add and stage our changes,
we'll commit our changes, so,
we've added more objects to our RMD file, and then finally,
we'll push those changes to the repository,
and we'll type git status,
one last time to double check that there's nothing left to commit,
and the working tree is clean.
If you go back to GitHub and refresh your account,
you'll notice that these changes have been applied.