# should include echo=FALSE so this chunck isn't part of report
includeSourceDocuments("/Users/Adam/Desktop/lec4.Rmd")
⇒ /Users/Adam/Desktop/lec4.Rmd
Here is a link to the .Rmd of today’s lecture
Replicator and Sequence functions These functions are handy for making sequences of numbers.
Seq is a generalization of “:”
Seq uses the arguement by
1:5
## [1] 1 2 3 4 5
5:1
## [1] 5 4 3 2 1
seq(0,11, by=2)
## [1] 0 2 4 6 8 10
seq(10,0, by=-2)
## [1] 10 8 6 4 2 0
The replicator function uses the argument times or each
rep(c(0,1), times=5)
## [1] 0 1 0 1 0 1 0 1 0 1
rep(letters[1:5],each=2)
## [1] "a" "a" "b" "b" "c" "c" "d" "d" "e" "e"
rep(1:3, each =2, times=3)
## [1] 1 1 2 2 3 3 1 1 2 2 3 3 1 1 2 2 3 3
The file path is the set of successive folders that bring you to the file.
There is a standard format for file paths. An example: /Users/kaplan/Downloads/0021_001.pdf
. Here the filename is 0021_001
, the filename extension is .pdf
, and the file itself is in the Downloads folder contained in the kaplan folder, which is in turn contained in the Users folder. The starting / means “on this computer”.
The R file.choose()
— which should be used only in the console, not an Rmd file — brings up an interactive file browser. You can select a file with the browser. The returned value will be a quoted character string with the path name.
file.choose()
then select a file. The output is: [1] "/Users/kaplan/Downloads/0021_001.pdf"
For example I have a .csv
file on my Desktop called names.csv. If I want to load it into R I might first find the path:
file.choose()[1] "/Users/Adam/Desktop/names.csv"
Then to load it I type:
my_names <- read.file("/Users/Adam/Desktop/names.csv")
## Reading data with read.csv()
my_names
## id last_name first_name born
## 1 1 Williams John 1932
## 2 2 Elfman Danny 1953
## 3 3 Horner James 1953
## 4 4 Shore Howard 1946
## 5 5 Zimmer Hans 1957
Some common filename extensions for the sort of web resources you will be using:
.png
for pictures.jpg
or .jpeg
for photographs.csv
or .Rdata
for data files.Rmd
for the human editable text of a document (called R Markdown)
*.html
for web pages themselves
It is important that when you do some data cleaning or analysis every command that you use is documented in your report and can be repeated to get your results. This makes your report reproducible. A .Rmd (R Markdown) file makes your report reproducible since it integrates computer commands into the narrative so that graphics are produced by the commands rather than being inserted from another source.
For example when you make a plot you can include the R commands that made the plot in your report using “chuncks”
library(DataComputing)
BabyNames %>%
filter(name=="Adam") %>%
group_by(year) %>%
summarise(total_births=sum(count)) %>%
ggplot( aes(x=year, y=total_births)) + geom_line()
This allows the reader to see how you generated the plot and how to modify it.
In lab this week you will learn about making R Markdown files. There is a link to an R-Markdown quick reference pdf in the data camp course you are doing in lab this week.
Note: Most .Rmd files will draw on a library that needs to be loaded into the R session.
When you compile .Rmd -> .html, R starts a brand new session that has no libraries loaded. You don’t need to install packages but you need to load libraries into your chunk. Usually you put a chunk at the top of your document with that looks like:
# you may want ```{r, include=FALSE} so report doesn't show the contents of this chunk in your output file
library(DataComputing)
library(mosaic)
You have probably noticed URLs in the locator window near the top of your browser. A URL includes in its path name the location of the server on which the file is stored (e.g. www.macalester.edu) followed by the path to the file on that surver. Here, the path is /~montgomery and the file is graywolf.html.
You will sometimes need to copy URLs into your work in R, to access a dataset, make a link to some reference, etc. Remember to copy the entire URL, including the http:// part if it is there. For example, to include the jpeg picture of the gray wolf in your own Rmd file, use the following markup, which includes the URL.
[Gray Wolf](http://www.macalester.edu/~montgomery/GrayWolf.jpg)
For example, here is a link to the R Markdown cheat sheat: R Markdown. You will also see a link to a markdown reference pdf in data camp during this weeks lab.
When you hand in documents for the course, you will almost always be handing in an HTML file. However, it’s very useful to be able to access the original Rmd file from which the HTML was compiled, or perhaps other files such as CSV data. The Data Computing
package provides a way to do this easily. Inside an R chunk in the Rmd file, include the following command:
includeSourceDocuments(“AssignmentTwo.Rmd”)
Of course, instead of always specifying "AssignmentTwo.Rmd"
, give the quoted character string containing the path and filename for your Rmd file. You can construct this easily by using file.choose()
in the console, and copying the result as the argument of includeSourceDocuments()
function. This will embed the named file into your HTML, so that the file will be available directly through the HTML file. You need to view the html in your browser to be able to download the .Rmd file.
For example see the top chunk of today’s lecture notes.
When doing an assignment, you will be creating an .Rmd
file, compiling it to .html
, and then handing in your assignment by uploading the HTML file to b-courses.
To create a new .Rmd file, open the “File/New File/R Markdown/From Template”. Choose the top “DataComputing Simple” template from the menu. This will open a new document in the RStudio editor. The document will be initialized with some contents. You’ll be adding on to these.
Save the .Rmd
file in an appropriate place on your system. It’s up to you what you name the file, so long as it ends with the filename extension .Rmd
. (RStudio will add this extension if you don’t specify one. To avoid mistakes, let RStudio do this work.)
A recommendation: use the assignment number and your last name, e.g. `Assignment-One-Lucas.Rmd. No spaces in the name, please.
Make sure to follow the instructions in the template. As you do your work, edit the .Rmd file that you saved; you don’t need to get another template for that assignment. But you will need to use the template for each assignment.
You will need to turn in your Assignment 3 as an .html
file. It is completely optional this week.
Kitr, Shiny and Pandoc are all included with Rstudio. You only need to download latex