First of all, let’s create a new RStudio project, to help us keep our data, code and outputs organised. The final step here creates a new script (text file of functions) for us to start typing our functions into:
We’re going to follow standard practice by saving the commands that we want to run in R in a separate file called a script file. We also encourage you to document these commands by using comments: text which is marked by a hash #
symbol so that R knows it’s not a command and doesn’t need to be acted on
This is what your RStudio window should look like now, with the different panels labelled:
Before we can begin running an distance sampling analysis, we need to install the necessary packages in R: unmarked for the distance sampling analysis, and any other packages which are required by unmarked. It’s easy to install packages using RStudio
Activate the packages with the library()
function
Type the following R commands into your new empty R script file in the Source area
Remember to add comments!
Add a comment, preceded by #
, to each line of code to remind yourself what it does
When you type commands into your script, they’re not run in R – you’re just saving them as a record of the code you’re writing
For R to process these commands, you need to pass them to the Console window (bottom left quadrant of RStudio). The quickest way is to:
Run code fragments or chunks
The same approach works to run fragments, or multiple lines - select the code and press Ctrl-EnterCtrl-Enter
When you see code like this: R function
, type or copy-paste it into your script and use Ctrl-EnterCtrl-Enter to run it in the Console, so that:
Remember that at any point you can ask R to show you the help file for a particular function, by typing a question mark followed by its name
RStudio will open the help file in the lower right Plots/Help quadrant