Notebooks
Start an R project in the git repository:
- In RStudio, File -> New project…
- Choose Existing Directory, and select your project working directory
Add a data file to an appropriate place in your working directory.
Use a data file of your own, or this example dataset: bryceveg.R (see dataset description for more info on this dataset)
Click to see hints
- A suitable location could be e.g.
[your_project]/data/raw/1992-01-01/
Create a new R Notebook
Click to see hints
- File -> New File … -> R Notebook
- Save it somewhere appropriate, e.g. somewhere in a
resultsfolder
Add code to notebook to read in the dataset
Click to see hints
- To get code to execute you add a chunk of code (Choose Code -> Insert Chunk if you don’t want to type so much)
- Add a chunk like this to the notebook (of course depending on how you’ve named and organized your files):
```{r}
raw_data_file <- '../data/raw/1992-01-01/bryceveg.R' # relative to where the .Rmd file is!
data <- read.table(raw_data_file,header=TRUE,row.names=1)
```
Add code to do some analysis and plots of the data
Click to see hints
- Insert chunks with some relevant code, e.g.:
# Transformation of Vegetation Data
```{r}
raw_data_file <- '../data/raw/1992-01-01/bryceveg.R' # relative to where the .Rmd file is!
data <- read.table(raw_data_file,header=TRUE,row.names=1)
```
# How many plots does each species occur in?
```{r}
# to get number of presences for each species.
# Note that the first part of the function
# call (veg>0) evaluates to TRUE/FALSE or 1/0),
# and it is the sum of ones and zeros that
# gets calculated.
spc_pres<-apply(veg>0,2,sum)
# to see a plot of the cumulative empirical density
# function (CEDF) for species presences
plot(sort(spc_pres))
```
- Example code taken from http://ecology.msu.montana.edu/labdsv/R/labs/lab1/lab1.html#Introduction
Make git ignore html output from R notebooks, and commit the notebook
The html output files are big and generated automatically when the notebook file is saved, so it’s not necessary to have it in the repository
Click to see hints
- Add
*.nb.htmlto .gitignore - Decide if you want to add and commit the dataset file to the repository, or not
- Commit and push
- You might have noticed that there is support for git in RStudio – check this out and play around with it