Notebooks

Start an R project in the git repository:

:computer: 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)

:key: Click to see hints
  • A suitable location could be e.g. [your_project]/data/raw/1992-01-01/


:computer: Create a new R Notebook

:key: Click to see hints
  • File -> New File … -> R Notebook
  • Save it somewhere appropriate, e.g. somewhere in a results folder


:computer: Add code to notebook to read in the dataset

:key: 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)
```


:computer: Add code to do some analysis and plots of the data

:key: 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))
```


:computer: 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

:key: Click to see hints
  • Add *.nb.html to .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


Extra

:computer: Break out some code and put it in a separate .R file in an appropriate place

:computer: Call the external code from the notebook