0% found this document useful (0 votes)
2 views76 pages

Data Science Tool Box Module 2

Data Science Specialization Module 2

Uploaded by

Math Station
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views76 pages

Data Science Tool Box Module 2

Data Science Specialization Module 2

Uploaded by

Math Station
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Science Tool Box Module 2:

This is an introduction to the command interface.


The command line interface is a way of working with files and folders
that involves typing a command as opposed to pointing and clicking with a mouse.
It's incredibly useful for data science and will be used both when you programming
in R and when you're using other tools in the data scientist tool box.
Play video starting at ::20 and follow transcript0:20
So, what is the command line interface?
Almost every computer that's shipped today comes
with some form of a command line interface.
For this class, the command line
interfaces we'll be using, for Windows, it's
Git Bash and you'll see more about that in the "Introduction to Git".
And for Mac and Linux, it's the terminal.
Play video starting at ::38 and follow transcript0:38
[SOUND] So what can the Command Line Interface do?
Well, you can use it to navigate between folders.
You can use it to create files, folders, and programs, and
then you can use it to edit those files and folders.
Finally you can use it to run computer programs which is one of
the main uses we'll be having for it in a data science program.
Play video starting at ::56 and follow transcript0:56
So first we need to understand the basics of directories.
So directory is just another name for folder, and it's the
common name that you'll be hearing about a lot in this class.
So being in a directory and moving to the directory.
Play video starting at :1:8 and follow transcript1:08
Directories on your computer are organized like a tree.
So there's one directory will be inside of another directory.
And so we can navigate between
those directories using the command line interface.
So, here's an example, we have a music directory and there
are three sub directories, one for each composer here, and so you
can imagine that the Debussy directory is inside of the music
directory and so when we think about the structure of this directory.
We think of being either inside of a directory or being up from a directory.
So, one directory up from the Debussy directory is the music directory.
In general, the directory that's up from the directory that you're
thinking of is the directory that contains the directory you're thinking of.
Play video starting at :1:56 and follow transcript1:56
[SOUND] So the directory structure on your computer looks something like this.
You have sort of a tree structure
where each sub, each directory has sub directories.
Play video starting at :2:6 and follow transcript2:06
And so a couple of special cases that you might want, that need to be considered.
First it's the root directory.
So this is the root directory up here at the top of the tree.
And it's indicated at the top because it contains all of the other directories.
And it's usually the shorthand for the new directory is a slash.
So a slash indicates the new directory.
Play video starting at :2:28 and follow transcript2:28
[SOUND] The other main directory that you might want
to be paying attention to is your home directory.
So that's down here.
So, the home directory is where you log in when you log in your computer and most
of your personal stuff, your photos, your music, your
R function will all be in your personal directory.
Play video starting at :2:45 and follow transcript2:45
And so that would be a sub directory of the root directory again.
Play video starting at :2:51 and follow transcript2:51
[SOUND] So if you want to navigate between those directories using the
command line interface, first you have to have a command line interface.
So, for Windows users you should open the Start menu,
search for git bash, and then open git bash the application.
Play video starting at :3:7 and follow transcript3:07
If you're a Mac user, you open Spotlight,
search for terminal, and then open the terminal application.
Play video starting at :3:13 and follow transcript3:13
When you open the terminal or GitBash, you'll
see something that looks a lot like this.
So this is basically application where you mostly see blank space.
And so what will see is what's called the prompt.
Which is your username.
Here in this case it's Sean followed by [INAUDIBLE].
And then so when you open this up, when you open
up the command line interface, you start in your home directory.
An important term that we'll be using a lot is called your working directory.
So that's whatever directory you're in at the moment.
So when you open you're command line
interface, you're working directory is your home directory.
If you move into a sub directory called
music, then your working directory will be music.
[SOUND] So another thing to keep in mind is the
path, so, people think about the path in the directory structure.
And so we'll be moving around along the path.
And so, suppose that you're down here, you're down
in your home directory which is signified by the tilde.
And so you might want to know what are the
directors that are, directories that are up from your home directory.
So the first directory that's up is the users directory.
And then the next directory up is the root directory.
So you can imagine the path is the set of directories you
have to follow in order to get back up to the root directory.
Play video starting at :4:33 and follow transcript4:33
[SOUND] So if you type PWD when you're in the command line interface.
So here we are at that prompt.
You can see the username, the dollar sign.
Then if you type PWD and hit return, what you'll see is the path.
So, this is the path to the working directory that you're in right now.
So in this case, this person is in the
root directory, then users, and underneath that they're in Sean.
So, that's the path to the working directory that they're in.
So, PWD stands for print working directory.
Play video starting at :5:8 and follow transcript5:08
[SOUND] If you use PWD, you can use that at any time in your directory,
it will always print the directory that
you're in, that you're looking at right then.
Play video starting at :5:18 and follow transcript5:18
So, there's always a recipe that goes with using command line interfaces.
So what you'll do is you'll use what's called the command.
So in the previous case it was called PWD.
Then you'll use something called flags.
These are like parameters or things that you'll pass to
the command that tell it what kind of behavior to take.
And then arguments can be what command is going to modify
so what files it'll look at and things like that.
Play video starting at :5:45 and follow transcript5:45
Depending on the command, you may or may not need to use flags or arguments.
So for example when you were using PWD, it was a command and it didn't require any
sort of options, it didn't require any flags
or arguments in order to change what was happening.
Play video starting at :6: and follow transcript6:00
[SOUND] So again, what we can use is we
can use pwd to print the current working directory.
So, if I'm a different user then, when i go to my
home directory and I type PWD, I'll actually get a different path.
It's the path, users followed by Jeff because now I'm a different user.
And so I have a different current working directory.
Play video starting at :6:21 and follow transcript6:21
Clear will clear all the commands in your current command line interface.
So for example, suppose you typed a bunch of commands.
In this case, we typed PWD, and we have something here in users Jeff,
and we don't want to have that cluttering up the space in our interface anymore.
Then what we can do is type just the
word clear, and that will clear out everything in
the command line interface, and you'll be left with
just a prompt at the top of the screen again.
Play video starting at :6:46 and follow transcript6:46
And a very important command is LS.
So, LS lists files and folders in the current directory.
So, if you type LS, for example, in
direc, [INAUDIBLE] directory, you might get all the subfolders.
So for example, you might get the desktop, the photos, and music.
And last, dash A lists both hidden and unhidden files.
And so for example, the desktop, photos,
and music were all unhidden, but then there
are these files, the trash files and on a Mac and .DS underscore store file.
Those files are start, start with a dot and so they're hidden when you type LS.
They'll also be hidden if you look at in a folder structure on the computer.
But LS dash A reveals those folders.
Play video starting at :7:31 and follow transcript7:31
LS dash A lists some details for those folders.
Play video starting at :7:35 and follow transcript7:35
So for example it lists information about the
size of the folders and stuff like that.
Play video starting at :7:39 and follow transcript7:39
An important note is that both dash A and dash L are flags, so
Play video starting at :7:45 and follow transcript7:45
they're prec, preceded by a dash and often a flag is preceded by a dash.
You can combine them together into one flag by
just catnating the two letters together after the dash.
[SOUND] CD is another command.
It stands for change directory.
It takes as an argument the directory that you want to go visit.
So if you're in a particular directory and you want to move to another directory,
you can pass to CD a particular path and it will take you to that directory.
So if you type CD with no argument at
all it'll just take you straight to your home directory.
Play video starting at :8:23 and follow transcript8:23
And then an important one is if you CD.
What that'll do is it'll change directories up one level, so
you'll just move up one level if you do CD dot dot.
So for example if I'm in my home directory and I type CD Music/Debussy.
Then, I print what directory I've moved to.
I will have moved to that, sub directory by using the CD command.
Play video starting at :8:50 and follow transcript8:50
Then if I wanted to move up just one directory to the music folder
what I could is I could type CD dot dot, and what that would
do is it would just move me up one directory, and so if I
print working directory now I move down into the, or up into the music directory.
Play video starting at :9:6 and follow transcript9:06
[SOUND] If I just type CD and I don't type anything after it like this
right here then what I end up with is just going back to my home directory.
Play video starting at :9:16 and follow transcript9:16
Mkdir or M K D I R stands for make directory so,
it's basically just like what you'd do if you say right click create new folder.
And so, what you need to do is you given
an argument that's the name of the directory you're creating.
So, for example, suppose I'm here again in my home directory, Jeff.
And I type MKDIR, space, documents.
What that will do is it will create a new folder called documents.
So if I type LS, the list out all the directories that I see.
One of the directories will now be documents
that I've just created with this MKDIR command.
So now I can actually move into that
document's directory, and then print my current working directory
and you'll see that I'm now moved into
this new documents directory that I created with MKDIR.
Play video starting at :10:7 and follow transcript10:07
So touch creates an empty file.
So if I type, here again, touch, test file, what that will do
is that it will actually just create a file called test underscore file.
And so if I then list out everything that's in my home directory,
you'll see that now one of the files listed is test underscore file.
So if you need to create a file you use touch.
Play video starting at :10:29 and follow transcript10:29
CP stands for copy.
So suppose you want to take a file and copy it into another file.
What you do is pass copy two arguments.
First is the file that you want to copy and
then the place that you want it to be copied to.
So for example if I type CP and then the test file that I've created.
And then a directory, documents.
What it will do is it will take the test
file and it will move it into the documents directory.
Play video starting at :10:59 and follow transcript10:59
So if I copy that file there and then I move
into the documents directory by typing, CD documents, and I list
out list out the files, you'll see that one of the
files that is in that directory now is the test file.
You can then CD dot dot, and that will put you back into your home directory.
Play video starting at :11:18 and follow transcript11:18
[SOUND] So CP can, CP can also be used for copying the contents of directories.
Play video starting at :11:24 and follow transcript11:24
So suppose you want to move one directory into another directory, you
can do that but you need to use the dash R flag.
So the dash R is a flag that stands for recursive.
And so if you have the line CP.
Dash R, so that's CP with a flag dash R, documents to more documents, it will
copy the entire folder, or the entire directory
documents into a new directory called more documents.
So for example, what we can do here is we can use MKDIR to create more docs.
And then what we can do is we can use
this CP dash R command to move documents into more documents.
Play video starting at :12:6 and follow transcript12:06
Then if we move in to the more docs command using
C or directory using CD and we loop, use the is
command to list everything in that directory, we see that there's
a test file in that directory because it's been copied over.
Play video starting at :12:21 and follow transcript12:21
RN stands for remove.
So, RN is sort of like delete.
You basically use it to delete files that you don't want.
So for example if we list out the files in my
home directory, we now have all these new files that we've created.
Suppose we want to remove this test file.
Then I just type RM space test underscore file and it will remove that file.
So, if I list things out again you'll see that the test file is now missing.
Play video starting at :12:50 and follow transcript12:50
You can use RM to remove entire directories
and their contents by using the dash R flag.
So, in other words you can recursively move all of the files in a directory.
But you should be very careful when you do this because
there is no undo so if you delete it, it's gone.
So be very careful when you're using RM dash R.
So if I use in my home directory RM dash R more docs.
It will delete the more docs directory and all of the files that were inside it.
And so you can see, again, if I LS now
after doing that, all of the more docs has been deleted.
MV stands for move, so with MV you can move files between directories.
So again, suppose I crate this new file with touch.
So I created that file.
And suppose I want to move it into
documents, then I would type MV space the name
of the file, new underscore file, and then
the directory I wanted to move it to documents.
And so what I would end up with if I typed ls now is that there wouldn't be a file in
my main directory, but if I CD, I move to the documents directory.
And I list out everything that's in there.
I end up seeing that I've moved the new file into that directory.
Play video starting at :14:8 and follow transcript14:08
[SOUND] You can also use MV to rename files.
So if you move a file to another file with a new name it will just change the name.
So for example here if I list the files of the
directory I'm in then there's two files, Test File and New File.
And I want to change the name of new file.
I can type MV, space, new underscore file.
That's the file name I was, it was previously.
And I want to create it a renamed file.
So I'm moving, I'm changing the name of this file to renamed file.
And then if I list out all the files that exist now, now there's test file and
renamed file, which had the contents that were previously in new file.
[SOUND] And so echo will print whatever arguments you provide.
So for example, if you to echo, new, hello world!,
like this, what it will do is just print out hello world!
Echo is very useful for printing out
Play video starting at :15:5 and follow transcript15:05
the contents of particular variables that have
been stored that we'll talk about later.
Play video starting at :15:11 and follow transcript15:11
Date will predict pre, pre, print the date.
So if you type date like this you'll get the date
that this actually in this case that the slides were created.
[SOUND] So, summary of commands are PWD, which is printing the working directory.
It's telling you what directory you're in.
Clear, which clears the screen.
LS, which lists the files in the current working directory.
CD, which allows you to change working directories.
MKDIR, which creates a new directory, touch, which creates a new
file, CP, which create, copies a file, RM, which removes a
file or directory if you use the dash R command, or
flag sorry, and MV means moving a file or renaming a file.
And then date and echo are ways to look
at the date or to echo out a particular command.
: Added to Selection. Press [CTRL + S] to save as a note

Like
Dislike
Share
This is a very brief introduction to the Git Version Control System.
Play video starting at ::5 and follow transcript0:05
So Version Control is a system that records
all the changes that you've made to a
file or a set o files over time so that you can, recall specific versions later.
It's one of the most commonly used tools for data scientists because,
often you'll be working on a set of scripts or a set of
programs, and you'll make changes to them and over time, some of
those changes will be good, and some of those changes will be bad.
And maybe other people will be working at the same time on a similar set of
functions that you want to be able to keep
track of everything that's being done to those files.
Play video starting at ::34 and follow transcript0:34
So, many of the ways, the way that many of us work with
files, whether we create something, we save it, we change it and we save
it again, and then when you, what happens is you'll lose those intermediate files,
the files that you saved in between the final version and the first version.
And so, Version Control means we're just going to try
and save or manage all of those intermediate files.
And it's really important when you're sort of collaborating
with others because, they might be using as, a different
intermediate file and you might want to know how to
coordinate what happened when you get to the final version.
Play video starting at :1:9 and follow transcript1:09
So Git is a free and open source version of of a version control system.
It's distributed so it can handle everything from
small to very large projects with speed and efficiency.
It's one of the most commonly used sort of version control systems right now.
Play video starting at :1:24 and follow transcript1:24
It's created by the same people who developed Linux.
It's definitely the most popular version control today compared
to all the other version control systems like SVN.
Play video starting at :1:35 and follow transcript1:35
Everything is stored in local repositories,
or on your computer, and they're called
repos and then you do most of the operations from the command line.
And so this is the link I've given you here is a, sort
of a short history of how Git was developed, and how to get started.
Play video starting at :1:51 and follow transcript1:51
So, the first thing that you need to do is go and get, download Git.
So I've given you the website that you can see right here.
So, if you go to that website and download the appropriate version of
the software for your operating system, that would be the right place to start.
Play video starting at :2:7 and follow transcript2:07
The next thing that you do is, once it's
done downloading, you open it up and you begin
the installation process, so there'll be an install wizard
that will take you through the steps of installing Git.
Play video starting at :2:19 and follow transcript2:19
Unless you really know what you're doing, you should probably just go
with all the default options at each step of the installation process.
Play video starting at :2:26 and follow transcript2:26
Once the installation is finished, you might want to hit
the Finish button, although you may want to check uncheck
the box next to review the release notes because
you probably won't be interested in that at this point.
Play video starting at :2:41 and follow transcript2:41
So the first thing that you want to do is open up a program
called Git Bash which is the command line enr, environment for interacting with Git.
This is particularly true if you're on Windows users.
It should be loca, located in to the directory in which Git was
installed or for Windows users, it will be in the Start menu now.
[BLANK_AUDIO]
So, once you have Git Bash open you'll see a short welcome message followed
by the name of your computer and a dollar sign on the next line.
Play video starting at :3:12 and follow transcript3:12
And so the dollar sign means that it's again, the prompt like you've seen in
the sort of command line interface lecture and so, it's your turn to type a command.
Play video starting at :3:21 and follow transcript3:21
And so, each commit to a Git repository will
be tagged with the username of the person the commit.
Play video starting at :3:28 and follow transcript3:28
So what you need to do to sort of get things
set up is, you need to enter your username and your e-mail.
So you type these commands where you type Git config dash global username.
And here you want to type in your username that you're going to be using.
And then, you do the same thing only user dot email.
And here, you're going to type in the email
that you're going to be using with GitHub.
You only have to do this once when the system opens
up, but you can always change it down the road if
you want to use the same commands, if you want to change say
your user name or the email that's associated with the Git account.
Play video starting at :4:5 and follow transcript4:05
Now type the following to confirm your changes.
So, what you do is you just type git config
dot dash dash list, like I see, like you see here.
And, so, you should be able to see your username and your email and all of that.
[BLANK_AUDIO]
So right now we're actually going to actually just exit Git Bash.
And so so you can do that with this command, just type exit and hit return.
So now that Git is setup on your computer ,we're going to do, do some lectures
on how do you use GitHub, which is a web development tool for dealing with GitHub.
It's dealing with Git, it's widely, widely used
among scientists and does lots of neat stuff.
And so, once that's up and running we'll show you how to do some
of the most important things that you need to do with the Version Control System.
This is a really brief introduction to GitHub.
Git is a version control software that allows you to control and
manage the revisions of projects that
you're working on locally on your computer.
And as such, it's a very useful piece of software on its own.
But GitHub is a more recent website that's been developed.
It allows for you to collaborate on projects together at a bigger scale.
And it's really where the power of Git comes into play.
So GitHub is a web -based hosting service for software development
that uses Git revis, revision control as sort of a driving force.
And so what it allows you to do is contribute to projects online And to have
your projects posted online so that other people
can see them and contribute to them as well.
[SOUND] So, basically what it does is it allows users to push and
pull their local repository, so things that you got under bit, or bit,
version control in Git on your local computer, it allows you to put
Push them and pull them to remote repositories that are on the web.
It also provides users with a home page that displays all of their repositories.
And the repositories that you have on GitHub are backed up
on the server in case something happens to your local ca, copies.
But the real key aspect of GitHub is the social aspect and so the social aspect
allows users to follow one another and to share projects
and to contribute to each other's projects and so that's really the power of GitHub.
One of the things that we noticed bout GitHub is that often
when you see somebody else's work You can sort of learn what
they're working on, how their code works, and it's a great way
to sort of identify people who would be good to work with.
And we've also found that when we put
projects on the web, other people will often
contribute, you know, for free, just because they're
interested in how they can make the software better.
So the first thing that you need to do is go
set up a GitHub account now that you have Git installed.
And so, you'll need to enter a username,
email, a password, and click sign up for GitHub.
[SOUND] An important note here is that you should use
the same email address that you used when you signed up
for Git in the previous lecture so that it, you'll
be able to have the two pieces of software work together.
Play video starting at :2:11 and follow transcript2:11
On the next screen you're going to click the
free plan and then click finish sign up.
And then you'll be signed up for a GitHub account.
[SOUND] This is a required component of the class is having a GitHub account.
Play video starting at :2:23 and follow transcript2:23
It's free, so it's not, it shouldn't be a problem.
After signing up, you will find yourself on this page,
which has several helpful resources for learning about Git and GitHub.
I suggest that you read those tutorials, as they're more in depth than
what we'll be able to cover in this class, and they're incredibly useful.
Play video starting at :2:40 and follow transcript2:40
If you click up here on your username in the upper
right hand corner up there, you can see your GitHub profile.
So, see all the projects that you currently have,
which when you start your account, wont be many.
Play video starting at :2:52 and follow transcript2:52
Your profile will show all of your activity from GitHub.
It'll also show other people who you are and what you're working on.
So, you can fill out information
about what kind of projects you're interested in.
Play video starting at :3:4 and follow transcript3:04
[INAUDIBLE] our plan is for this course is to use this GitHub
Profile as the place that you can sort of build a profile.
Which is a portfolio of the work that you've
developed that shows off your skills as a data scientist.
Play video starting at :3:17 and follow transcript3:17
[SOUND] If you click edit your profile, the right hand
portion of the screen you can add basic information about yourself.
It's totally optional, so you don't have to do that at all.
But if you're doing good work, so for
example, if in the course of this class you
create interesting data science products and ideas, you
want to be able to take credit for that.
In the next lecture, we'll talk you through how you actually
create a repository, a repo, and put it up on GitHub.
In the meantime, definitely explore the documentation on the
GitHub site because it gives you lots of different information.
That will help you when you're doing
virtual control ad when you're working with GitHub.
In this lecture, we'll cover one of the most fundamental things that
you can do with GitHub, which is creating a repository, or a repo.
So, just to, recap, to refresh your memory, so
we have two pieces of software that we talked about.
So, Git is a piece of software that allows you
to do version control of documents, like on your local computer.
And then GitHub is a piece of software that allows you, well, it's a
web service, that allows you to, deal
with repos or repositories, remotely, on the web.
Play video starting at ::27 and follow transcript0:27
So get GitHub allows you to share your repository's with
others, access other users public repositories, and then store copies
of your local repo's on the server in case something
happens to your local version, you have a back up copy.
Play video starting at ::41 and follow transcript0:41
So, there's a couple of different ways of creating a GitHub repository.
One is you can start a repository from scratch, that's creating
your own repo, and another is you can fork another user's repository.
So we'll start off with the first method which is
just creating your own repo from scratch and then we'll
talk a little bit about how you can Fork or
get information from another user and start working on their project.
So key point to keep in mind is that
when people talk about repositories, they often call, use repo.
Play video starting at :1:11 and follow transcript1:11
So, what you can do is, either go to your
profile page, which is going to be [Link] followed by your username.
And click on create a new repo in the
upper right hand corner of the page, or you could
go directly to [Link]/new you'll need to log in to
your GitHub account if you want to do this second version.
Play video starting at :1:31 and follow transcript1:31
So, the first thing you need to do is create a
name for your repo, and type a brief description so make
your name a good idea is to make the name of
the repo Googleable if you want to share it with other people.
And then have a description that's pretty clear and descriptive of the things
that you're going to be trying to do with the full files in that folder.
Play video starting at :1:49 and follow transcript1:49
And then one thing that you can do is
you can select either a public or a private repo.
Public repos are the default, and they're the free
version, and they can be shared with anybody else.
Private repos usually require a paid account but if you are at
an educational institution you can often request up to five private repos.
So then you definitely want to check the box with initialize this re, repository
with a README and click the Create repository button down at the bottom.
Play video starting at :2:20 and follow transcript2:20
So after you've done that, you've created your first GitHub repository.
So you can see that right now this is a file that's a repo, and Nick Carchetti.
Play video starting at :2:31 and follow transcript2:31
repo, and so you can see that it has a README document.
Because we initialized it with a README file.
And so the read me file, if you initiate with the README file, that
will actually be what is, what you observe right here is the README file contents.
And we'll talk a little bit more about how you can use mark down to
create a sort of a README file that makes the repository very easy to understand.
Play video starting at :2:57 and follow transcript2:57
Now you can create a copy of this repo on
your computer, so that you can make changes to it.
So you can open Git Bash, and create a directory on
your computer where you will store your copy of the repo.
So, for example you can do mkdir and then, here it's in your home directory
creating test-repo, and then navigate to this new directory using cd.
Play video starting at :3:20 and follow transcript3:20
And then what you can do is, you can initialize a local get repository by
using the command get init, if you've installed
get, as we've talked about previously in our
Play video starting at :3:32 and follow transcript3:32
previous lectures on Git, and then you can
point your local repository to the remote repository.
In other words, you can link up your
local repository with the remote GitHub repository by typing
git remote add origin, and then the URL
of the remote repository that you created on GitHub.
Now you've linked up your local copy with your remote version of GitHub.
Play video starting at :3:57 and follow transcript3:57
So, what you can do is, you can actually, here's what it looks like in action.
So this is actually a re-creating a repo and then cd'ing that repo and
then setting it up so it's connected to the remote version of that repo as well.
So,
Play video starting at :4:16 and follow transcript4:16
another thing that you can do is fork another user's repository or repo.
And so, the idea here is that what you want to
be able to do is develop software with other people.
So, they'll have repos that they've already created that are on GitHub.
Once you find one of those that you're interested in, you can
go to the, that repository and then you can click the Fork button,
and so what the Fork button will do is, it will create a
copy of that repo as it currently is situated in your GitHub profile.
So now you have it on your GitHub account.
And so, you can make a local copy, so you can
clone the repo down to your compu, computer by using the command.
Git clone, and that will take your local version of
your, or sorry, the remote version of your repo and clone
it down to your computer It will, if you use the
command like this it will clone to your current working directory.
And then what you can do is you can actually work on that and try to
contribute back to the repo, with and hope
that they'll use the changes that you made.
Play video starting at :5:19 and follow transcript5:19
If you made changes to your local copy,
you'll want to push your changes to GitHub.
You may be interested in staying current with the changes using your forked copy.
We'll cover some more of the Git and
GitHub basics in coming lectures, but, really the
basic, the best place to find all this sort of information is to look at these
tutorials right here both on GitHub and on the Git website which gives you a lot
of information about how you Fork and clone
and all the other components of using GitHub.

Like
Dislike
Share
This lecture is going to be a lecture
that covers the basic Git and GitHub commands that
you're going to be using for the files
that you're going to be creating in this class.
Play video starting at ::8 and follow transcript0:08
So the first thing that we should do is sort look at the structure
of where the different files are and what the different commands are going to do.
So you can start off by looking here
at the work space where you're actually working
on files on your computer, so that's like
the directories where you're working with the files.
Then there's an index.
This tells Git what are the files
that it should be controlling under version control.
And then there's the local repository; these are the files
that are stored or version controlled on your local computer.
Finally, there's the remote repository.
In our case, that will always be GitHub.
So, the idea here is that you're starting
off in your workspace and you create a file.
And the first thing that you need to do is you need to add that file to the
index so that Git knows to to monitor that file and keep up with all of its changes.
And then what you need to do is you need to commit that file so that, you need
to put a version of that file in your
local repository so that it can be stored and updated.
So as you make changes, you keep
committing those changes to your local repository.
Play video starting at :1:7 and follow transcript1:07
Finally, sometime when you have made a few Commits and
you want to update the remote repository, then what you'll
be doing is you'll be issuing a Push Command to
be able to put those changes into your remote repository.
Okay, so the first thing that you're
going to need to do is suppose you're working
in a directory that is a repo that is being under version control by Git.
So the first thing that you're going to want to maybe do is put new files
under version controls, so what you need to do is add them to the index.
And so to let know, Git know that they
need to be tracked, you can use the add command.
Play video starting at :1:41 and follow transcript1:41
So this is Gitadd.,
adds all the new files in your current working directory that you are working in,
so this is presuming that you are in the directory where you are adding new files.
Play video starting at :1:52 and follow transcript1:52
Git add -u will actually update what happens to
the files that have changed names or were deleted.
So gitadd.,
will just add all the new files, gitadd -u deals with all
the changes to files either adding or deleting or sort of name changing.
And then gitadd -A does both of the previous things all in one command.
So before you try to commit things to your local repository, you need to make
sure that you use the add command, so that you can add things to the index.
Play video starting at :2:25 and follow transcript2:25
Then once you've added them to the index, you can commit them to your local repo.
And so the way that you do that is you can use the git
commit command, so you do gitcommit, and then a flag, -m, and a message here.
And so the message is hopefully a useful description of
what are the changes that are happening in this command.
So, if I've added some new files, that
message might say I've the following new files.
Or it might tell you a little bit about the things that
you've deleted or changed so that you can change your local repo.
Play video starting at :2:53 and follow transcript2:53
This is only going to make changes to the local repo, it
won't make any changes to GitHub, this is still a local action.
Play video starting at :3: and follow transcript3:00
So if you would like to put things up on GitHub, then what you can
do is, still in that same working
directory, you could type the command, git push.
And so what that will do is take all the changes that you've committed
up until then and it will push them to the remote directory on Github.
Play video starting at :3:16 and follow transcript3:16
Sometimes you might be working on a
project, particularly in this class where there's
a version that might be used by many other people, and you might not
want to edit the version that's being used by everyone because if you make
a lot of changes to it, it might break all the work that they're doing.
So one thing that you might do, first is you might actually create a branch.
So a branch is just another version of the
same directory where you can make changes sort of independently.
So what you can do is you can use git checkout -b and then the
name of the branch that you want to do and that will create a new branch.
The default branch for all the repos that are created with Github is the master
branch, but you can create a repo with
any other names they develop for development branch.
To see what branch you're on at any given time.
If you go into the current working directory where the repo is,
and you type git branch, it'll tell you what branch you're on.
If you want to switch back to the master branch, what you can do is,
you can type git checkout master, and we'll
check you out back to the master branch.
And so you can look at that branch.
Play video starting at :4:15 and follow transcript4:15
One thing that you can do is once you've made a
pull request, or a push of your changes up to your repo.
Suppose that you're working on a repo where you're on a different
branch or you're working on a repo that you forked from somebody else,
then what you might want to do is merge your changes back into
the original repo or into the original branch that you were working on.
To do this you need to issue a pull request.
This is a unique fit, feature of Gitbhub.
It actually isn't a feature of Git.
And so what you do is you go to the Github website
and you, if you go to the branch that you're interested in.
So if you go and pick which branch that you're working on then what you can
do is you can actually click on this
button over here which is compare and pull request.
And what that will do is it will issue a pull
request to the individual that owns that other branch or repo.
So if it's yourself, you'll get a
notification that there's been a pull request.
If it's somebody else, they'll get a notification.
And then they can decide whether to merge that pull request
into their repo or not if they think the changes are appropriate.
So you can see all the changes that were made and
confirm whether they were sort of appropriate and interesting or not.
Play video starting at :5:22 and follow transcript5:22
So I'm giving you the very, very basis of
the Github commands here, but there's a whole lot more.
And you can often run into little tricks and difficulties with Git and Github.
So the best place to start is actually a Github help I found.
But the Git documentation is actually quite thorough, it takes
a little bit more reading and a little bit more doing.
Although my experience has been that the best place to, to
deal with this is to type sort of what you think
you want to do into Google or into Stack Overflow and you
find out the answers much more quickly that way in my experience.
This is the lecture on the basics of markdown.
So markdown is just a text file that's formatted
in a very specific and simple way that sites like
GitHub and also R and R Studio can recognize and
can be used to do a lot of different things.
You'll learn a lot more about markdown in
later courses, for example, the reproducible research course.
But for the purposes of being able to deal with GitHub, this is
also, I'm just going to give you really the basics right now on markdown.
Play video starting at ::27 and follow transcript0:27
So the first thing that you want to be able to
do and so it's just a text file that you're editing.
And the extension for the text file is .md.
So you're going to create a file
that's for example [Link] that's a markdown file.
The first thing that you're going to do is create headings.
So, for example, if you do the double pound like
this, followed by some text it will create a heading for a secondary heading.
So what will, what will happen is if you upload a .md file, say for
example to GitHub, if you use two pound signs and then followed by some text.
You'll end up seeing that the get interpreted by GitHub as a heading.
So slightly larger, more bold text.
You can also create smaller headings by using
a triple ha, or triple pound sign then.
And then typing this is a tertiary heading.
And you'll see that GitHub interprets that as a slightly smaller heading size.
Play video starting at :1:25 and follow transcript1:25
The other thing that you can do with, Markdown in addition to
typing just plain paragraphs is that you can create lists very easily.
So, for example, if you type, is, this star, and they you type
first item in the list, asterisk, second item in the list, so forth.
You'll get an unordered list, in other words a list
with dots, starting at the beginning, nicely formatted by GitHub.
Play video starting at :1:48 and follow transcript1:48
And so, those are the two basic things that you need to know right now.
You can use headings, and you can use lists to sort of organize your file.
And then you can just type plain paragraphs as well.
If you want to [UNKNOWN], know a lot more about markdown, you can try
out this link right here or if once you've installed Rstudio, you can actually
click the button, there's an MD button right in the middle of the
screen, that if you click on that, you'll get a quick guide to markdown.
And so you will don't need R Markdown or
any of the more technical details, until you get to
reproducible research, but this will be useful, in terms
of putting sum into your first few directories on GitHub.
So this lecture is about installing R Packages.
Play video starting at ::3 and follow transcript0:03
When you download R from the comprehensive R
Archive Network CRAN, you get the base R system.
And this includes a bunch of functions that you can
use to summarize data and make plots and things like that.
It basically covers the basic function, functionality
that you'll need including implementing the R language.
But the real reason R is so useful is that there are a lot
of add-on packages that extend this basic
functionality in a bunch of different directions.
Everything from cleaning data, to plotting data,
to analyzing data and making interactive applications.
So, R Packages are developed and published by the larger
R community, hopefully including you at the end of this course.
Play video starting at ::42 and follow transcript0:42
So, to obtain R Packages the primary place that you're going to go is CRAN.
But for some biological applications, and some big data applications, you might also
go to the Bioconductor Project that I have linked to both the websites here.
Play video starting at ::54 and follow transcript0:54
You can also obtain information about the available
packages on CRAN with the available packages function.
And so what you would do is you can enter R, start
up, and you'll get a prompt and you can type this command: a.
And then give it the available packages argument just like this.
And that will be a large number of packages.
So you could just hit a after you hit that you could just type a
and hit return and you would see all the packages, but there would be thousands.
So instead you can use the head command to look at say a certain number,
say just three of those, packages so these are the first three in alphabetical order.
Play video starting at :1:29 and follow transcript1:29
As of the making of this lecture there are approximately
5200 packages on CRAN covering a wide range of topics.
Play video starting at :1:37 and follow transcript1:37
An equally large number available on Bioconductor.
Play video starting at :1:40 and follow transcript1:40
One thing that you can do is if you know the
area that you're working in, but you don't know the R package
you're after, you can go to the Task Views link which
groups together many R packages that are related to a specific topic.
Play video starting at :1:52 and follow transcript1:52
So to install an R Package you primarily use the function [Link].
So what you would, you could do is just
use that with the package name as the argument.
So for example if I want to install the Slidify package what I
would do is I would just
type [Link] and then in quotes, slidify.
And what that would do is that would go to
CRAN and it would install that package on your computer.
Any package on which that package depends will also be downloaded and installed.
This is actually one of the nicest parts about
R, is that it's relatively straightforward to install new packages.
Play video starting at :2:25 and follow transcript2:25
You can also install multiple R packages with a single
line, so what you do is you again, you type, [Link].
And now what you do is you enclose in parentheses, with a C out front.
All the different package names separated by commas,
and surrounded by parenth, or surrounded by quotes.
And then what that would do is install the slidify, ggplot2, and devtools packages.
Play video starting at :2:51 and follow transcript2:51
You can also install packages relatively straightforward procedure
in RStudio, so hopefully you've installed R in RStudio.
You can go up to the Tools Menu and then just go down
to Install Packages, and that will open up a folder that will allow
you to pick the repository and then pick the package that you want to
be able to install from, and it will install that package for you.
Play video starting at :3:14 and follow transcript3:14
Installing packages from Bioconductor is a little bit different.
You don't use [Link], but it's still quite since, straightforward.
So what you do is you go and you type this command, source
and then this website right here, and that will load the biocLite function.
And so then first you just type biocLite by itself and what
that will do is it will install the basic version of Bioconductor.
That's actually quite a few packages so be prepared for a
lot of packages to be installed the first time you run it.
Then the next time you want to install specific
package you again would do just like you would [Link].
As you type biocLite and then c, parentheses, and then
each package name in quotation part, marks separated by a comma.
So that's how you install packages like that.
Play video starting at :4:1 and follow transcript4:01
You can also load the packages after you've installed them.
So if you install it, it doesn't mean that
all of the functions are immediately available to you.
You need to use the library command to tell R which packages to load in.
So, for example, if you've installed the ggplot2 library, and
you want to be able to use the functions in
ggplot2, you need to type in the command library(ggplot2) in
order to get access to that functions in that package.
All packages need to be loaded, will be loaded first, so for example if
you are missing some dependancies, then you wont be able to load that package.
An important note here is that you should not put the package names
In quotes when you are using library otherwise you won't get correct loading.
[INAUDIBLE] some packages produce messages when they are loading and some don't.
Either way you don't need to worry about it.
Play video starting at :4:55 and follow transcript4:55
So after you load a package the functions exported by that package will be attached
at the top of the search list so what you can do is you can type library(ggplot2).
And then if you type search, open parentheses, close parentheses, you will
see all of the functions that are part of the ggplot2 package.
So the summary is that R package is a powerful mechanism for extending
the functionality of our R Packages could be obtained by CRAN or other repositories.
You install the packages, function could be used
to install packages from the R console and then
library is what you do to load the
packages in to actually get access to the functions.

Like
Dislike
Share

This lecture is on installing Rtools.


This lecture is primarily useful for people using Windows computers.
People that are using Mac or Linux don't necessarily need this lecture.
Play video starting at ::9 and follow transcript0:09
So Rtools is a collection of tools that
are necessary for building R packages in Windows.
We won't be doing that right away, but
later in developing data trials you will be doing
that, so you need to be able to
install Rtools if you're going to complete that class.
And so, Rtools is available for, download at
this website up here, as I've listed it.
And so, what you can do is you can find a version
of Rtools that is available that is the latest version, and so you
want to select the .exe download link, and it corresponds to your version of
R, so you need to figure out what version of R you have.
And then, download that version, if you are not sure what version
of R you have, open and restart R and you will see in
the very beginning there will be some text listed out and one of
the things they list out in that text is the version of R.
Play video starting at ::57 and follow transcript0:57
If you, have the most recent version of R, you should select
the most recent Rtools download, it's at the top of the chart.
And so, if you just installed R for this course,
then you will also need the latest version of Rtools.
Play video starting at :1:10 and follow transcript1:10
Once the download completes, you'll just open
the executable file to begin the installation.
Play video starting at :1:16 and follow transcript1:16
So basically unless you know what you're doing, you should just
go with the default selection at each steps of the installation.
There only two steps worth noting, so if
you already have Cygwin installed on your machine,
you should just follow the instructions given during
installation, and those instructions are also at this URL.
Play video starting at :1:34 and follow transcript1:34
And you should make sure that box is
checked to have the installer edit your path.
And so you can see it very, very, if you're very eagle eyed, in this below.
But basically make you sure check the box to allow the installer to edit your path.
Play video starting at :1:48 and follow transcript1:48
Once the Rtools installation completes, you can open our studio, and
you install the dev tools package if you don't already have it.
So, if you type [Link]("devtools") in the console,
you'll figure out whether you have it or not.
And then to install it, you can just, as in previous
lectures, just use [Link]("devtools") like that,
and you'll have the devtools package.
Play video starting at :2:11 and follow transcript2:11
After devtools is done installing you can load it using library(devtools) and then
type find_rtools(), like this as shown below, and this should return a TRUE.
So you should see TRUE printed out on the screen if
Rtools installation has worked properly and you'll be ready to go.
Installing R
Now that we’ve got a handle on what a data scientist is, how to find answers, and then
spent some time going over a data science example, it’s time to get you set up to start
exploring on your own. And the first step of that is installing R.
What is R? What is CRAN?
First, let’s remind ourselves exactly what R is and why we might want to use it.
R is both a programming language and an environment, focused mainly on statistical
analysis and graphics. It will be one of the main tools you use in this and following
courses.
R is downloaded from the Comprehensive R Archive Network, or CRAN, and while this
might be your first brush with it, we will be returning to CRAN time and time again, when
we install packages - so keep an eye out!
Why should you use R?
Outside of this course, you may be asking yourself - why should I use R?
The reasons for using R are myriad, but some big ones are:
1) Its popularity
R is quickly becoming the standard language for statistical analysis. This makes R a great
language to learn as the more popular a software is, the quicker new functionality is
developed, the more powerful it becomes, and the better the support there is!
Additionally, as you can see in the graph below, knowing R is one of the top five languages
asked for in data scientist job postings!
R’s popularity among data scientists from [Link]
2) Its cost
FREE!
This one is pretty self-explanatory - every aspect of R is free to use, unlike some other
stats packages you may have heard of (eg: SAS, SPSS), so there is no cost barrier to using
R!
3) Its extensive functionality
R is a very versatile language - we’ve talked about its use in stats and in graphing, but its
use can be expanded to many different functions - from making websites, making maps
using GIS data, analysing language… and even making these lectures and videos! For
whatever task you have in mind, there is often a package available for download that
does exactly that!
4) Its community
And the reason that the functionality of R is so extensive is the community that has been
built around R. Individuals have come together to make “packages” that add to the
functionality of R - and more are being developed every day!
Particularly for people just getting started out with R, its community is a huge benefit -
due to its popularity, there are multiple forums that have pages and pages dedicated to
solving R problems. We talked about this in the Getting Help lesson; these forums are
great both for finding other people who have had the same problem as you, and posting
your own new problems.
Installing R
Now that we’ve spent some time looking at the benefits of R, it is time to install it! We’ll
go over installation for both Windows and Mac below, but know that these are general
guidelines and small details are likely to change subsequent to the making of this lecture -
use this as a scaffold.
For both Windows and Mac machines, we start at the CRAN homepage: [Link]
[Link]/
Installation - for Windows
If you are on a Windows computer, follow the link Download R for Windows, and follow
the directions there - if this is your first time installing R, go to the base distribution and
click on the link at the top of the page that should say something like “Download R
[version number] for Windows.” This will download an executable file for installation.

The download page for R on Windows


Open the executable, and if prompted by a security warning, allow it to run. Select the
language you prefer during installation and agree to the licensing information. You will
next be prompted for a destination location - this will likely be defaulted to Program Files,
in a subfolder called R, followed by another directory of the version number. Unless you
have any issues with this, the default location is perfect.
The install wizard for installing R
You will then be prompted to select which components should be installed. Unless you
are running short on memory, installing all of the components is desireable. Next you’ll be
asked about startup options, and again, the defaults are fine for this. You will then be
asked where Setup should place shortcuts - this is completely up to you, you can allow it
to add the program to the start menu, or you can click the box at the bottom that says to
not create a start menu link. Finally, you will be asked whether you want a desktop or
Quick Launch icon - up to you! I do not recommend changing the defaults for the registry
entries though.
After this window, the installation should begin. Test that the installation worked by
opening R for the first time!
The R terminal running!
Installation - for Macs
If you are on a Mac computer, follow the link Download R for (Mac) OS X. There you can
find the various R versions for download. Note: If your Mac is older than OS X 10.6 (“Snow
Leopard”), you will need to follow the directions on this page for downloading older
versions of R that are compatible with those operating systems.
Click on the link to the most recent version of R, which will download a .pkg file.
Downloading the .pkg file for Macs
Open the pkg file and follow the prompts as provided by the installer. First, click continue
on the welcome page and again on the important information window page. Next you will
be presented with the software license agreement, again, continue. Next you may be
asked to select a destination for R, either available to all users or to a specific disk - select
whichever you feel is best suited to your setup. Finally, you will be at the “Standard
Install” page; R selects a default directory and if you are happy with that location, go
ahead and click install. At this point, you may be prompted to type in the admin
password, do so, and the install will begin!
Installation of R on a Mac
Once the installation is finished, go to your Applications and find R. Test that the
installation worked by opening R for the first time!
Summary
In this lesson we first looked at what R is and why we might want to use it. We then
focused on the installation process for R on both Windows and Mac computers. Before
moving on to the next lecture, be sure that you have R installed properly.
Installing RStudio
We’ve installed R and can open the R interface to input code, but there are other ways to
interface with R - and one of those ways is using RStudio. In this lesson, we’ll get RStudio
installed on your computer.
What is RStudio?
RStudio is a graphical user interface for R, that allows you to write, edit and store code,
generate, view and store plots, manage files, objects and dataframes, and integrate with
version control systems – to name a few of its functions. We will be exploring exactly
what RStudio can do for you in future lessons, but for anybody just starting out with R
coding, the visual nature of this program as an interface for R is a huge benefit.
Installing RStudio
Thankfully, installation of RStudio is fairly straightforward. First, you go to the RStudio
download page. We want to download the RStudio Desktop version of the software, so
click on the appropriate “Download” , under that heading and you will see a list
of “Installers for supported platforms”.

The RStudio download page. Select RStudio Desktop

The various versions of RStudio available for different operating systems


At this point the installation process diverges for Macs and Windows, so follow the
instructions for the appropriate OS.
Installing RStudio - Windows
For Windows, select the RStudio installer for the various Windows editions (Vista, 7, 8,
10). This will initiate the download process. When the download is complete, open this
executable file to access the installation wizard. You may be presented with a security
warning at this time - allow it to make changes to your computer.
Following this, the installation wizard will open. Following the defaults on each of the
windows of the wizard is appropriate for installation. In brief, on the welcome screen,
click next. If you want RStudio installed elsewhere, “Browse” through your file system.
Otherwise, it will likely default to the “Program Files” folder - this is appropriate. Click
next. On this final page, allow RStudio to create a Start menu shortcut. Click Install.
RStudio is now being installed. Wait for this process to finish; RStudio is now installed on
your computer. Click Finish.
Check that RStudio is working appropriately by opening it from your Start menu.

The installation wizard for RStudio installation


Completing installation of RStudio

RStudio is running!
Installing RStudio - Macs
For Macs, select the Macs OS X RStudio installer (Mac OS X 10.6+ (64-bit)). This will initiate
the download process. When the download is complete, click on the downloaded file and
it will begin to install. When this is finished, the applications window will open. Drag the
RStudio icon into the Applications directory. Test the installation by opening your
applications folder and opening the RStudio software.

Download the Mac appropiate version of RStudio

Drag the RStudio file into your Applications folder to complete installation for RStudio
RStudio is running!
Summary
In this lesson we installed RStudio, both for Macs and for Windows computers. Before
moving on to the next lecture, click through the available menus and explore the software
a bit. We will have an entire lesson dedicated to exploring RStudio, but having some
familiarity beforehand will be helpful!
RStudio Tour
Now that we have RStudio installed, we should familiarize ourselves with the various
components and functionality of it! RStudio provides a cheatsheet of the RStudio
environment - warning: this link initiates a download of a PDF from the RStudio GitHub.
The various quadrants
Rstudio can be roughly divided into four quadrants, each with specific and varied
functions, plus a main menu bar. When you first open RStudio, you should see a window
that looks roughly like this:
RStudio’s main interface
You may be missing the upper left quadrant and instead have the left side of the screen
with just one region, “Console” - if this is the case, go to File > New File > R Script and now
it should more closely resemble the image. You can change the sizes of each of the various
quadrants by hovering your mouse over the spaces between quadrants and click-dragging
the divider to resize the sections.
We will go through each of the regions and describe some of their main functions. It
would be impossible to cover everything that RStudio can do, so we urge you to explore
RStudio on your own too!
The four main quadrants of RStudio, plus the main menu bar
The menu bar
The menu bar runs across the top of your screen and should have two rows. The first row
should be a fairly standard menu, starting with “File” and “Edit.” Below that, there is a
row of icons that are shortcuts for functions that you’ll frequently use.
The commonly used options of the main menu bar
To start, let’s explore the main sections of the menu bar that you will use. The first being
the File menu. Here we can open new or saved files, open new or saved projects (we’ll
have an entire lesson in the future about R Projects, so stay tuned!), save our current
document or close RStudio. If you mouse over “New File”, a new menu will appear that
suggests the various file formats available to you. R Script and R Markdown files are the
most common file types for use, but you can also generate R notebooks, web apps,
websites, or slide presentations. If you click on any one of these, a new tab in the
“Source” quadrant will open. We’ll spend more time in a future lesson on R Markdown
files and their use.

The File menu


The Session menu has some R specific functions, in which you can restart, interrupt or
terminate R - these can be helpful if R isn’t behaving or is stuck and you want to stop what
it is doing and start from scratch.
The Session menu
The Tools menu is a treasure trove of functions for you to explore. For now, you should
know that this is where you can go to install new packages (see next lecture), set up your
version control software (see future lesson: Linking GitHub and RStudio), and set your
options and preferences for how RStudio looks and functions. For now, we will leave this
alone, but be sure to explore these menus on your own once you have a bit more
experience with RStudio and see what you can change to best suit your preferences!
The Tools menu
The console
This region should look familiar to you - when you opened R, you were presented with the
console. This is where you type and execute commands, and where the output of said
command is displayed.
The console
To execute your first command, try typing 1 + 1 then enter at the > prompt. You should
see the output [1] 2 below your command.

Typing into the console and getting an output


Now copy and paste the following into your console and hit enter.
example <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8), nrow = 4, ncol = 2)
This creates a matrix with four rows and two columns, with the numbers 1 through 8.
The environment/history
To view this matrix, first look to the Environment quadrant, where you should see the
following:
The environment quadrant
Click anywhere on the “example” line, and a new tab on the Source quadrant should
appear, showing the matrix you created. Any dataframe or matrix that you create in R can
be viewed this way in RStudio.

Your newly made matrix, opened in a new tab of the source panel
RStudio also tells you some information about the object in the environment, like
whether it is a list or a dataframe or if it contains numbers, integers or characters. This is
very helpful information to have as some functions only work with certain classes of data.
And knowing what kind of data you have is the first step to that.
The quadrant has two other tabs running across the top of it. We’ll just look at the History
tab now. Your history tab should look something like this:

The history tab


Here you will see the commands that we have run in this session of R. If you click on any
one of them, you can click “To Console” or “To Source” and this will either rerun the
command in the console, or will move the command to the source, respectively. Do so
now for your example matrix and send it to Source.
The history tab
The source/The script editor panel
The Source panel is where you will be spending most of your time in RStudio. This is
where you store the R commands that you want to save for later, either as a record of
what you did or as a way to rerun code. We’ll spend a lot of time in this quadrant when
we discuss R Markdown, but for now, click the save icon along the top of this quadrant
and save the script as my_first_R_script.R Now you will always have a record of creating
this matrix.
Your first R script!
Files/help/plots/packages panel
The final region we’ll look at occupies the bottom right of the RStudio window. In this
quadrant, five tabs run across the top: Files, Plots, Packages, Help, and Viewer.
In Files, you can see all of the files in your current working directory. If this isn’t where you
want to save or retrieve files from, you can also change the current working directory in
this tab using the ellipsis at the far right, finding the desired folder, and then under the
“More” cogwheel, setting this new folder as the working directory.
The files tab
In the Plots tab, if you generate a plot with your code, it will appear here. You can use the
arrows to navigate to previously generated plots. The Zoom function will open the plot in
a new window, that is much larger than the quadrant. Export is how you save the plot.
You can either save it as an image or as a PDF. The broom icon clears all plots from
memory.
The plots tab
The Packages tab will be explored more in depth in the next lesson on R packages. Here
you can see all the packages you have installed, load and unload these packages, and
update them.

R packages
Now that we’ve installed R and RStudio and have a basic understanding of how they work
together, we can get at what makes R so special: packages.
What is an R package?
So far, anything we’ve played around with in R uses the “base” R system. Base R, or
everything included in R when you download it, has rather basic functionality for statistics
and plotting but it can sometimes be limiting. To expand upon R’s basic functionality,
people have developed packages. A package is a collection of functions, data, and code
conveniently provided in a nice, complete format for you. At the time of writing, there are
just over 14,300 packages available to download - each with their own specialized
functions and code, all for some different purpose. For a really in depth look at R Packages
(what they are, how to develop them), check out Hadley Wickham’s book from
O’Reilly, “R Packages.”
Side note: A package is not to be confused with a library (these two terms are often
conflated in colloquial speech about R). A library is the place where the package is located
on your computer. To think of an analogy, a library is, well, a library… and a package is a
book within the library. The library is where the books/packages are located.
Packages are what make R so unique. Not only does base R have some great functionality
but these packages greatly expand its functionality. And perhaps most special of all, each
package is developed and published by the R community at large and deposited
in repositories.
What are repositories?
A repository is a central location where many developed packages are located and
available for download.
There are three big repositories:
1. CRAN (Comprehensive R Archive Network): R’s main repository (>12,100 packages
available!)
2. BioConductor: A repository mainly for bioinformatic-focused packages
3. GitHub: A very popular, open source repository (not R specific!)
Take a second to explore the links above and check out the various packages that are out
there!

The big three repositories for R packages


How do you know what package is right for you?
So you know where to find packages… but there are so many of them, how can you find a
package that will do what you are trying to do in R? There are a few different avenues for
exploring packages.
First, CRAN groups all of its packages by their functionality/topic into 35 “themes.” It calls
this its “Task view.” This at least allows you to narrow the packages you can look through
to a topic relevant to your interests.

CRAN’s “Task View” that groups packages into 35 topics


Second, there is a great website, RDocumentation, which is a search engine for packages
and functions from CRAN, BioConductor, and GitHub (ie: the big three repositories). If you
have a task in mind, this is a great way to search for specific packages to help you
accomplish that task! It also has a “task” view like CRAN, that allows you to browse
themes.
More often, if you have a specific task in mind, Googling that task followed by “R package”
is a great place to start! From there, looking at tutorials, vignettes, and forums for people
already doing what you want to do is a great way to find relevant packages.
How do you install packages?
Great! You’ve found a package you want… How do you install it?
Installing from CRAN
If you are installing from the CRAN repository, use the [Link]() function, with the
name of the package you want to install in quotes between the parentheses (note: you
can use either single or double quotes). For example, if you want to install the package
“ggplot2”, you would use: [Link]("ggplot2")
Try doing so in your R console! This command downloads the “ggplot2” package from
CRAN and installs it onto your computer.
If you want to install multiple packages at once, you can do so by using a character vector,
like: [Link](c("ggplot2", "devtools", "lme4"))
If you want to use RStudio’s graphical interface to install packages, go to the Tools menu,
and the first option should be “Install packages…” If installing from CRAN, select it as the
repository and type the desired packages in the appropriate box.

Various methods to install packages within R/RStudio


Installing packages from CRAN through R/RStudio
Installing from Bioconductor
The BioConductor repository uses their own method to install packages. First, to get the
basic functions required to install through BioConductor,
use: source("[Link]
This makes the main install function of BioConductor, biocLite(), available to you.
Following this, you call the package you want to install in quotes, between the
parentheses of the biocLite command, like so: biocLite("GenomicFeatures")

Installing packages with BioConductor


Installing from GitHub
This is a more specific case that you probably won’t run into too often. In the event you
want to do this, you first must find the package you want on GitHub and take note of both
the package name AND the author of the package. Check out this guide for installing from
GitHub, but the general workflow is:
1. [Link]("devtools") - only run this if you don’t already have devtools
installed. If you’ve been following along with this lesson, you may have installed it
when we were practicing installations using the R console
2. library(devtools) - more on what this command is doing immediately below this
3. install_github("author/package") replacing “author” and “package” with their
GitHub username and the name of the package.
Installing packages from GitHub
Loading packages
Installing a package does not make its functions immediately available to you. First you
must load the package into R; to do so, use the library() function. Think of this like any
other software you install on your computer. Just because you’ve installed a program,
doesn’t mean it’s automatically running - you have to open the program. Same with R.
You’ve installed it, but now you have to “open” it. For example, to “open” the “ggplot2”
package, you would run:library(ggplot2)
NOTE: Do not put the package name in quotes! Unlike when you are installing the
packages, the library() command does not accept package names in quotes!
Step one of getting a package is installing it, but to use it, you must load it using library();
similar to installing R and then loading it by opening the .exe file
There is an order to loading packages - some packages require other packages to be
loaded first (dependencies). That package’s manual/help pages will help you out in finding
that order, if they are picky.
If you want to load a package using the RStudio interface, in the lower right quadrant
there is a tab called “Packages” that lists out all of the packages and a brief description, as
well as the version number, of all of the packages you have installed. To load a package
just click on the checkbox beside the package name
Using the RStudio interface to load a package
Updating, removing, unloading packages
Once you’ve got a package, there are a few things you might need to know how to do:
Checking what packages you have installed
If you aren’t sure if you’ve already installed a package, or want to check what packages
are installed, you can use either of: [Link]() or library() with nothing between
the parentheses to check!
In RStudio, that package tab introduced earlier is another way to look at all of the
packages you have installed.
Updating packages
You can check what packages need an update with a call to the
function [Link]() This will identify all packages that have been updated since you
installed them/last updated them.
To update all packages, use [Link](). If you only want to update a specific
package, just use once again [Link]("packagename")
Functions used to see what packages are installed and update them
Within the RStudio interface, still in that Packages tab, you can click “Update,” which will
list all of the packages that are not up to date. It gives you the option to update all of your
packages, or allows you to select specific packages.

Using the RStudio interface to update your packages


You will want to periodically check in on your packages and check if you’ve fallen out of
date - be careful though! Sometimes an update can change the functionality of certain
functions, so if you re-run some old code, the command may be changed or perhaps even
outright gone and you will need to update your code too!
Unloading packages
Sometimes you want to unload a package in the middle of a script - the package you have
loaded may not play nicely with another package you want to use.
To unload a given package you can use the detach() function. For
example, detach("package:ggplot2", unload=TRUE) would unload the ggplot2 package
(that we loaded earlier). Within the RStudio interface, in the Packages tab, you can simply
unload a package by unchecking the box beside the package name.

Unloading or “detaching” a package


Uninstalling packages
If you no longer want to have a package installed, you can simply uninstall it using the
function [Link](). For example, [Link]("ggplot2")
(Try that, but then actually re-install the ggplot2 package - it’s a super useful plotting
package!)
Within RStudio, in the Packages tab, clicking on the “X” at the end of a package’s row will
uninstall that package.
Uninstalling packages
Sidenote: How do you know what version of R you have?
Sometimes, when you are looking at a package that you might want to install, you will see
that it requires a certain version of R to run. To know if you can use that package, you
need to know what version of R you are running!
One way to know your R version is to check when you first open R/RStudio - the first thing
it outputs in the console tells you what version of R is currently running. If you didn’t pay
attention at the beginning, you can type version into the console and it will output
information on the R version you are running. Another helpful command is sessionInfo() -
it will tell you what version of R you are running along with a listing of all of the packages
you have loaded. The output of this command is a great detail to include when posting a
question to forums - it tells potential helpers a lot of information about your OS, R, and
the packages (plus their version numbers!) that you are using.
Ways to see what version of R you are running
Using the commands in a function
In all of this information about packages, we haven’t actually discussed how to use a
package’s functions!
First, you need to know what functions are included within a package. To do this, you can
look at the man/help pages included in all (well-made) packages. In the console, you can
use the help() function to access a package’s help files. Try help(package = "ggplot2") and
you will see all of the many functions that ggplot2 provides. Within the RStudio interface,
you can access the help files through the Packages tab (again) - clicking on any package
name should open up the associated help files in the “Help” tab, found in that same
quadrant, beside the Packages tab. Clicking on any one of these help pages will take you
to that functions help page, that tells you what that function is for and how to use it.
The help functions available to you
Once you know what function within a package you want to use, you simply call it in the
console like any other function we’ve been using throughout this lesson. Once a package
has been loaded, it is as if it were a part of the base R functionality.
If you still have questions about what functions within a package are right for you or how
to use them, many packages include “vignettes.” These are extended help files, that
include an overview of the package and its functions, but often they go the extra mile and
include detailed examples of how to use the functions in plain words that you can follow
along with to see how to use the package. To see the vignettes included in a package, you
can use the browseVignettes() function. For example, let’s look at the vignettes included
in ggplot2:browseVignettes("ggplot2") . You should see that there are two included
vignettes: “Extending ggplot2” and “Aesthetic specifications.” Exploring the Aesthetic
specifications vignette is a great example of how vignettes can be helpful, clear
instructions on how to use the included functions.
How to browse vignettes for packages
Summary
In this lesson, we’ve explored R packages in depth. We examined what a packages is (and
how it differs from a library), what repositories are, and how to find a package relevant to
your interests. We investigated all aspects of how packages work: how to install them
(from the various repositories), how to load them, how to check which packages are
installed, and how to update, uninstall, and unload packages. We took a small detour and
looked at how to check what version of R you have, which is often an important detail to
know when installing packages. And finally, we spent some time learning how to explore
help files and vignettes, which often give you a good idea of how to use a package and all
of its functions.
If you still want to learn more about R packages, here are two great resources! R
Packages: A Beginner’s Guide from Adolfo Álvarez on DataCamp and a lesson from the
University of Washington, on an Introduction to R Packages from Ken Rice and Timothy
Thornton.
R Projects
One of the ways people organize their work in R is through the use of R Projects, a built in
functionality of RStudio that helps to keep all your related files together. RStudio provides
a great guide on how to use Projects so definitely check that out!
What is an R Project?
When you make a Project, it creates a folder where all files will be kept, which is helpful
for organizing yourself and keeping multiple projects separate from each other. When you
re-open a project, RStudio remembers what files were open and will restore the work
environment as if you had never left - which is very helpful when you are starting back up
on a project after some time off! Functionally, creating a Project in R will create a new
folder and assign that as the working directory so that all files generated will be assigned
to the same directory.
What are the benefits to using Projects?
The main benefit of using Projects is that it starts the organization process off right! It
creates a folder for you and now you have a place to store all of your input data, your
code, and the output of your code. Everything you are working on within a Project is self-
contained; which often means finding things is much easier - there’s only one place to
look!
Also, since everything related to one project is all in the same place, it is much easier to
share your work with others - either by directly sharing the folder/files, or by associating it
with version control software. We’ll talk more about linking Projects in R with version
control systems in a future lesson entirely dedicated to the topic!
Finally, since RStudio remembers what documents you had open when you closed the
session, it is easier to pick a project up after a break - everything is set-up just as you left
it!
Creating a Project
There are three ways to make a Project:
1) From scratch - this will create a new directory for all your files to go in
2) From an existing folder - this will link an existing directory with RStudio
3) From version control - this will “clone” an existing project onto your computer (Don’t
worry too much about this one, you’ll get more familiar with it in the next few lessons)
Let’s create a Project from scratch, which is often what you will be doing!
Open RStudio, and under File, select “New Project”. You can also create a new Project by
using the Projects toolbar and selecting “New Project” in the drop down menu, or there is
a “New Project” shortcut in the toolbar.
Ways to initiate a new project
Since we are starting from scratch, select “New Project” and a window will appear. Select
“New Directory” and when prompted about the Project type, select “New Project”

New project options


Pick a name for your project and for this time, save it to your Desktop. This will create a
folder on your Desktop where all of the files associated with this Project will be kept. Click
“Create Project.”

Creating a new project


A blank RStudio session should open.
Your new project
A few things to note:
1) In the “Files” quadrant of the screen, you can see that RStudio has made this new
directory your working directory and generated a single file with the extension “.Rproj”
2) In the upper-right of the window, there is a Projects toolbar that states the name of
your current Project and has a drop down menu with a few different options that we’ll
talk about in a second.

Note the new project file in the Files quadrant and the Project toolbar
Opening a project
Opening an existing Project is as simple as double clicking the .Rproj file on your
computer. You can accomplish the same from within RStudio by opening RStudio and
going to File > Open Project. You can also use the Project toolbar and open the drop down
menu and select “Open Project…”
Ways to open a project
Quitting a project or switching to another
Quitting a project is as simple as closing your RStudio window. You can also go to File >
Close Project, and this will do the same. Finally, you can use the Project toolbar by clicking
on the drop down menu and choosing “Close Project”.
Ways to quit a project
All of these options will quit a Project and doing so will cause RStudio to write which
documents are currently open (so they can be restored when you start back up again) and
it then closes the R session. When you set up your Project, you can tell it to save
environment (so, for example, all of your variables and data tables will be preloaded
when you reopen the project), but this is not the default behaviour.
The Projects toolbar is also an easy way to switch between Projects - click on the drop
down menu and choose “Open Project” and find your new Project you want to open - this
will save the current Project, close it, and then open the new Project within the same
window. If you want multiple Projects open at the same time, do the same but instead
select “Open Project in New Session”. This can also be accomplished through the File
menu, where those same options are available.

Ways to switch between projects


Best practices
When you are setting up a project, it can be helpful to start out creating a few directories.
Try a few strategies and see what works best for you, but most file structures are set-up
around having a directory containing the raw data, a directory that you keep scripts/R
files in, and a directory for the output of your code.
For example:
An example of a possible folder structure to organize your project
If you set up these folders before you start, it can save you organizational headaches later
on in a project when you can’t quite remember where something is!
Summary
In this lesson we’ve covered what Projects in R are, why you might want to use them, how
to open, close, or switch between projects, and some best practices to best set you up for
organizing yourself!

You might also like