-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Fannin
authored and
Brian Fannin
committed
Aug 1, 2016
1 parent
580fa04
commit d060ca8
Showing
15 changed files
with
310 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# Getting started | ||
|
||
> "R isn't software. It's a community." | ||
> --- John Chambers | ||
This chapter will give you a short tour through R. | ||
|
||
* Enter a few basic commands | ||
|
||
## The Operating Environment | ||
|
||
Right. So, you've got R installed. Now what? Among the first differences you'll encounter relative to Excel is that you now have several different options when it comes to using R. R is an engine designed to process R commands. Where you store those commands and how you deal with that output is something over which you have a great deal of control. Terrible, frighening control. Here are those options in a nutshell: | ||
|
||
* Command-line interface (CLI) | ||
* RGui | ||
* RStudio | ||
* Others | ||
|
||
### Command-line interface | ||
|
||
R, like S before it, presumed that users would interact with the program from the command line. And, if you invoke the R command from a terminal, that's exactly what you'll get. The image below is from my | ||
|
||
![R at the command-line](images/R_CommandLine.png) | ||
|
||
Throughout this book, I will assume that you're using RStudio. You don't have to, but I will strongly recommend it. Why? | ||
|
||
* Things are easier with RStudio | ||
|
||
RStudio, keeps track of all the variables in memory | ||
|
||
* Everyone else is using it. | ||
|
||
OK, not much of an argument. This is the exact opposite of the logic our parents used to try and discourage us from smoking. However, in this case, it makes sense. When you're talking with other people and trying to reproduce your problem or share your awesome code, they're probably using RStudio. Using the same tool reduces the amount of effort needed to communicate. | ||
|
||
## Entering Commands | ||
|
||
Now that you've got an is environment, you're ready to go. That cursor is blinking and waiting for you to tell it what to do! So what's the first thing you'll accomplish? | ||
|
||
Well, not much. We'll get into more fun stuff in the next chapter, but for now let's play it safe. You can use R a basic calculator, so take a few minutes to enter some basic mathematical expressions. | ||
|
||
```{r eval=TRUE, echo=TRUE} | ||
1 + 1 | ||
pi | ||
2*pi*4^2 | ||
``` | ||
|
||
* I can't find the console | ||
|
||
In RStudio, the console may be reached by pressing CTRL-2 (Command-2 on Mac). | ||
|
||
## Getting help | ||
|
||
```{r eval=FALSE, echo=TRUE, size='tiny'} | ||
?plot | ||
??cluster | ||
``` | ||
|
||
Within RStudio, the TAB key will autocomplete | ||
|
||
## The working directory | ||
|
||
The source of much frustration when starting out. | ||
|
||
Where am I? | ||
|
||
```{r eval=TRUE, echo=TRUE, size='tiny'} | ||
getwd() | ||
``` | ||
|
||
How do I get somewhere else? | ||
|
||
```{r eval=FALSE, results='hide', size='tiny'} | ||
setwd("~/SomeNewDirectory/SomeSubfolder") | ||
``` | ||
|
||
Try to stick with relative pathnames. This makes work portable. | ||
|
||
### Directory paths | ||
|
||
R prefers *nix style directories, i.e. "/", NOT "\\". Windows prefers "\\". | ||
|
||
"\\" is an "escape" character, used for things like tabs and newline characters. To get a single slash, just type it twice. | ||
|
||
More on file operations in the handout. | ||
|
||
### Source files | ||
|
||
Typing, editing and debugging at the command line will get tedious quickly. | ||
|
||
A source file (file extension .R) contains a sequence of commands. | ||
|
||
Analogous to the formulae entered in a spreadsheet (but so much more powerful!) | ||
|
||
## Your first script | ||
|
||
```{r} | ||
N <- 100 | ||
B0 <- 5 | ||
B1 <- 1.5 | ||
set.seed(1234) | ||
e <- rnorm(N, mean = 0, sd = 1) | ||
X1 <- rep(seq(1,10),10) | ||
Y <- B0 + B1 * X1 + e | ||
myFit <- lm(Y ~ X1) | ||
``` | ||
|
||
Save this file. | ||
|
||
CTRL-S on Windows/Linux, CMD-S on Mac. | ||
|
||
### Executing a script | ||
|
||
Either: | ||
|
||
1. Open the file and execute the lines one at a time, or | ||
|
||
2. Use the "source" function. | ||
|
||
```{r eval=FALSE} | ||
source("SomefileName.R") | ||
``` | ||
|
||
Within RStudio, you may also click the "Source" button in the upper right hand corner. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.